• swiper动态渲染视图,解决答题问题


    最近做项目碰到这样一个需求,当当前题目没答的时候,不能进入下一题,而我用的组件是swiper,swiper没有类似阻止滑动的属性,于是只能自己想了.......问题的解决思路就是动态渲染题目,并且过滤掉第一道题,当第一道题答完才渲染第二道题,以此类推,直至渲染全部题目,我们可以采用vue的计算属性,动态渲染题目

    1. computed: {
    2. answeredSubject() {
    3. // 根据答案列表过滤出已答题目,从第二道题开始,跳过第一道题
    4. return this.subject.filter((item, index) => index === 0 || !!this.answerlist[index - 1]);
    5. },
    6. },

    完整代码如下

    1. <template>
    2. <view class="content">
    3. <!-- swiper整屏滑动-->
    4. <swiper :vertical="true" class="swiper" :current="activeIndex" @change="swiperChange">
    5. <swiper-item class="swiper-item" v-for="(item,index) in answeredSubject" :key="index">
    6. <view style="padding: 32rpx">
    7. <view class="title">{{ item.title }}</view>
    8. <view class="option A" :class=" answerlist[index] == 'A'? 'selected':'' " @click="answer(index,'A')">
    9. {{ item.one }}
    10. </view>
    11. <view style="width: 400rpx;height: 700rpx;"></view>
    12. <view class="option B" :class="answerlist[index] == 'B'? 'selected':'' " @click="answer(index,'B')">
    13. {{ item.two }}
    14. </view>
    15. </view>
    16. <view class="footer">向上滑動</view>
    17. </swiper-item>
    18. </swiper>
    19. </view>
    20. </template>
    21. <script>
    22. export default {
    23. data() {
    24. return {
    25. subject: [
    26. {
    27. no: 1,
    28. title: '(1/12)美食展會上,路人手上的美食吸引了你,你會?',
    29. one: 'A.攔住路人問出是哪家店',
    30. two: 'B.記住食物的樣子逐個攤位尋找',
    31. },
    32. {
    33. no: 2,
    34. title: '(2/12)你的朋友突然說要去吃火鍋,你會?',
    35. one: 'A.立刻跟著朋友去',
    36. two: 'B.先去看看有沒有其他更好吃的',
    37. },
    38. {
    39. no: 3,
    40. title: '(3/12)你的朋友突然說要去吃火鍋,你會?',
    41. one: 'A.立刻跟著朋友去',
    42. two: 'B.先去看看有沒有其他更好吃的',
    43. },
    44. {
    45. no: 4,
    46. title: '(4/12)你的朋友突然說要去吃火鍋,你會?',
    47. one: 'A.立刻跟著朋友去',
    48. two: 'B.先去看看有沒有其他更好吃的',
    49. },
    50. {
    51. no: 5,
    52. title: '(5/12)你的朋友突然說要去吃火鍋,你會?',
    53. one: 'A.立刻跟著朋友去',
    54. two: 'B.先去看看有沒有其他更好吃的',
    55. },
    56. ],
    57. activeIndex: 0,//当前显示的swiper索引
    58. answerlist: [
    59. ]
    60. }
    61. },
    62. computed: {
    63. answeredSubject() {
    64. // 根据答案列表过滤出已答题目,从第二道题开始,跳过第一道题
    65. return this.subject.filter((item, index) => index === 0 || !!this.answerlist[index - 1]);
    66. },
    67. },
    68. onLoad() {
    69. console.log(this.answeredSubject)
    70. },
    71. methods: {
    72. answer(index, selectedOption) {
    73. // 填充答案列表,确保答案与题目一一对应
    74. while (this.answerlist.length <= index) {
    75. this.answerlist.push(null); // 填充空答案
    76. }
    77. // 更新用户的答案
    78. this.answerlist[index] = selectedOption;
    79. this.$forceUpdate();//强制刷新视图
    80. // 输出答案列表,用于调试
    81. console.log(this.answerlist);
    82. console.log(this.answeredSubject,'123123')
    83. // 在这里可以执行下一题的逻辑,例如滑动到下一题
    84. // 示例代码:this.swiper.slideTo(index + 1); // 假设您有一个名为 swiper 的 swiper 实例
    85. },
    86. swiperChange(e) {
    87. this.activeIndex = e.detail.current
    88. }
    89. }
    90. }
    91. </script>
    92. <style lang="scss">
    93. .content {
    94. width: 100%;
    95. height: 100vh;
    96. .swiper {
    97. width: 100%;
    98. height: 100vh;
    99. .swiper-item {
    100. width: 100%;
    101. height: 100vh;
    102. .title {
    103. font-size: 40rpx;
    104. margin-top: 80px;
    105. }
    106. .option {
    107. font-size: 35rpx;
    108. margin-top: 40rpx;
    109. }
    110. .selected {
    111. color: #007aff;
    112. }
    113. .A {
    114. float: left;
    115. }
    116. .B {
    117. float: right;
    118. }
    119. .footer {
    120. position: fixed;
    121. bottom: 0;
    122. left: 50%;
    123. transform: translateX(-50%);
    124. background-color: #333;
    125. color: #fff;
    126. padding: 20rpx 40rpx;
    127. border-radius: 10rpx;
    128. animation: slide-up 1.5s infinite alternate;
    129. }
    130. @keyframes slide-up {
    131. 0% {
    132. transform: translateX(-50%) translateY(0);
    133. }
    134. 100% {
    135. transform: translateX(-50%) translateY(-40rpx);
    136. }
    137. }
    138. }
    139. }
    140. }
    141. </style>

  • 相关阅读:
    MongoDB聚合运算符:$bsonSize
    Java面试题-Java核心基础-第四天(变量&方法)
    解决多御浏览器问题的5大干货,看这一篇就够了
    redis为什么使用跳跃表而不是树
    电脑系统还原怎么操作?
    ReACT介绍与llama_index ReActAgent实践
    [acwing周赛复盘] 第 64 场周赛20220813
    每日最新新闻接口,免费好用
    计算机毕业设计Python+djang的药物管理系统
    [Spring boot] Spring boot 整合RabbitMQ实现通过RabbitMQ进行项目的连接
  • 原文地址:https://blog.csdn.net/weixin_51526447/article/details/133086291