小程序实现点击tab切换左右滑动

本文实例为大家分享了小程序实现点击tab切换左右滑动的具体代码,供大家参考,具体内容如下

wxml

<scroll-view scroll-x="true" class="navbar-box">
 <block wx:for="{{recordMain}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">
  <view class="nav-item " data-current="{{idx}}" bindtap="switchNav">
   <text class="{{currentTab == idx ? 'active' : ''}}">{{navItem.title}}</text>
  </view>
 </block>
</scroll-view>

<swiper style="margin-top:80rpx;height:{{winHeight - 40}}px;" class="tab-box" current="{{currentTab}}" duration="300" data-current="{{idx}}" bindchange="switchTab">
 <swiper-item style="height:100%;overflow-y:scroll" wx:for="{{[0,1,2,3,4,5]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-cnetent">
  <block wx:for="{{tabContent}}" wx:key=" " bindtap='myOrderDetails'>
   <!-- 列表 -->
   <view class='listBox'>
     <view class='listTop'>
      <image src='{{item.goodsImg}}' class='goodsImg'></image>
      <view class='infor'>
       <view class=''>
        <text class='name'>{{item.name}}</text>
        <text class='price'>¥{{item.price}}</text>
       </view>
       <view class=''>
        <text class='choose'>{{item.choose}}</text>
        <text class='number'>×{{item.number}}</text>
       </view>
      </view>
     </view>
     <view class='listBottom'>
      <view>共{{item.number}}件商品,合计:¥{{item.allPrice}}</view>
      <view class='status'>
       <button>查看物流</button>
       <button>确认收货</button>
      </view>
     </view>
   </view>
  </block>
 </swiper-item>
</swiper>

wxss

::-webkit-scrollbar {
 width: 0;
 height: 0;
 color: transparent;
}

.navbar-box {
 height: 70rpx;
 line-height: 70rpx;
 position: fixed;
 top: 0rpx;
 background: white
}

.nav-item {
 display: inline-block;
 width: 16.6%;
 text-align: center;
}

.nav-item text {
 padding-bottom: 10rpx;
}

page {
 background: #f2f2f2;
 font-size: 28rpx;
}

.active {
 color: #a53533;
 border-bottom: 4rpx solid #a53533;
 box-sizing: border-box;
}

.menu {
 font-size: 28rpx;
 width: 100%;
 /* overflow-x: scroll; */
 border-bottom: 20rpx solid #f2f2f2;
 padding: 30rpx 30rpx 0rpx 30rpx;
 box-sizing: border-box;
 display: flex;
 justify-content: space-between;
 position: fixed;
 top: 0rpx;
 z-index: 999;
 background: white;
}

.chooseNav {
 padding-bottom: 10rpx;
}

.listBox {
 padding: 30rpx;
 width: calc(100% - 60rpx);
 margin-left: 30rpx;
 margin-top: 30rpx;
 background: white;
 box-sizing: border-box;
 border-radius: 8rpx;
} 

.listTop {
 display: flex;
 justify-content: space-between;
}

.goodsImg {
 width: 200rpx;
 height: 200rpx;
 margin-right: 20rpx;
}

.infor {
 flex: 1;
 margin-top: 80rpx;
 font-size: 26rpx;
 color: #666;
}

.infor view {
 width: 100%;
 display: flex;
 justify-content: space-between;
}

.infor view:nth-of-type(2) {
 font-size: 24rpx;
}

.name, .choose {
 font-weight: 600;
 display: inline-block;
 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
 width: 320rpx;
}

.price, .number {
 padding: 5rpx 10rpx;
 box-sizing: border-box;
}

.listBottom {
 text-align: right;
}

button::after {
 border: none;
}

.status button {
 display: inline-block;
 background: white;
 border: 1px solid #dedede;
 border-radius: 66rpx;
 font-size: 24rpx;
 margin-left: 20rpx;
 color: #666;
 padding: 0rpx 30rpx;
 box-sizing: border-box;
 height: 50rpx;
 line-height: 45rpx;
 margin-top: 20rpx;
}

wxjs

Page({
 data: {
  recordMain: [

   {
    title: "全部"
   },
   {
    title: "待付款"
   },
   {
    title: "待发货"
   },
   {
    title: "待发货"
   }, {
    title: "已完成"
   },
   {
    title: "已取消"
   },
  ],
  tabContent: [
   {
    goodsImg: '/img/goods.png',
    name: '阿莎玛沙阿莎玛沙发阿莎玛沙发阿莎玛沙发阿莎玛沙莎玛沙发发',
    price: "666",
    choose: '已选:全新,16期',
    number: '32',
    allPrice: '888'
   },
  ],
  currentTab: 0,
  navScrollLeft: 0,
  winWidth: 0,
  winHeight: 0,
 },
 // 事件处理函数
 onLoad: function () {
  var that = this;
  /** 获取系统信息*/
  wx.getSystemInfo({
   success: function (res) {
    that.setData({
     winWidth: res.windowWidth,
     winHeight: res.windowHeight,
    });
   }
  });
 },
 // 滑动事件
 // 点击标题切换当前页时改变样式
 switchNav:function(e) {
  console.log(e.currentTarget.dataset.current)
  var that = this
  var cur = e.currentTarget.dataset.current;
  if (that.data.currentTab == cur) {
   return false;
  } else {
   that.setData({
    currentTab: cur
   })
  }
 },
 // 滚动切换标签样式
 switchTab: function(e) {
  console.log(e)
  var that = this;
  that.setData({
   currentTab: e.detail.current
  }); 

  if (e.detail.current == 0) {
   console.log(0)
  }
  else if (e.detail.current == 1) {
   console.log(11)
  }
  else if (e.detail.current == 2) {
   console.log(2222)
  }
  else if (e.detail.current == 3) {
   console.log(33333)
  }
  else if (e.detail.current == 4) {
   console.log(44444444)
  }
  else if (e.detail.current == 5) {
   console.log(55555)
  }
 }
})

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

(0)

相关推荐

  • 微信小程序滚动Tab实现左右可滑动切换

     微信小程序滚动Tab实现左右可滑动切换 效果: 最终效果如上.问题: 1.tab标题总共8个,所以一屏无法全部显示. 2.tab内容区左右滑动切换时,tab标题随即做标记(active). 3.当active的标题不在当前屏显示时,要使其能显示到当前屏中. 一.wxml结构 tab标题因一排八个,所以使用 scroll-view组件,使其可横向滚动. tab内容可左右滑动切换,使用swiper组件实现 为了偷懒,所以数据都通过wx:for遍历重复出来. 说明: 1.设置data-current

  • 10行代码实现微信小程序滑动tab切换

    本文实例为大家分享了微信小程序滑动tab切换展示的具体代码,供大家参考,具体内容如下 效果预览: js部分: Page({ data: { arr: [1,2,3,4,5,6,7,8], index: 1 }, onLoad: function (options) { this.setData({ childW: this.data.arr.length * 80 }); }, tabOn: function (e) { this.setData({ index: e.currentTarget

  • 微信小程序tab切换可滑动切换导航栏跟随滚动实现代码

    简介 看到今日头条小程序页面可以滑动切换,而且tab导航条也会跟着滚动,点击tab导航,页面滑动,切导航栏也会跟着滚动,就想着要怎么实现这个功能 像商城类商品类目如果做成左右滑动切换类目用户体验应该会好很多,我这里只是一个小demo,没有怎么去考虑数据的问题,主要是想着去实现这么个功能,有可能后期引入数据后会出现问题,欢迎提出互相讨论 解决过程 1.在想要实现这个问题的时候找了不少别人的博客看,主体页面布局方面是比较统一的,tab导航栏使用<scroll-view>标签,内容使用<swi

  • 微信小程序顶部导航栏滑动tab效果

    小程序商品展示需要导航栏的商品分类进行滑动,供大家参考,具体内容如下 效果图: 首先是滑动的效果: <scroll-view scroll-x="true" style="width: 100%;white-space:nowrap;"> </scroll-view> 小程序使用</scroll-view>,横向移动即可 WXML:这里面我将导航栏显示类目定义为5个,每个20%,当超出5个分类,也就是index>4的时候,导

  • 微信小程序自定义可滑动顶部TabBar选项卡实现页面切换功能示例

    本文实例讲述了微信小程序自定义可滑动顶部TabBar选项卡实现页面切换功能.分享给大家供大家参考,具体如下: 顶部滚动选项卡 话不多说,直接上代码 pages/home/home.wxml <scroll-view scroll-x="true" style='width: 100%; white-space:nowrap; '> <!-- tab --> <view class="tab"> <view class=&qu

  • 小程序实现点击tab切换左右滑动

    本文实例为大家分享了小程序实现点击tab切换左右滑动的具体代码,供大家参考,具体内容如下 wxml <scroll-view scroll-x="true" class="navbar-box"> <block wx:for="{{recordMain}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">

  • 微信小程序实现滴滴导航tab切换效果

    本文实例为大家分享了微信小程序实现tab切换效果的具体代码,供大家参考,具体内容如下 效果图如下: (请自动忽视底部tab.....) 简单介绍一下:顶部导航使用 scroll-view 组件 中间的内容部分使用 swiper 组件 实现的逻辑就是: 先这样在这样,这样然后那样.(此处省略200个字). 代码如下,复制可用 wxml <view class="contain"> <!-- 导航栏 --> <scroll-view class="t

  • 微信小程序实现登录注册tab切换效果

    利用三元运算符实现登录注册效果切换,供大家参考,具体内容如下 直接上代码 wxml: <view class='top'> <text class="{{login?'active':''}}" catchtap='login'>登录</text> <text class="{{login?'':'active'}}" catchtap='register'>注册</text> </view>

  • 微信小程序实现点击导航条切换页面

    本文实例为大家分享了微信小程序实现点击导航条切换页面的具体代码,供大家参考,具体内容如下 我录制了个gif如下,黄色部分是不可以滑动的,蓝色部分可以滑动. 代码解说: 首先我在js自定义了navState参数用于判断导航的当前状态, 定义了data-index用于js中动态修改导航的当前状态, nav-switch-style为选择导航条时的样式, 不可滑动视图切换很简单,用wx:if判断状态显示相应页就好了, 滑动页视图切换要用到swiper和 swiper-item, 用bindchang方

  • 微信小程序实现点击按钮修改view标签背景颜色功能示例【附demo源码下载】

    本文实例讲述了微信小程序实现点击按钮修改view标签背景颜色功能.分享给大家供大家参考,具体如下: 1.效果展示 2.操作步骤: ① 数据绑定view样式背景属性值 ② 通过逻辑文件设置该背景属性初始值 ③ 通过点击按钮修改背景属性值 3.关键代码 index.wxml文件: <view style="background:{{viewBg}};color:white;height:100px;">我是view标签</view> <button type=

  • 微信小程序外卖选购页实现切换分类与数量加减功能案例

    本文实例讲述了微信小程序外卖选购页实现切换分类与数量加减功能.分享给大家供大家参考,具体如下: 关于微信小程序外卖界面实现选购切换分类与数量加减功能开发步骤: 效果图 实现以下功能 一. 点击分类项,切换右边的食品,并高亮自身 这个实现比较简单,给一个states数组,用于记录每一项分类的状态,点击设为true,wxml渲染时三目运算判断即可. categoryStates = categoryStates.map(function (item, i) { if (index == i) { i

  • vue tab滚动到一定高度,固定在顶部,点击tab切换不同的内容操作

    template里面: <!-- tab切换star --> <ul class="tab-list" :class="{fixTitle:whether}"> <li @click="curId=0" :class="{'cur':curId===0}">产品特点</li> <li @click="curId=1" :class="{'cur

  • 微信小程序如何根据不同用户切换不同TabBar(简单易懂!)

    目录 现有需求: 1全局.app.json 2.自定义custom-tab-bar 结果展示 总结 现有需求: 小程序用户有三种身份(公众.运维人员.领导),根据不同用户身份显示不同的tabbar 众所周知微信小程序全局文件app.json里面的"tabBar"里面的list只能放置2-5个,要想实现3个tabbar,必须得复用tabbar,三种身份都需要个人中心,剩下的是长列表(两个),表单,图表 刚好是5个,废话少说,上代码 代码有点长,建议仔细看一下 1全局.app.json t

  • 微信小程序实现点击按钮修改字体颜色功能【附demo源码下载】

    本文实例讲述了微信小程序实现点击按钮修改字体颜色功能.分享给大家供大家参考,具体如下: 1.效果展示 2.关键代码 index.wxml文件 <view class="view" style="color:{{color}}">我是view标签</view> <view style="display:flex;"> <block wx:for="{{colorArray}}" wx:k

随机推荐