微信小程序——-会滑动的顶部tab选项卡

微信小程序——-会滑动的顶部tab选项卡

主要是在scroll-view设置scroll-x=“true”,然后在设置scroll-left(横向滚动条位置)为一定的数值,就可以了

不废话直接进入主题

test.wxml

<scroll-view class="tab-scoller " scroll-x="true"  scroll-left="{{scrollLength}}">
      <block wx:for="{{listTab}}" wx:key="code">
            <view class="tab-view" data-index="{{index}}" id="{{item.code}}" bindtap="reflashData">
                  <text class="tab-text active" wx:if="{{index == curIndex}}">{{item.text}}</text>
                  <text class="tab-text" wx:else>{{item.text}}</text>

            </view>
      </block>
</scroll-view> 
<view style="width:100%;heigth:100%;">
      <text style="position:absolute;top:50%;left:40%; font-size:1.5rem">{{curText}}</text>
</view>

test.wxss

.tab-scoller {
  background: linear-gradient(to bottom, #2262fc, rgba(57, 134, 222, 0.84));
  height: 3rem;
  white-space: nowrap;
  display: flex;
}
/*取消移动条*/
::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}

.active {
  color: #000 !important;
  background-color: #fff;
}

.tab-view {
  text-align: center;
  color: #fff;
  font-size: 1rem;
  height: 1.2rem;
  width: 4rem;
  margin-top: 1rem;
  border-right: 1px solid #fff;
  display: inline-block;
  line-height: 1.2rem;
}

.tab-text {
  display: block;
  bottom: 0.4rem;
  position: relative;
  height: 1.5rem;
  margin: 0 5%;
  border-radius: 0.5rem;
  padding: 0.3rem 0.2rem 0;
  color: #fff;
}

test.js

// pages/more/test.js
Page({
  data: {
    listTab:[
      {"code":"01","text":"tab1"},
      {"code":"02","text":"tab2"},
      {"code":"03","text":"tab3"},
      {"code":"04","text":"tab4"},
      {"code":"05","text":"tab5"},
      {"code":"06","text":"tab6"},
      {"code":"07","text":"tab7"}
    ],
    curIndex:0,
    curText:null,
    scrollLength: 0
  },
  onLoad: function () {
    console.log('onLoad') 
    this.initData(0)
  },
  //初始化数据
  initData:function(index){
    var that = this
    this.setData({
        curIndex:index,
        curText:that.data.listTab[index].text,
      })
  },
  //tab点击事件,刷新数据
  reflashData:function(event){
    var that = this

      var index = event.currentTarget.dataset.index
      //移动滚动条,//200和35是我估算的
      if(index > this.data.curIndex ){
        if(that.data.scrollLength < 200){
          this.setData({
            scrollLength: that.data.scrollLength + 35 * (index - that.data.curIndex) 
          })
        }
      }else{
        if(that.data.scrollLength > 0){
          this.setData({
            scrollLength: that.data.scrollLength - 35 * (that.data.curIndex - index)
          })
        }
      }
      //移动view位置,改变选中颜色
     this.initData(index)


  },
})

这里写图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
微信小程序中,scroll-view 组件默认阻止页面上其他元素的滑动事件,这可以通过设置 `catchtouchmove` 属性来解决。 具体做法如下: 1. 在 scroll-view 组件上添加 `catchtouchmove="true"` 属性,如下所示: ```html <scroll-view catchtouchmove="true"> <!-- scroll-view 内容 --> </scroll-view> ``` 2. 在页面的 `onLoad` 或 `onReady` 生命周期中,获取 scroll-view 组件的高度和宽度,然后在页面上添加一个与 scroll-view 同样大小的透明的 view,并将其 zIndex 设置为比 scroll-view 更高的数值,这样就可以让页面上的其他元素在透明的 view 上进行滑动了。代码如下: ```javascript onLoad: function () { var that = this; wx.createSelectorQuery().select('#scrollview').boundingClientRect(function (rect) { that.setData({ scrollHeight: rect.height }); }).exec(); wx.getSystemInfo({ success: function (res) { that.setData({ windowHeight: res.windowHeight }); } }); }, ``` ```html <!-- 添加一个与 scroll-view 大小相同的透明 view --> <view class="transparent-view" style="height: {{windowHeight - scrollHeight}}px; z-index: 1;"></view> <scroll-view id="scrollview" catchtouchmove="true"> <!-- scroll-view 内容 --> </scroll-view> ``` 3. 在页面的 CSS 中,让透明的 view 不显示出来,代码如下: ```css .transparent-view { background-color: transparent; } ``` 通过以上步骤,就可以解决微信小程序中 scroll-view 组件滑动穿透的问题了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值