微信小程序scroll-view組件官方文檔 傳送門
提前准備:使用<view>組件制作五條撐滿的橫向區域

<!--index.wxml--> Cynical丶Gary <view > <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view class='a size'>a</view> <view class='b size'>b</view> <view class='c size'>c</view> <view class='d size'>d</view> <view class='e size'>e</view> </view>

.container{ display: flex; } .size{ width: 100%; height: 150rpx; } .a{ background: red; order:1; flex:10; } .b{ background: yellow; order:2; flex:2; } .c{ background: blue; order:3; flex:1; } .d{ background: green; order:4; flex:1; } .e{ background: orange; order:5; flex:1; }
Learn
一、scroll-y屬性
二、scroll-x屬性
一、scroll-y屬性
scroll-y:允許縱向滾動【默認值false】
使用<scroll-view>組件可以設置小程序中<view>組件縱向滾動,使用<scroll-view>組件時注意必須設置高度的樣式如:style='height:300rpx'

<!--index.wxml--> Cynical丶Gary <scroll-view scroll-y="true" style='height:300rpx'> <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view class='a size'>a</view> <view class='b size'>b</view> <view class='c size'>c</view> <view class='d size'>d</view> <view class='e size'>e</view> </scroll-view>

.container{ display: flex; } .size{ width: 100%; height: 150rpx; } .a{ background: red; order:1; flex:10; } .b{ background: yellow; order:2; flex:2; } .c{ background: blue; order:3; flex:1; } .d{ background: green; order:4; flex:1; } .e{ background: orange; order:5; flex:1; }
upper-threshold:距頂部/左邊多遠時(單位px,2.4.0起支持rpx),觸發 scrolltoupper 事件【默認值50】
lower-threshold:距底部/右邊多遠時(單位px,2.4.0起支持rpx),觸發 scrolltolower 事件【默認值50】
bindscrolltoupper:滾動到頂部/左邊,會觸發 scrolltoupper 事件
bindscrolltolower:滾動到底部/右邊,會觸發 scrolltolower 事件
給<scroller-view>組件添加滾動觸發事件
<scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" >
並在index.js中添加scrolltoupper函數事件和scrolltolower函數事件
scrolltoupper:function(){ console.log("滾動到頂部"); }, scrolltolower:function(){ console.log("滾動到底部"); }

<!--index.wxml--> Cynical丶Gary <scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" > <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view class='a size'>a</view> <view class='b size'>b</view> <view class='c size'>c</view> <view class='d size'>d</view> <view class='e size'>e</view> </scroll-view>

.container{ display: flex; } .size{ width: 100%; height: 150rpx; } .a{ background: red; order:1; flex:10; } .b{ background: yellow; order:2; flex:2; } .c{ background: blue; order:3; flex:1; } .d{ background: green; order:4; flex:1; } .e{ background: orange; order:5; flex:1; }

Page({ data:{ }, scrolltoupper:function(){ console.log("滾動到頂部"); }, scrolltolower:function(){ console.log("滾動到底部"); } })
當然我們也可以給<scroll-view>添加兩個額外的屬性upper-threshold和lower-threshold
upper-threshold:距頂部/左邊多遠時(單位px,2.4.0起支持rpx),觸發 scrolltoupper 事件【默認值50】
lower-threshold:距底部/右邊多遠時(單位px,2.4.0起支持rpx),觸發 scrolltolower 事件【默認值50】
使用方法
<scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0">
scroll-top:設置豎向滾動條位置(單位px,2.4.0起支持rpx)【無默認值】
可以看到,當我們設置scroll-top="100"時,滾動條默認出現在距離頂部100px位置的地方,當我們設置scroll-top="150"時,滾動條默認出現在距離頂部150px位置的地方

<!--index.wxml--> Cynical丶Gary <scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0" scroll-top="150"> <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view class='a size'>a</view> <view class='b size'>b</view> <view class='c size'>c</view> <view class='d size'>d</view> <view class='e size'>e</view> </scroll-view>

.container{
display: flex;
}
.size{
width: 100%;
height: 150rpx;
}
.a{
background: red;
order:1;
flex:10;
}
.b{
background: yellow;
order:2;
flex:2;
}
.c{
background: blue;
order:3;
flex:1;
}
.d{
background: green;
order:4;
flex:1;
}
.e{
background: orange;
order:5;
flex:1;
}

Page({ data:{ }, scrolltoupper:function(){ console.log("滾動到頂部"); }, scrolltolower:function(){ console.log("滾動到底部"); } })
enable-back-to-top:iOS點擊頂部狀態欄、安卓雙擊標題欄時,滾動條返回頂部,只支持豎向【默認值false】
注意:微信小程序開發工具中默認在web環境中進行開發,而此屬性需要在手機上運行~
bindscroll:滾動時觸發,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}
在index.js中添加滾動時觸發的函數bindscroll()
bindscroll:function(){ console.log("滾動中~"); }
在index.wxml中進行事件綁定bindscroll="bindscroll"
<scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0" scroll-top="150" enable-back-to-top="true" bindscroll="bindscroll">

<!--index.wxml--> Cynical丶Gary <scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0" scroll-top="150" enable-back-to-top="true" bindscroll="bindscroll"> <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view class='a size'>a</view> <view class='b size'>b</view> <view class='c size'>c</view> <view class='d size'>d</view> <view class='e size'>e</view> </scroll-view>

Page({ data:{ }, scrolltoupper:function(){ console.log("滾動到頂部"); }, scrolltolower:function(){ console.log("滾動到底部"); }, bindscroll:function(){ console.log("滾動中~"); } })
scroll-into-view:值應為某子元素id(id不能以數字開頭)。設置哪個方向可滾動,則在哪個方向滾動到該元素
index.wxml中給五個<view>組件添加id元素
<view id="a" class='a size'>a</view> <view id="b" class='b size'>b</view> <view id="c" class='c size'>c</view> <view id="d" class='d size'>d</view> <view id="e" class='e size'>e</view>
通過scroll-into-view給<scroll-view>組件添加屬性
<scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0" scroll-into-view="e" enable-back-to-top="true" bindscroll="bindscroll">

<!--index.wxml--> Cynical丶Gary <!-- scroll-top="150" --> <scroll-view scroll-y="true" style='height:300rpx;' bindscrolltoupper="scrolltoupper" bindscrolltolower="scrolltolower" upper-threshold="0" lower-threshold="0" scroll-into-view="e" enable-back-to-top="true" bindscroll="bindscroll"> <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view id="a" class='a size'>a</view> <view id="b" class='b size'>b</view> <view id="c" class='c size'>c</view> <view id="d" class='d size'>d</view> <view id="e" class='e size'>e</view> </scroll-view>

Page({
data:{
},
scrolltoupper:function(){
console.log("滾動到頂部");
},
scrolltolower:function(){
console.log("滾動到底部");
},
bindscroll:function(){
console.log("滾動中~");
}
})
二、scroll-x屬性
scroll-x:允許橫向滾動【默認值為false】
scroll-left:設置橫向滾動條位置(單位px,2.4.0起支持rpx)

<!--index.wxml--> Cynical丶Gary <scroll-view class='container' scroll-x="true" style='height:300rpx;' scroll-left="200"> <!-- 手指按下后1s樣式由a改變為d,手指松開后2s樣式由d改變回a --> <view id="a" class='a size'>a</view> <view id="b" class='b size'>b</view> <view id="c" class='c size'>c</view> <view id="d" class='d size'>d</view> <view id="e" class='e size'>e</view> </scroll-view>

.container{ display: flex; white-space: nowrap; } .size{ width: 250rpx; height: 150rpx; display:inline-block; } .a{ background: red; order:1; flex:10; } .b{ background: yellow; order:2; flex:2; } .c{ background: blue; order:3; flex:1; } .d{ background: green; order:4; flex:1; } .e{ background: orange; order:5; flex:1; }

Page({ data:{ }, scrolltoupper:function(){ console.log("滾動到頂部"); }, scrolltolower:function(){ console.log("滾動到底部"); }, bindscroll:function(){ console.log("滾動中~"); } })