前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序tab切换加联动

微信小程序tab切换加联动

作者头像
天天_哥
发布2018-09-29 14:54:00
2.4K1
发布2018-09-29 14:54:00
举报
文章被收录于专栏:天天天天

微信小程序tab切换加联动,就像有些app中的tab切换

1.实现思路

时时不能忘记在微信小程序中是数据驱动,要实现这种效果还是需要从数据入手

2.看scroll和swiper组件文档
scroll

属性名

类型

说明

scroll-left

Number

设置横向滚动条位置

swiper

属性名

类型

说明

current

Number

当前所在滑块的 index

代码语言:javascript
复制
<!--index.wxml-->
<scroll-view scroll-x class='scroll-container' scroll-left="{{scroll_left}}">
    <view wx:for="{{item}}" wx:key="index"  class="toptabbox">
      <text bindtap='tabchange' class="toptab {{currentindex==index?'on':''}}" data-current="{{index}}">{{item.text}}</text>
    </view>
</scroll-view>
<swiper bindchange='changeview' current="{{currentindex}}" class='swiper-container'>
  <swiper-item wx:for="{{item}}" wx:key="index" classs="swiper-item">
    <view class='swiper-item-con' style="background:{{item.color}};">{{item.text}}</view>
  </swiper-item>
</swiper>
代码语言:javascript
复制
/*index.js*/
Page({
  data: {
    currentindex: 0,
    scroll_left: 0,
    item: [
      { "cid": 'c001', "text": "首页", "color": "#d5bb96" },
      { "cid": 'c002', "text": "推荐", "color": "#a696d5" },
      { "cid": 'c003', "text": "视频", "color": "#ce96d5" },
      { "cid": 'c004', "text": "体育", "color": "#a396d5" },
      { "cid": 'c005', "text": "新闻", "color": "#d5bb96" },
      { "cid": 'c006', "text": "科技", "color": "#d596c5" },
      { "cid": 'c007', "text": "军事", "color": "#d5ac96" },
      { "cid": 'c008', "text": "百科", "color": "#d5bb96" },
      { "cid": 'c009', "text": "小知识", "color": "#96b1d5" },
      { "cid": 'c010', "text": "生活", "color": "#96d5a6" },
      { "cid": 'c011', "text": "问答", "color": "#d5bb96" }
    ],
  },

  changeview: function (e) {
    var current_tap = e.detail.current;
    var s = 0;
    /*此处选择7为临界点是点前面的7不需要scroll滑动*/
    if (current_tap>=7) {
      s = parseInt(current_tap-7) * 60;
    }
    this.setData({
      currentindex: e.detail.current,
      scroll_left: s
    });
    
  },
 
  tabchange: function (e) {
    var current_swiper = e.currentTarget.dataset.current;
    var s = 0;
    if (current_swiper >= 7) {
      console.log(current_swiper)
      s = parseInt(current_swiper-7) * 60;
    }
    this.setData({
      currentindex: current_swiper,
      scroll_left: s
    });

  }
})
代码语言:javascript
复制
/*index.wxss*/
page{
  width:100%;
  height:100%;
  padding-top:70rpx;
  box-sizing: border-box;
}

.on{
  color:#ff0000;
  border-bottom:1px solid #ff0000;
}

.scroll-container{
  height:70rpx;
  width:750rpx;
  white-space: nowrap;/*必须设置的css属性*/
  position: fixed;
  top:0;
  left:0;
}

.toptabbox{
  height:100rpx;
  margin:0 12rpx 10rpx;
  display:inline-block;/*确实很重要用float好像不行*/
}
.toptab{
  width:100%;
  height:60rpx;
  text-align:center;
  font-size:30rpx;
  line-height:60rpx;
}
.swiper-container{
  color:#fff;
  font-size:28rpx;
  height:100%;
}
.swiper-item{
  width:750rpx;
  height:100%;
  color:#fff;
  
}
.swiper-item-con{
  text-align: center;
  height:100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

注:数据绑定,特别是怎么动态绑定class,控制选中或者未选中状态,其中,绑定class或者style都和vue中的格式不太一样

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.实现思路
  • 2.看scroll和swiper组件文档
    • scroll
      • swiper
      相关产品与服务
      云开发 CloudBase
      云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档