微信小程序 教程之wxapp視圖容器 scroll-view

scroll-view

可滾動視圖區域。

屬性名 類型 默認值 說明
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}

使用豎向滾動時,需要給<scroll-view/>一個固定高度,通過 WXSS 設置 height。

示例代碼:

<view class="section">
 <view class="section__title">vertical scroll</view>
 <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>
</view>
<view class="section section_gap">
 <view class="section__title">horizontal scroll</view>
 <scroll-view class="scroll-view_H" scroll-x="true" style="width: 100%">
 <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>
</view>

var order = ['red', 'yellow', 'blue', 'green', 'red']
Page({
 data: {
 toView: 'red',
 scrollTop: 100
 },
 upper: function(e) {
 console.log(e)
 },
 lower: function(e) {
 console.log(e)
 },
 scroll: function(e) {
 console.log(e)
 },
 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
 })
 }
})
scroll-view


轉自:http://www.jb51.net/article/95157.htm

發佈了30 篇原創文章 · 獲贊 42 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章