如圖微信小程序中 scroll-view組件外div加display:flxed或者是absolute後, scroll-view組件滾動失效。
解決辦法:
-
scroll-view寬度爲屏幕寬度,
-
子元素view.cont設置overflow:auto;內容超出屏幕會出滾動條,然後便可以拖動了,
-
最外層header設置overflow-y: hidden;隱藏滾動條,實現了模擬scroll-view的效果
-
點擊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
})
},
})