uni-app 組件之scroll-view


原網頁地址:https://uniapp.dcloud.io/component/scroll-view

說明

scroll-view,可滾動視圖區域。用於區域滾動。

需注意在webview渲染頁面中,區域滾動性能不及頁面滾動。

屬性說明

屬性名 類型 默認值 說明 平台差異說明
scroll-x Boolean false 允許橫向滾動  
scroll-y Boolean false 允許縱向滾動  
upper-threshold Number 50 距頂部/左邊多遠時(單位px),觸發scrolltopper事件  
lower-threshold Number 50 距離底部/右邊多遠時(單位px),觸發scrolltolwer事件  
scroll-top Number   設置豎直滾動條位置  
scroll-top Number   設置橫向滾動條位置  
scroll-into-view String   值應為某子元素id(id不能以數字開頭)。設置哪個方向可滾動,則在哪個方向滾動到該元素  
scroll-with-animation Boolean false 在設置滾動條位置時使用動畫過渡  
enable-back-to-top Boolean false iOS點擊頂部狀態欄、安卓雙擊標題欄時,滾動條返回頂部,只支持豎向 app-nvue,微信小程序
show-scrollbar Boolean false 控制是否出現滾動條 App-nvue 2.1.5+
refresher-enabled Boolean false 開啟自定義下拉刷新 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
refresher-threshold Number 45 設置自定義下拉刷新閾值 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
refresher-default-style String "black" 設置自定義下拉刷新默認樣式,支持設置 black,white,none,none 表示不使用默認樣式 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
refresher-background String "#FFF" 設置自定義下拉刷新區域背景顏色 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
refresher-triggered Boolean false 設置當前下拉刷新狀態,true 表示下拉刷新已經被觸發,false 表示下拉刷新未被觸發 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
enable-flex Boolean false 啟用 flexbox 布局。開啟后,當前節點聲明了 display: flex 就會成為 flex container,並作用於其孩子節點。 微信小程序 2.7.3
scroll-anchoring Boolean false 開啟 scroll anchoring 特性,即控制滾動位置不隨內容變化而抖動,僅在 iOS 下生效,安卓下可參考 CSS overflow-anchor 屬性。 微信小程序 2.8.2
@scrolltoupper EventHandle   滾動到頂部/左邊,會觸發 scrolltoupper 事件  
@scrolltolower EventHandle   滾動到底部/右邊,會觸發 scrolltolower 事件      
@scroll EventHandle   滾動時觸發,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}  
@refresherpulling EventHandle   自定義下拉刷新控件被下拉 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
@refresherrefresh EventHandle   自定義下拉刷新被觸發 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
@refresherrestore EventHandle   自定義下拉刷新被復位 app-vue 2.5.12+,微信小程序基礎庫2.10.1+
@refresherabort EventHandle   自定義下拉刷新被中止 app-vue 2.5.12+,微信小程序基礎庫2.10.1+

使用數值滾動時,需要給<scroll-view>一個固定高度,通過css設置heigth;使用橫向滾動時,需要給<scroll-view>添加white-space:nowrap;樣式。

示例 查看演示

以下示例代碼來自hello uni-app項目,推薦使用HBuilderX。

 1 <!-- 本示例未包含完整css,獲取外鏈css請參考上文,在hello uni-app項目中查看 -->
 2 <template>
 3     <view>
 4         <view class="uni-padding-wrap uni-common-mt">
 5             <view class="uni-title uni-common-mt">
 6                 Vertical Scroll
 7                 <text>\n縱向滾動</text>
 8             </view>
 9             <view>
10                 <scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
11                 @scroll="scroll">
12                     <view id="demo1" class="scroll-view-item uni-bg-red">A</view>
13                     <view id="demo2" class="scroll-view-item uni-bg-green">B</view>
14                     <view id="demo3" class="scroll-view-item uni-bg-blue">C</view>
15                 </scroll-view>
16             </view>
17             <view @tap="goTop" class="uni-link uni-center uni-common-mt">
18                 點擊這里返回頂部
19             </view>
20             <view class="uni-title uni-common-mt">
21                 Horizontal Scroll
22                 <text>\n橫向滾動</text>
23             </view>
24             <view>
25                 <scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="120">
26                     <view id="demo1" class="scroll-view-item_H uni-bg-red">A</view>
27                     <view id="demo2" class="scroll-view-item_H uni-bg-green">B</view>
28                     <view id="demo3" class="scroll-view-item_H uni-bg-blue">C</view>
29                 </scroll-view>
30             </view>
31         </view>
32     </view>
33 </template>
示例
 1 export default {
 2     data() {
 3         return {
 4             scrollTop: 0,
 5             old: {
 6                 scrollTop: 0
 7             }
 8         }
 9     },
10     methods: {
11         upper: function(e) {
12             console.log(e)
13         },
14         lower: function(e) {
15             console.log(e)
16         },
17         scroll: function(e) {
18             console.log(e)
19             this.old.scrollTop = e.detail.scrollTop
20         },
21         goTop: function(e) {
22             this.scrollTop = this.old.scrollTop
23             this.$nextTick(function() {
24                 this.scrollTop = 0
25             });
26             uni.showToast({
27                 icon:"none",
28                 title:"縱向滾動 scrollTop 值已被修改為 0"
29             })
30         }
31     }
View Code

自定義下拉刷新

注意自定義下拉刷新的性能不及pages.json中配置的原生下拉刷新。

1 <template>
2     <view>
3         <scroll-view style="height: 300px;" scroll-y="true" refresher-enabled="true" :refresher-triggered="triggered"
4             :refresher-threshold="100" refresher-background="lightgreen" @refresherpulling="onPulling"
5             @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort"></scroll-view>
6     </view>
7 </template>
View Code
 1 <script>
 2     export default {
 3         data() {
 4             return {
 5                 triggered: false
 6             }
 7         },
 8         onLoad() {
 9             this._freshing = false;
10             setTimeout(() => {
11                 this.triggered = true;
12             }, 1000)
13         },
14         methods: {
15             onPulling(e) {
16                 console.log("onpulling", e);
17             },
18             onRefresh() {
19                 if (this._freshing) return;
20                 this._freshing = true;
21                 setTimeout(() => {
22                     this.triggered = false;
23                     this._freshing = false;
24                 }, 3000)
25             },
26             onRestore() {
27                 this.triggered = 'restore'; // 需要重置
28                 console.log("onRestore");
29             },
30             onAbort() {
31                 console.log("onAbort");
32             }
33         }
34     }
35 </script>
View Code

Tips

  • APP-vue和小程序中,請勿在 scroll-view 中使用 map、video 等原生組件。小程序中 scroll-view 中也不要使用 canvas、textarea 原生組件。更新:微信基礎庫2.4.4起支持了原生組件在 scroll-view、swiper、movable-view 中的使用。app-nvue無此限制。
  • scroll-view 不適合放長列表,有性能問題。長列表滾動和下拉刷新,應該使用原生導航欄搭配頁面級的滾動和下拉刷新實現。包括在app-nvue頁面,長列表應該使用list而不是scroll-view。
  • scroll-into-view 的優先級高於 scroll-top。
  • scroll-view是區域滾動,不會觸發頁面滾動,無法觸發pages.json配置的下拉刷新、頁面觸底onReachBottomDistance、titleNView的transparent透明漸變。
  • 若要使用下拉刷新,建議使用頁面的滾動,而不是 scroll-view 。插件市場有前端模擬的基於scroll-view的下拉刷新,但性能不佳。如必需使用前端下拉刷新,推薦使用基於wxs的下拉刷新,性能會比基於js監聽方式更高。
  • 如果遇到scroll-top、scroll-left、refresher-triggered屬性設置不生效的問題參考:組件屬性設置不生效解決辦法
  • scroll-view的滾動條設置,可通過css的-webkit-scrollbar自定義,包括隱藏滾動條。(app-nvue無此css)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM