经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
小程序实现选择题选择效果
来源:jb51  时间:2018/11/5 10:46:23  对本文有异议

本文实例为大家分享了小程序实现选择题的显示方法,供大家参考,具体内容如下

下面是三张效果图:初始图,选项正确图,选项错误图。

wxml代码:

  1. <view class='selection'>
  2. <view class='{{view1}}' bindtap='view1Click' id='1'>a</view>
  3. <view class='{{view2}}' bindtap='view2Click' id='2'>b</view>
  4. <view class='{{view3}}' bindtap='view3Click' id='3'>c</view>
  5. <view class='{{view4}}' bindtap='view4Click' id='4'>d</view>
  6. </view>

js代码:

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. view1: 'selection1',
  7. view2: 'selection1',
  8. view3: 'selection1',
  9. view4: 'selection1',
  10. // 默认答案为2,后台会给的
  11. key: 2,
  12. // 选项是否被点击
  13. isSelect: false
  14. },
  15. view1Click: function(e) {
  16. var select = e.target.id
  17. // 选项没被选择时将执行
  18. if (!this.data.isSelect) {
  19. // 将选项设置为“已被选择”
  20. this.setData({
  21. isSelect: true
  22. })
  23. // 注意!此处必须是'=='而不是'='
  24. if (select == this.data.key) {
  25. this.setData({
  26. view1: 'selection2'
  27. })
  28. } else {
  29. this.setData({
  30. view1: 'selection3'
  31. })
  32. // 将正确选项显示出来
  33. this.showAnswer(this.data.key)
  34. }
  35. }
  36. },
  37. view2Click: function(e) {
  38. var select = e.target.id
  39. // 选项没被选择时将执行
  40. if (!this.data.isSelect) {
  41. this.setData({
  42. isSelect: true
  43. })
  44. // 注意!此处必须是'=='而不是'='
  45. if (select == this.data.key) {
  46. this.setData({
  47. view2: 'selection2'
  48. })
  49. } else {
  50. this.setData({
  51. view2: 'selection3'
  52. })
  53. // 将正确选项显示出来
  54. this.showAnswer(this.data.key)
  55. }
  56. }
  57. },
  58. view3Click: function(e) {
  59. var select = e.target.id
  60. // 选项没被选择时将执行
  61. if (!this.data.isSelect) {
  62. this.setData({
  63. isSelect: true
  64. })
  65. // 注意!此处必须是'=='而不是'='
  66. if (select == this.data.key) {
  67. this.setData({
  68. view3: 'selection2'
  69. })
  70. } else {
  71. this.setData({
  72. view3: 'selection3'
  73. })
  74. // 将正确选项显示出来
  75. this.showAnswer(this.data.key)
  76. }
  77. }
  78. },
  79. view4Click: function(e) {
  80. var select = e.target.id
  81. // 选项没被选择时将执行
  82. if (!this.data.isSelect) {
  83. this.setData({
  84. isSelect: true
  85. })
  86. // 注意!此处必须是'=='而不是'='
  87. if (select == this.data.key) {
  88. this.setData({
  89. view4: 'selection2'
  90. })
  91. } else {
  92. this.setData({
  93. view4: 'selection3'
  94. })
  95. // 将正确选项显示出来
  96. this.showAnswer(this.data.key)
  97. }
  98. }
  99. },
  100. showAnswer: function(key) {
  101. // 通过swith语句判断正确答案,从而显示正确选项
  102. switch (key) {
  103. case 1:
  104. this.setData({
  105. view1: 'selection2'
  106. })
  107. break;
  108. case 2:
  109. this.setData({
  110. view2: 'selection2'
  111. })
  112. break;
  113. case 3:
  114. this.setData({
  115. view3: 'selection2'
  116. })
  117. break;
  118. default:
  119. this.setData({
  120. view4: 'selection2'
  121. })
  122. }
  123. }
  124. })

wxss代码:

  1. .selection1{
  2. width: 400rpx;
  3. height: 150rpx;
  4. background-color: grey;
  5.  
  6. display: flex;
  7. flex-direction: column;
  8. justify-content: space-around;
  9. align-items: center;
  10. }
  11. .selection2{
  12. width: 400rpx;
  13. height: 150rpx;
  14. background-color: blue;
  15. display: flex;
  16. flex-direction: column;
  17. justify-content: space-around;
  18. align-items: center;
  19. }
  20. .selection3{
  21. width: 400rpx;
  22. height: 150rpx;
  23. background-color: red;
  24. display: flex;
  25. flex-direction: column;
  26. justify-content: space-around;
  27. align-items: center;
  28. }
  29. .selection{
  30. width: 750rpx;
  31. height: 800rpx;
  32. display: flex;
  33. flex-direction: column;
  34. justify-content: space-around;
  35. align-items: center;
  36. }
  37.  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号