小程序自定義 scroll-view 滾動條
話不多說, 直接上效果圖
效果圖
wxml代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< scroll-view scroll-x class = "scroll-view" bindscroll = "bindScroll" > < block wx:for = "{{arr}}" wx:key = "index" > < view class = "scroll-item" >scroll-view{{index}}</ view > </ block > </ scroll-view > <!-- 滾動條 --> < view class = "slide" > < view class = 'slide-bar' > < view class = "slide-action" style = "width:{{slideWidth}}rpx; margin-left:{{slideLeft<=1 ? 0 : slideLeft+'rpx'}};" > </ view > </ view > </ view > |
wxss代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
page{ height : 100 vh; background : rgb ( 111 , 80 , 65 ) } .scroll-view{ display : flex; width : 100% ; white-space : nowrap ; padding-top : 20 rpx; } .scroll-item:nth-child( 1 ){ margin-left : 40 rpx; } .scroll-item { display : inline- block ; width : 550 rpx; height : 463 rpx; background : rgba( 199 , 180 , 165 ); border-radius: 20 rpx; margin-right : 30 rpx; color : #fff ; } .slide{ background : rgb ( 111 , 80 , 65 ); width : 100% ; padding-top : 20 rpx; } .slide .slide-bar{ width : 180 rpx; margin : 0 auto ; height : 4 rpx; background : rgba( 255 , 255 , 255 ,. 2 ); } .slide .slide-bar .slide-action{ height : 100% ; background : #fff ; } |
js代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/** * 頁面的初始數據 */ data: { arr: 10, slideWidth: '' , slideLeft: '' }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { // 計算比例 this .calcRatio(); }, /** * 計算比例 */ calcRatio() { var windowWidth = wx.getSystemInfoSync().windowWidth; // 計算列表總長度 var totalLength = ( this .data.arr * 580) + 40; // 計算滑塊的比例 var slideRatio = 180 / totalLength * (750 / windowWidth); /** * 屏幕總長度 / 列表總長度 = 滑塊占滾動條長度的比例 * 滑塊占滾動條長度的比例 * 滾動列表的長度 = 滑塊的寬度 */ var sliderWidth = 750 / totalLength * 180; this .setData({ slideWidth: sliderWidth, totalLength: totalLength, slideRatio: slideRatio }) }, /** * 監聽滾動 */ bindScroll(e) { this .setData({ slideLeft: e.detail.scrollLeft * this .data.slideRatio }) }, |
附:scroll-view可滾動視圖區域
總結
到此這篇關于微信小程序自定義scroll-view的文章就介紹到這了,更多相關微信小程序自定義scroll-view內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_44227577/article/details/105906827