本文編輯: 明日創意科技有限公司瀏覽 14373
版權所有,嚴禁轉載
可滾動視圖區域。
Tip:
效果圖:
wxml
<scroll-view scroll-y="true" style="height: 200px;" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}" scroll-top="{{scrollTop}}">
<view id="green" class="scroll-view-item bc_green"></view>
<view id="red" class="scroll-view-item bc_red"></view>
<view id="yellow" class="scroll-view-item bc_yellow"></view>
<view id="blue" class="scroll-view-item bc_blue"></view>
</scroll-view>
<view class="btn-area">
<button size="mini" bindtap="tap">click me to scroll into view </button>
<button size="mini" bindtap="tapMove">click me to scroll</button>
</view>
js
var order = ['red', 'yellow', 'blue', 'green', 'red']
Page({
data: {
toView: 'green',
scrollTop: 100,
scrollLeft: 0
},
//滾動條滾到頂部的時候觸發
upper: function(e) {
console.log(e)
},
//滾動條滾到底部的時候觸發
lower: function(e) {
console.log(e)
},
//滾動條滾動后觸發
scroll: function(e) {
console.log(e)
},
//點擊按鈕切換到下一個view
tap: function(e) {
for (var i = 0; i < order.length; ++i) {
if (order[i] === this.data.toView) {
this.setData({
toView: order[i + 1]
})
break
}
}
},
//通過設置滾動條位置實現畫面滾動
tapMove: function(e) {
this.setData({
scrollTop: this.data.scrollTop + 10
})
}
})
css
.scroll-view_H{
white-space: nowrap;
}
.scroll-view-item{
height: 200px;
}
.scroll-view-item_H{
display: inline-block;
width: 100%;
height: 200px;
}
Tip:
效果圖:
wxml
<scroll-view class="scroll-view_H" scroll-x="true" style="width: 100%" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-left="{{scrollLeft}}">
<view id="green" class="scroll-view-item_H bc_green"></view>
<view id="red" class="scroll-view-item_H bc_red"></view>
<view id="yellow" class="scroll-view-item_H bc_yellow"></view>
<view id="blue" class="scroll-view-item_H bc_blue"></view>
</scroll-view>
js
var order = ['red', 'yellow', 'blue', 'green', 'red']
Page({
data: {
toView: 'green',
scrollTop: 100,
scrollLeft: 0
},
//滾動條滾到頂部的時候觸發
upper: function(e) {
console.log(e)
},
//滾動條滾到底部的時候觸發
lower: function(e) {
console.log(e)
},
//滾動條滾動后觸發
scroll: function(e) {
console.log(e)
},
//點擊按鈕切換到下一個view
tap: function(e) {
for (var i = 0; i < order.length; ++i) {
if (order[i] === this.data.toView) {
this.setData({
toView: order[i + 1]
})
break
}
}
},
//通過設置滾動條位置實現畫面滾動
tapMove: function(e) {
this.setData({
scrollLeft: this.data.scrollLeft + 10
})
}
})
wxss
.scroll-view_H{
white-space: nowrap;
}
.scroll-view-item{
height: 200px;
}
.scroll-view-item_H{
display: inline-block;
width: 100%;
height: 200px;
}
屬性 | 類型 | 默認值 | 描述 |
---|---|---|---|
scroll-x | Boolean | false | 允許橫向滾動 |
scroll-y | Boolean | false | 允許縱向滾動 |
upper-threshold | Number | 50 | 距頂部/左邊多遠時(單位px),觸發 scrolltoupper 事件 |
lower-threshold | Number | 50 | 距底部/右邊多遠時(單位px),觸發 scrolltolower 事件 |
scroll-top | Number | 設置豎向滾動條位置 | |
scroll-left | Number | 設置橫向滾動條位置 | |
scroll-into-view | String | 值應為某子元素id,則滾動到該元素,元素頂部對齊滾動區域頂部 | |
bindscrolltoupper | EventHandle | 滾動到頂部/左邊,會觸發 scrolltoupper 事件 | |
bindscrolltolower | EventHandle | 滾動到底部/右邊,會觸發 scrolltolower 事件 | |
bindscroll | EventHandle | 滾動時觸發,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY} |