当前位置:   article > 正文

微信小程序开发-实现选项切换功能,通过实现微信小程序页面滑动功能,同一页面设置多个选项,可以切换显示不同选项,实现选项的显示和隐藏,新手级教程_微信小程序选项卡切换怎么实现

微信小程序选项卡切换怎么实现

需求背景:通过实现微信小程序页面滑动功能,可以在同一个页面设置多个选项,并切换显示不同选项,实现选项的显示和隐藏。

使用的开发工具为:微信小程序开发工具

在微信小程序中的page目录下的新建class页面,并在app.json文件中的"pages"数组里面配置class路径,这样才会显示出这个页面

接着执行以下操作,读者可根据代码示例与代码说明、注释结合来看,并根据实际需求来做调整:

第一步:在class页面的js文件中,使用小程序框架提供的 Page() 函数来创建页面实例,并将需要绑定的数据和事件处理函数添加到实例中。在小程序运行时,这些数据和函数将被绑定到对应的页面元素上,以实现数据驱动和交互功能。

代码示例

  1. // pages/class/class.js
  2. Page({
  3.   /**
  4.    * 页面的初始数据
  5.    */
  6.   data: {
  7.     // tab选项
  8.     tabList: [
  9.       {title: "免费课程",index: "0",},
  10.       {title: "付费课程",index: "1",}
  11.     ],
  12.     tabsId: 0, //默认选型为免费课程
  13.   },
  14.   // 滑动时触发的事件
  15.   slideOn(e) {
  16.     // 拿到当前索引并动态改变
  17.     this.setData({
  18.         tabsId: e.detail.current
  19.     })
  20. },
  21. //点击tab时触发
  22. tabsOn(e) {
  23.     this.setData({
  24.         //拿到当前索引并动态改变
  25.         tabsId: e.currentTarget.dataset.idx
  26.     })
  27. },
  28. })


代码说明:
    1. `data` 对象定义了页面的初始数据。其中包含了两个属性:
       - `tabList` 是一个数组,包含了两个对象,每个对象表示一个选项卡。每个选项卡对象有两个属性:`title` 表示选项卡标题,`index` 表示选项卡的索引。
       - `tabsId` 是一个数字,表示当前选中的选项卡的索引,默认为 0。

    2. `slideOn(e)` 函数是滑动时触发的事件处理函数。当滑动到不同的选项卡时,通过获取滑动事件 `e.detail.current` 的值来动态改变当前选中的选项卡索引,并更新到 `tabsId` 中。

    3. `tabsOn(e)` 函数是点击选项卡时触发的事件处理函数。通过获取点击事件 `e.currentTarget.dataset.idx` 的值来动态改变当前选中的选项卡索引,并更新到 `tabsId` 中。

第二步:在class页面的js文件中引用van图标组件
代码示例

  1. // pages/class/class.json
  2. {
  3.   "usingComponents": {
  4.     "van-icon": "@vant/weapp/icon/index"// 引用van图标组件
  5.   }
  6. }


第三步:在class页面的wxml的文件中来做一个页面布局,通过循环渲染选项卡并监听滑动事件来实现在不同内容之间进行切换,并且根据当前选择的选项卡显示对应内容。

代码示例:

  1. <!--pages/class/class.wxml-->
  2. <view class="top-box">
  3.   <!-- Tab布局 -->
  4.   <view class="navBox">
  5.         <view class="titleBox" wx:for="{{tabList}}" wx:key="index" bindtap="tabsOn" data-idx="{{item.index}}">
  6.             <text class="{{item.index == tabsId ? 'fontColorBox' : ''}}">{{item.title}}</text>  
  7.         </view>
  8.     </view>
  9.     <!-- 内容布局 -->
  10.     <swiper class="swiperTtemBox" bindchange="slideOn" current="{{tabsId}}" circular>
  11.         <!-- circular 启用循环滑动 -->
  12.         <swiper-item>
  13.             <view class="buttum-free-box">
  14.               <view class="buttum-free-box1">
  15.                 <view class="buttum-free-img1">
  16.                   <image class="img1" src="https://ts1.cn.mm.bing.net/th/id/R-C.3de08811a0039edc107724a80006d99d?rik=ORTjMJ2MD0JMCQ&riu=http%3a%2f%2fpic.rmb.bdstatic.com%2f3de08811a0039edc107724a80006d99d.jpeg&ehk=8MwsIqU00BkjlfPY8jS47noobfo7y4Zvh9aKHoBPzRc%3d&risl=&pid=ImgRaw&r=0" mode="aspectFill"/>
  17.                 </view>
  18.                 <view class="buttum-free-txt">
  19.                   <text class="buttum-free-text1">夏天</text>
  20.                   <text class="buttum-free-text2">课时:48h</text>
  21.                   <text class="buttum-free-text3">简介:一场爱与和平之旅</text>
  22.                 </view>
  23.                 
  24.               </view>
  25.               <view class="buttum-free-box2">
  26.                 <view class="buttum-free-img2">
  27.                   <image class="img2" src="https://2f.zol-img.com.cn/product/144/343/ceyLgJjKf4U6.jpg" mode="aspectFill"/>
  28.                 </view>
  29.                 <view class="buttum-free-txt">
  30.                   <text class="buttum-free-text1">大个子熊与小个子猪</text>
  31.                   <text class="buttum-free-text2">课时:12h</text>
  32.                   <text class="buttum-free-text3">简介:一场爱与和平之旅</text>
  33.                 </view>
  34.   
  35.               </view>
  36.               
  37.             </view>
  38.         </swiper-item>
  39.         <swiper-item>
  40.           <!-- 第一行 -->
  41.           <view class="buttum-pay-line1">
  42.             <text class="buttum-pay-line1-text">一年级语文</text>
  43.             <view class="pay-icon1">
  44.               <image class="queen" src="/images/tabs/皇冠-黄色.png"></image>
  45.               <text class="pay-icon1-num">18/40</text>
  46.             </view>
  47.           </view>
  48.           <!-- 第二行 -->
  49.           <view class="buttum-pay-line2">
  50.                   <view class="buttum-pay-left-box">
  51.                     <view class="Unit">Unit 1</view>
  52.                     <view class="queenANDschedule">
  53.                         <image class="queen1" src="/images/tabs/皇冠-灰色.png" mode="aspectFill"></image>
  54.                         <image class="queen2" src="/images/tabs/皇冠-黄色.png" mode="aspectFill"></image>
  55.                       <view class="scheduleout">
  56.                         <view class="schedulein"></view>
  57.                       </view>
  58.                     </view>
  59.                   </view>
  60.                   <view class="buttum-pay-right-box">
  61.                     <image class="lock" src="/images/tabs/锁.png"></image>
  62.                   </view>
  63.                 </view>
  64.                   
  65.           <!-- 第三行 -->
  66.           <view class="buttum-pay-line1">
  67.             <text class="buttum-pay-line1-text">绘本</text>
  68.             <view class="pay-icon1">
  69.               <image class="queen" src="/images/tabs/皇冠-黄色.png"></image>
  70.               <text class="pay-icon1-num">18/40</text>
  71.             </view>
  72.           </view>
  73.           <!-- 第四行 -->
  74.           <view class="buttum-pay-line2">
  75.                   <view class="buttum-pay-left-box">
  76.                     <view class="Unit">Unit 1</view>
  77.                     <view class="queenANDschedule">
  78.                         <image class="queen1" src="/images/tabs/皇冠-灰色.png" mode="aspectFill"></image>
  79.                         <image class="queen2" src="/images/tabs/皇冠-黄色.png" mode="aspectFill"></image>
  80.                       <view class="scheduleout">
  81.                         <view class="schedulein"></view>
  82.                       </view>
  83.                     </view>
  84.                   </view>
  85.                   <view class="buttum-pay-right-box">
  86.                     <image class="lock" src="/images/tabs/锁.png"></image>
  87.                   </view>
  88.                 </view>
  89.         </swiper-item>
  90.     </swiper>
  91.   
  92. </view>

代码说明:

    1. `<view class="top-box">...</view>`:顶部容器,包含了选项卡和内容布局。

    2. `<view class="navBox">...</view>`:选项卡容器,用于展示选项卡列表。

    3. `<view class="titleBox" wx:for="{{tabList}}" wx:key="index" bindtap="tabsOn" data-idx="{{item.index}}">...</view>`:每个选项卡的容器。通过 `wx:for` 循环遍历 `tabList` 数组,并使用 `bindtap` 绑定点击事件处理函数 `tabsOn`。使用 `data-idx="{{item.index}}"` 将当前选项卡的索引传递给事件处理函数。

    4. `<text class="{{item.index == tabsId ? 'fontColorBox' : ''}}">{{item.title}}</text>`:选项卡标题文本。根据当前选中的选项卡索引与循环遍历的索引进行比较,如果相等则添加 `fontColorBox` 样式类,用于标识当前选中的选项卡。

    5. `<swiper class="swiperTtemBox" bindchange="slideOn" current="{{tabsId}}" circular>...</swiper>`:内容布局容器,使用滑动视图来实现切换不同内容区域。

    6. `<swiper-item>...</swiper-item>`:每个内容区域的容器。可以包含不同的子元素来展示不同内容。
    在这段代码中,有两个 swiper-item 子元素用于展示免费课程和付费课程的内容。每个 swiper-item 中包含了一些 view 元素和 image 元素来展示具体的课程信息、图片等。根据需要可以添加更多 swiper-item 来展示更多不同类型的内容。

第四步:在class页面的WXSS文件中设置页面的样式,包含了选项卡、免费课程和付费课程等元素的样式。
代码示例:

  1. /* pages/class/class.wxss */
  2. .top-box {
  3.   padding: 0 10px;
  4. }
  5. /* 选项事件盒子 */
  6. .navBox {
  7.   /* tab整体样式 */
  8.   height: 100rpx;
  9.   display: flex;
  10.   align-items: center;
  11.   position: relative;
  12.   bottom: -25px;
  13.   left: -10px;
  14. }
  15. .fontColorBox {
  16.   /* 选中tab样式 */
  17.   color: white;
  18.   
  19.   font-weight: bold;
  20.   display: flex;
  21.   flex-direction: column;
  22.   align-items: center;
  23.   justify-content: center; 
  24.   background-color: #8ad1fa;
  25.   height: 40px;
  26.   width: 110px;
  27.   border-radius: 18px;
  28. }
  29. .titleBox {
  30.   /* 未选中tab样式 */
  31.   color: #565656;
  32.   font-size: 20px;
  33.   font-weight: 700;
  34.   display: flex;
  35.   flex-direction: column;
  36.   align-items: center;
  37.   padding: 0 10px;
  38. }
  39. .swiperTtemBox {
  40.   /* 内容样式 */
  41.   padding: 16rpx;
  42.   height: calc(100vh - 150rpx);
  43. }
  44. .buttum-free-box {
  45.   display: flex;
  46.   flex-direction: column;
  47.   justify-content: space-between;
  48.   padding: 100rpx 0;
  49. }
  50. .buttum-free-box1 {
  51.   display: flex;
  52.   justify-content: space-between;
  53.   width: 330px;
  54. }
  55. .buttum-free-box2 {
  56.   display: flex;
  57.   justify-content: space-between;
  58.   width: 330px;
  59.   position: relative;
  60.   bottom: -30px;
  61. }
  62. .img1 {
  63.   height: 90px;
  64.   width: 125px;
  65.   border-radius: 18px;
  66. }
  67. .img2 {
  68.   height: 90px;
  69.   width: 125px;
  70.   border-radius: 18px;
  71.   position: relative;
  72.   bottom: -3px;
  73. }
  74. .buttum-free-txt {
  75.   display: flex;
  76.   flex-direction: column;
  77.   justify-content: center; 
  78.   position: relative;
  79.   left: -10px;
  80.   
  81. }
  82. .buttum-free-text1{
  83.   font-size: 18px;
  84.   font-weight: 700;
  85.   color: #565656;
  86.   padding: 5px 0;
  87.   
  88. }
  89. .buttum-free-text2{
  90.   font-size: 16px;
  91.   color: #565656;
  92.   padding: 5px 0;
  93. }
  94. .buttum-free-text3{
  95.   font-size: 16px;
  96.   color: #565656;
  97.   padding: 5px 0;
  98. }
  99. /* 付费课程 */
  100.   /* 第一行 */
  101. .buttum-pay-line1 {
  102.   display: flex;
  103.   justify-content: space-between;
  104.   position: relative;
  105.   bottom: -20px;
  106.   padding: 10px 0;
  107. }
  108. .buttum-pay-line1-text {
  109. display: flex;
  110. align-items: center;
  111. color: #000000;
  112. font-size: 18px;
  113. font-weight: 300;
  114. }
  115. .pay-icon1{
  116.   display: flex;
  117.   align-items: center;
  118. }
  119. .queen{
  120.   width: 80rpx;
  121.   height: 80rpx;
  122. }
  123. .pay-icon1-num{
  124.   font-size: 19px;
  125.   font-weight: 300;
  126.   color: #646464;
  127. }
  128. /* 第二行 */
  129. .buttum-pay-line2 {
  130.   display: flex;
  131.   justify-content: space-between;
  132.   position: relative;
  133.   bottom: -25px;
  134. }
  135. .buttum-pay-left-box {
  136.   background-color: white;
  137.   display: flex; 
  138.   flex-direction: column; 
  139.   align-items: center;
  140.   justify-content: space-around;
  141.   width: 330rpx;
  142.   height: 260rpx;
  143.   border-radius: 18px;
  144. }
  145. .Unit{
  146.   padding: 10px 0;
  147.   font-size: 50rpx;
  148. }
  149. .queenANDschedule {
  150.   display: flex;
  151.   width: 340rpx;
  152.   height: 80rpx;
  153. }
  154. .queen1 {
  155.   width: 100rpx;
  156.   height: 100rpx;
  157.   position: relative;
  158.   left: 8px;
  159.   bottom: 8px;
  160. }
  161. .queen2 {
  162.   width: 89rpx;
  163.   height: 89rpx;
  164.   position: relative;
  165.   left: -24px;
  166.   bottom: 5px;
  167. }
  168. .scheduleout {
  169.   background-color: #b9babc;
  170.   width: 310rpx;
  171.   height: 30rpx;
  172.   border-top-right-radius: 18px;
  173.   border-bottom-right-radius: 18px;
  174.   position: relative;
  175.   bottom: -18px;
  176.   left: -15px;
  177. }
  178. .schedulein {
  179.   background-color: #f0c263;
  180.   width: 130rpx;
  181.   height: 30rpx;
  182.   border-top-right-radius: 18px;
  183.   border-bottom-right-radius: 18px;
  184.   position: relative;
  185.   left: -6px;
  186. }
  187. .buttum-pay-right-box {
  188.   background-color: white;
  189.   display: flex;
  190.   flex-direction: column;
  191.   justify-content: center;
  192.   align-items: center; 
  193.   width: 330rpx;
  194.   height: 260rpx;
  195.   border-radius: 18px;
  196. }
  197. .lock {
  198.   width: 150rpx;
  199.   height: 150rpx;
  200. }

欢迎在评论区留言讨论~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/802583
推荐阅读
相关标签
  

闽ICP备14008679号