原文链接: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. 【Pycharm】【HTML】注释问题

    学习HTML中,遇到的注释前存在空行的问题: 只要找到Pycharm设置中:勾选去掉即可

  2. 阅读笔记—MVC

    MVC设计模式 Model 1 体系结构 在Model 1 体系结构中,每一个请求的目标都是JSP页面.JSP页面负责完成请求所需要的而所有任务,其中包括验证客户.使用JavaBeans访问数据库及管 ...

  3. 【Henu ACM Round #12 A】 Grandma Laura and Apples

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 知道题意之后就是一个模拟的过程了. 用int now记录当前苹果的个数.bool flag记录是否有小数(即半个苹果) (这样处理为 ...

  4. PHP GD 生成图片验证码+session获取储存验证码

    以下分享一个PHP制作的图片验证码案例:案比例如以下图: 运用PHP GD具体请看:http://www.php.net/manual/zh/book.image.php 后台图片代码例如以下: &l ...

  5. 源代码看CoordinatorLayout.Behavior原理

    在上一篇博客CoordinatorLayout高级使用方法-自己定义Behavior中,我们介绍了怎样去自己定义一个CoordinatorLayout的Behavior.通过文章也能够看出Behavi ...

  6. 【Java编码准则】の #01限制内存中敏感数据的生命周期

    当竞争对手的应用程序与我们的应用程序执行在同一个系统上时,我们的应用程序在内存中的敏感数据是非常easy被竞争对手获取的.假设我们的应用程序符合以下几种情况之中的一个,那么竞争对手能够获取到我们应用的 ...

  7. android图像处理系列之四--给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  8. C Tricks(十八)—— 整数绝对值的实现

    为正还是为负:(对 int 类型而言,第一位为符号位,其余为数值,则右移 31 位,再与 1 求与) 如果为正 ⇒ 返回原值 如果为负 ⇒ 对其二进制形式各位取反 + 1 int abs(int x) ...

  9. 12. ZooKeeper之Java客户端API使用—创建会话。

    转自:https://blog.csdn.net/en_joker/article/details/78686649 客户端可以通过创建一个ZooKeeper(org.apache.zookeeper ...

  10. 5.decltype类型拷贝

    #include <iostream> using namespace std; template <class T> void show(T *p) { //初始化 decl ...