原文链接:https://mp.weixin.qq.com/s/p6OMCbTHOYGJsjGOINpYvQ

1

概述

微信最近有很多火爆的小程序。成语猜猜看算得上前十火爆的了。今天我们就分享这样的小教程。希望对大家有所帮助。


快去拿个小板凳,坐等跟多更新

注意:如若需要请联系微信geekxz

2

wxml

<!--pages/content/index.wxml-->
<view style='position: relative;'>
 <view class='container'>
   <view class='ads'>
     <text>【您已获得一个红包,打开支付宝即可领取】</text>
   </view>
   <view class='c-display'>
     <view class='display-others'></view>
     <view class='display-content'>
       <image class="content-img" src='{{curImgUrl}}'></image>
     </view>
     <view class='display-opts'>
       <button class='btn-alert' bindtap='onPrompt' size='mini'>
         提示
       </button>
       <button open-type="share" class='btn-share' size='mini'>
         分享
       </button>
     </view>
   </view>
   <view class='c-opts'>
     <view class='opts-others'></view>
     <view class='opts-content' bindtap='onSelected'>
       <block wx:key='this' wx:for="{{selectedContent}}" wx:for-item="item">
         <view class="content-input" id='result_{{item.index}}' data-id='{{item.index}}' data-content='{{item.content}}'>
           <text data-id='{{item.index}}' data-content='{{item.content}}'>{{item.content}}</text>
         </view>
       </block>
     </view>
     <view class='opts-score'></view>
   </view>
   <view class='c-select' bindtap='onSelect'>
     <block wx:for="{{selectContent}}" wx:for-index="i" wx:for-item="content" wx:key="this">
       <view class='select-index' data-content='{{content}}' id='select_{{i}}'>
         <text data-content='{{content}}'>{{content}}</text>
       </view>
     </block>
   </view>
 </view>
 <!--弹窗  -->
 <dialog is-show="{{isComplete}}" is-pass="{{isTrue}}" bindonNext="generateNewData"/>
</view>

3

js

// pages/content/index.js
var allSelected = require('../../utils/data.js')
Page({
 /**
  * 页面的初始数据
  */
 data: {
   selectContent: [],
   selectedContent: [
     { "index": 0, "content": "" },
     { "index": 1, "content": "" },
     { "index": 2, "content": "" },
     { "index": 3, "content": "" },
   ],
   pics: [
     "1.jpeg", "2.jpeg", "3.png", "4.png", "5.jpg", "6.jpg", "7.jpeg"
   ],
   correctTexts: ["马到成功", "三心二意", "千里之外", "一箭双雕", "不明觉厉", "鸡同鸭讲", "胆小如鼠"],
   basePath: "/resources/",
   sorted: [],
   curImgUrl: "",
   curCorrectText: "",
   allSelected: allSelected.mtData().list,
   isTrue: false,
   isComplete: false,
 },
 /**
  * 提示
  */
 onPrompt: function () {
   for (let i = 0; i < this.data.selectedContent.length; i++) {
     if (this.data.selectedContent[i].content == "") {
       this.data.selectedContent[i].content = this.data.curCorrectText[i];
       break;
     }
   }
   this.updateState();
 },
 /**
  * 随机生成文字
  */
 generateText: function () {
   //清除上一次数据
   if (this.data.selectContent.length > 0) {
     this.data.selectContent.splice(0, this.data.selectContent.length);
   }
   for (let i = 0; i < this.data.curCorrectText.length; i++) {
     this.data.selectContent.push(this.data.curCorrectText[i]);
   }
   let start = Math.floor(Math.random() * 500);
   for (let i = 0; i < 32 - this.data.curCorrectText.length; i++) {
     let index = (start + i) % this.data.allSelected.length;
     this.data.selectContent.push(this.data.allSelected[index]);
   }
   this.data.selectContent.sort(this.randomSort);
 },
 /**
  * 获取新数据
  */
 generateNewData() {
   if (this.data.sorted.length <= 0) {
     console.log("通关...");
     return;
   }
   for (let i = 0; i < this.data.selectedContent.length; i++) {
     this.data.selectedContent[i].content = "";
   }
   let index = this.data.sorted.shift();
   this.data.curImgUrl = this.data.basePath + this.data.pics[index];
   this.data.curCorrectText = this.data.correctTexts[index];
   this.generateText();
   //更新数据
   this.setData({
     curImgUrl: this.data.curImgUrl,
     curCorrectText: this.data.curCorrectText,
     selectContent: this.data.selectContent,
     selectedContent: this.data.selectedContent
   });
 },
 /**
  * check是否结果
  */
 checkResult: function () {
   for (let i = 0; i < this.data.selectedContent.length; i++) {
     if (this.data.selectedContent[i].content != this.data.curCorrectText[i]) {
       return false;
     }
   }
   return true;
 },
 /**
  * 是否作答完毕
  */
 isCompleted: function () {
   for (let i = 0; i < this.data.selectedContent.length; i++) {
     if (this.data.selectedContent[i].content == "") {
       return false;
     }
   }
   return true;
 },
 /**
  * 作答
  */
 onSelect: function (event) {
   let content = event.target.dataset.content;
   if (!content || this.isCompleted()) {
     return;
   }
   for (let i = 0; i < this.data.selectedContent.length; i++) {
     if (this.data.selectedContent[i].content == "") {
       this.data.selectedContent[i].content = content;
       break;
     }
   }
   this.updateState();
 },
 /**
  * 更新作答状态
  */
 updateState() {
   this.setData({ selectedContent: this.data.selectedContent });
   this.data.isComplete = this.isCompleted();
   if (this.data.isComplete) {
     this.data.isTrue = this.checkResult();
     this.setData({ isTrue: this.data.isTrue });
   }
   this.setData({ isComplete: this.data.isComplete });
 },
 /**
  * 修改答案
  */
 onSelected: function (event) {
   let content = event.target.dataset.content;
   let index = event.target.dataset.id;
   if (content != "") {
     this.data.selectedContent[index].content = "";
     this.setData({ selectedContent: this.data.selectedContent });
   }
 },
 /**
  * 随机排序
  */
 randomSort: function (a, b) {
   return Math.random() > 0.5 ? -1 : 1;
 },
 /**
  * 生命周期函数--监听页面加载
  */
 onLoad: function (options) {
   //生成随机排序辅助数组
   for (let i = 0; i < this.data.pics.length; i++) {
     this.data.sorted.push(i);
   }
   this.data.sorted.sort(this.randomSort);
   this.generateNewData();
 },
 /**
  * 生命周期函数--监听页面初次渲染完成
  */
 onReady: function () {
 },
 /**
  * 生命周期函数--监听页面显示
  */
 onShow: function () {
 },
 /**
  * 生命周期函数--监听页面隐藏
  */
 onHide: function () {
 },
 /**
  * 生命周期函数--监听页面卸载
  */
 onUnload: function () {
 },
 /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
 onPullDownRefresh: function () {
 },
 /**
  * 页面上拉触底事件的处理函数
  */
 onReachBottom: function () {
 },
 /**
  * 用户点击右上角分享
  */
 onShareAppMessage: function () {
 }
})

4   css

/* pages/content/index.wxss */
page {
 background-color: #f7be86;
}
.container {
 background-color: #f7be86;
 height: 100%;
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: space-around;
 box-sizing: border-box;
}
.ads {
 margin-top: 20rpx;
 font-size: 22rpx;
 color: red;
 font-weight: bold;
}
.c-display {
 background-color: #9d6137;
 width: 95%;
 margin: 30rpx 0;
 height: 400rpx;
 display: flex;
 flex-direction: row;
 align-items: center;
 border-radius: 16rpx;
}
/*视图操作栏  */
.display-opts {
 width: 30%;
 height: 100%;
 font-size: 30rpx;
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: space-around;
}
.btn-alert {
 padding: 10rpx;
 border-width: 4rpx;
 border-radius: 10rpx;
 color: white;
 background-color: #fcb545;
}
.btn-share {
 padding: 10rpx;
 border-width: 4rpx;
 border-radius: 10rpx;
 color: #259f3d;
 background-color: #9bff14;
}
/*视图图片显示区  */
.display-content {
 width: 60%;
 display: flex;
 flex-direction: row;
 justify-content: center;
}
.content-img {
 width: 100%;
 max-height: 350rpx;
}
.display-others {
 width: 10%;
}
.c-opts {
 width: 100%;
 /* margin-top: 20rpx; */
 display: flex;
 flex-direction: row;
}
.opts-others {
 width: 20%;
}
.opts-content {
 width: 60%;
 display: flex;
 flex-direction: row;
 align-items: center;
 justify-content: space-around;
}
.opts-score {
 width: 20%;
}
.content-input {
 display: flex;
 align-items: center;
 justify-content: center;
 font-size: 40rpx;
 color: white;
 border-radius: 10rpx;
 width: 80rpx;
 height: 80rpx;
 background-color: #852200;
}
.c-select {
 margin-top: 70rpx;
 width: 100%;
 height: 400rpx;
 display: flex;
 flex-direction: row;
 flex-wrap: wrap;
 align-items: center;
 justify-content: center;
}
.select-index {
 display: flex;
 align-items: center;
 justify-content: center;
 font-size: 30rpx;
 color: #852200;
 margin: 10rpx 10rpx;
 width: 66rpx;
 height: 66rpx;
 background-color: #fce6be;
 border-radius: 8rpx;
}

以上代码为效果为 图一

注意路径问题

文末福利:

福利一:前端,Java,产品经理,微信小程序,Python等100G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880

福利二:微信小程序入门与实战全套详细视频教程。

【领取方法】

关注 【编程微刊】微信公众号:

回复【小程序demo】一键领取130个微信小程序源码demo资源。

回复【领取资源】一键领取前端,Java,产品经理,微信小程序,Python等资源合集100G资源大放送。

微信小程序--成语猜猜看的更多相关文章

  1. 微信小程序获取手机号码看这篇文章就够了

    前言 微信小程序获取手机号码,从官方文档到其他博主的文档 零零散散的 (我就是这样看过来 没有一篇满意的 也许是我搜索姿势不对) 依旧是前人栽树 后人乘凉 系列.保证看完 就可以实现获取手机号码功能 ...

  2. 关于微信小程序的一些看法和理解

    最近做了几个小时的调研,微信小程序 微信小程序是可以理解成在微信中的APP,他的目标是APP的替代者,由于目前的APP主要区分安卓和IOS,或者其他平台, 那么微信小程序的平台在微信,在任何一个手机系 ...

  3. 从零开始学做微信小程序,看这些就够了!

    随着正式开放公测,微信小程序再次万众瞩目,越来越多的企业和个人涌入到小程序开发的大军中.小程序究竟是什么?适合做小程序的产品有哪些?做小程序需要提前准备什么?如何零基础学做小程序?此文,将列出OSC上 ...

  4. 微信小程序来了,小程序都能做些什么

    2017年的微信大动作就是微信小程序了,到底小程序都能做些什么?这是很多人关注的热点,小程序开发对企业又有什么帮助呢?下面让厦门微信小程序开发公司来为你就分析下.       微信小程序与APP的关系 ...

  5. 微信小程序--家庭记账本开发--04

    界面的布局 在微信小程序开发过程中,界面的布局是十分重要的,无论是一个什么样的程序,界面的美观合理才能提供给客户一个较好的使用体验,就微信小程序布局自己看了许多小程序布局,自己将学习心得记录如下: 下 ...

  6. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

  7. 通过微信小程序看前端

    前言 2016年9月22日凌晨,微信官方通过“微信公开课”公众号发布了关于微信小程序(微信应用号)的内测通知.整个朋友圈瞬间便像炸开了锅似的,各种揣测.介绍性文章在一夜里诞生.而真正收到内测邀请的公众 ...

  8. 换个角度看微信小程序[推荐]

    去年参加几次技术沙龙时,我注意到一个有意思的现象:与之前大家统一接受的换名片不同,有些人并不愿意被添加微信好友--"不好意思,不熟的人不加微信". 这个现象之所以有意思,是因为名片 ...

  9. 微信小程序的开发:通过微信小程序看前端

    前言 2016年9月22日凌晨,微信官方通过"微信公开课"公众号发布了关于微信小程序(微信应用号)的内测通知.整个朋友圈瞬间便像炸开了锅似的,各种揣测.介绍性文章在一夜里诞生.而真 ...

随机推荐

  1. [lougu2243]双端队列搜索

    正统双端队列搜索 回顾:普通队列进行边权为定值的最短路 每次到达都是最优的(意味着不用取min) why? 因为所有状态按照 入队的先后顺序 具有 层次单调性,每次扩展,都往外走一步,满足从起始到该状 ...

  2. python2 pip安装包等出现各种编码错误UnicodeDecodeError: 'ascii'(/或者utf-8) codec can't decode byte 0xd2...

    1.问题描述: python2环境,pip安装包时报错UnicodeDecodeError: 'ascii'(/或者utf-8) codec can't decode byte 0xd2... 类似如 ...

  3. Python day2 知识回顾

    标准库一般放在lib,site-packages放自己下载的:起的py文件名称不要和模块名字相同:import sys#print(sys.path)#打印去哪里寻找这些模块的路径#print(sys ...

  4. 怎样从Cortex-m向STM32移植使用SPI接口协议

    /*************************************************************************************************** ...

  5. Codeforces 441 B. Valera and Fruits

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. LeetCode 06 ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行 ...

  7. Makefile 文件格式

    Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o  code.o gcc  -o  test   prog.o   code.o 当中 ...

  8. eclipse - 下载网址

    这里面有着非常齐全的eclipse相关资源,而且都是放在网盘里面的,下载也方便 http://www.androiddevtools.cn/

  9. 5.Node.js 安装配置

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js安装包及源码下载地址为:https://nodejs.org/en/downlo ...

  10. 一个小的考试系统 android 思路

    一个小的考试系统 android 思路 假如有 100 组,每组有4个单选钮,设置超时检测确认后去测结果估分视图去切换,如果还有,就再显示下一组 所有结束就给个总结显示 有超时结束过程加上 提示正确选 ...