微信小程序scroll-view仿拼多多横向滑动滚动条
程序员文章站
2022-08-02 12:50:21
本文实例为大家分享了微信小程序横向滑动滚动条的具体代码,供大家参考,具体内容如下
效果图
实现代码
index.wxml
本文实例为大家分享了微信小程序横向滑动滚动条的具体代码,供大家参考,具体内容如下
效果图
实现代码
index.wxml
<scroll-view scroll-x="true" class="scroll-view-x" style="padding-top:10rpx" scroll-with-animation="true" wx:if="{{tlist[currenttab].secondlist}}" scroll-left='0' bindscroll="getleft"> <!--内容区域--> <view> <view class="navigator_second" wx:for="{{tlist[currenttab].secondlist}}" wx:for-item="childitem" bindtap="getproductlist" data-id="{{childitem.id}}"> <image class="navigator_icon" src="{{childitem.icon}}"></image> <text class="navigator_text">{{childitem.name}}</text> </view> </view> <view wx:if="{{tlist[currenttab].thirdlist}}"> <view class="navigator_second" wx:for="{{tlist[currenttab].thirdlist}}" wx:for-item="childitem" data-id="{{childitem.id}}" bindtap="getproductlist"> <image class="navigator_icon" src="{{childitem.icon}}" ></image> <text class="navigator_text">{{childitem.name}}</text> </view> </view> </scroll-view > <!--滚动条部分--> <view wx:if="{{slideshow}}" class="slide"> <view class='slide-bar' > <view class="slide-show" style="width:{{slidewidth}}rpx; margin-left:{{slideleft<=1 ? 0 : slideleft+'rpx'}};"></view> </view> </view>
index.js
//获取应用实例 var app = getapp(); page({ data: { navigate_type:'',//分类类型,是否包含二级分类 slidewidth:'',//滑块宽 slideleft:0 ,//滑块位置 totallength:'',//当前滚动列表总长 slideshow:false, slideratio:'' }, onload: function () { var self = this ; var systeminfo = wx.getsysteminfosync() ; self.setdata({ list: _list, windowheight: app.globaldata.navigate_type == 1 ? systeminfo.windowheight : systeminfo.windowheight - 35, windowwidth: systeminfo.windowwidth, navigate_type: app.globaldata.navigate_type }) //计算比例 self.getratio(); }, //根据分类获取比例 getratio(){ var self = this ; if (!self.data.tlist[self.data.currenttab].secondlist || self.data.tlist[self.data.currenttab].secondlist.length<=5){ this.setdata({ slideshow:false }) }else{ var _totallength = self.data.tlist[self.data.currenttab].secondlist.length * 150; //分类列表总长度 var _ratio = 230 / _totallength * (750 / this.data.windowwidth); //滚动列表长度与滑条长度比例 var _showlength = 750 / _totallength * 230; //当前显示红色滑条的长度(保留两位小数) this.setdata({ slidewidth: _showlength, totallength: _totallength, slideshow: true, slideratio:_ratio }) } } , //slideleft动态变化 getleft(e){ this.setdata({ slideleft: e.detail.scrollleft * this.data.slideratio }) } })
index.wxss
.scroll-view-x{ background-color: #fff; white-space: nowrap; } .navigator_second{ width:150rpx; text-align:center; display:inline-block; height:115rpx; } .navigator_icon{ width:60rpx; height:60rpx; } .navigator_text{ display:block; width:100%; font-size:24rpx } .slide{ height:30rpx; background:#fff; with:100%; padding:14rpx 0 5rpx 0 } .slide .slide-bar{ width:230rpx; margin:0 auto; height:1.5px; background:#eee; } .slide .slide-bar .slide-show{ height:100%; background-color:#ff6969; }
项目地址:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。