微信小程序fixed定位後scroll-view滾動失效問題

如圖微信小程序中 scroll-view組件外div加display:flxed或者是absolute後, scroll-view組件滾動失效。

解決辦法:

  1. scroll-view寬度爲屏幕寬度,

  2. 子元素view.cont設置overflow:auto;內容超出屏幕會出滾動條,然後便可以拖動了,

  3. 最外層header設置overflow-y: hidden;隱藏滾動條,實現了模擬scroll-view的效果

  4. 點擊view.green時 設置元素cur爲當前點擊view的offsetLeft

<view class="header">
          <scroll-view class="scroll-view_H" scroll-x="{{true}}">
            <view class="cont">
                <view class="list">
                      <view class="green" wx:for="{{tags}}" wx:key="{{index}}" bindtap='fnclick'>{{item}}</view>
                  </view>
                <view class="cur" style='left:{{left}}px'></view>
            </view>
          </scroll-view>
 </view>
.header{
  background: #fff;
  position: fixed;
  z-index: 200;
  top:0;
  left: 0;
  height: 60rpx;
  overflow-y: hidden;
}
.header .cont{
  white-space: nowrap;
  position: relative;
  height: 90rpx;
  overflow:auto;
  width:750rpx;

}
.header .cont .list view{
 width: 120rpx;
 display: inline-block;
 text-align: center;
font-size: 30rpx;
}
.cur{
  position: absolute;
  left: 0;
  bottom: 30rpx;
  width: 70rpx;
  height: 6rpx;
  background: yellow;
  margin-left: 25rpx;
  transition: .5s all ease;
}

Page({
     data:{
      tags:[
          "smile",
          "smile", 
          "smile", 
          "smile", 
          "smile",
          "smile",
          "smile", 
          "smile", 
          "smile", 
          "smile", 
          "smile"
      ],
      left:0,
    },
    onLoad(){
    },
    // 導航條鼠標跟隨
    fnclick(ev){
      this.setData({
        left:ev.target.offsetLeft
      })

    },

})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章