uniapp中scroll-view基礎(chǔ)用法示例代碼
前言
在uniapp日常開發(fā)的過程中經(jīng)常會有局部滾動的需求,而scroll-view組件正好可以滿足這一需求。需注意在webview渲染的頁面中,區(qū)域滾動的性能不及頁面滾動。
縱向滾動
將scroll-view組件中的屬性scroll-y設(shè)定為true開啟縱向滾動功能,給scroll-view設(shè)置一個高度,當(dāng)內(nèi)容高度大于scroll-view高度時即可開啟滾動功能(內(nèi)容高度小于scroll-view高度時無法體現(xiàn)滾動功能)
實現(xiàn)代碼:
<template> <view> <scroll-view scroll-y="true" style="height: 700rpx;"> <view v-for="(item,index) in 3" style="height: 500rpx;" :style="{ backgroundColor: colorList[index]}"> {{index}} </view> </scroll-view> </view> </template> <script> export default { data() { return { colorList:["blue","red","yellow"] } }, methods: { } } </script> <style> </style>
效果圖:
橫向滾動
將scroll-view組件中的屬性scroll-x設(shè)定為true開啟橫向滾動功能,給scroll-view設(shè)置一個寬度,當(dāng)內(nèi)容寬度大于scroll-view寬度時即可開啟滾動功能(內(nèi)容寬度小于scroll-view寬度時無法體現(xiàn)滾動功能)
注意:scroll-view本身的display:flex不生效、如果想實現(xiàn)display:flex功能,則可以給scroll-view加上white-space: nowrap,給內(nèi)容容器加上display:inline-block
實現(xiàn)代碼:
<template> <view> <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;"> <view v-for="(item,index) in 3" style="height: 500rpx;width: 100%;display: inline-block;" :style="{ backgroundColor: colorList[index]}"> {{index}} </view> </scroll-view> </view> </template> <script> export default { data() { return { colorList:["blue","red","yellow"] } }, methods: { } } </script> <style> </style>
效果圖:
錨點(diǎn)定位
當(dāng)我們已進(jìn)入頁面就需要滾動到某一個元素的時候,錨點(diǎn)定位就可以很好的幫助我們定位并滾動到指定位置
將scroll-with-animation設(shè)定為true開啟動畫效果、對scroll-into-view進(jìn)行動態(tài)綁定
注意:scroll-into-view綁定的值得是字符串,使用其他類型則會報錯
實現(xiàn)代碼:
<template> <view> <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" scroll-with-animation="true" :scroll-into-view="'scroll'+scrollId"> <view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}" :id="'scroll'+index"> {{index}} </view> </scroll-view> </view> </template> <script> export default { data() { return { colorList:["blue","red","yellow"], scrollId:1 } }, methods: { } } </script> <style> </style>
效果圖:
觸底事件
在滑動的數(shù)據(jù)需要懶加載的時候,我們就需要通過用戶滑動到底部時觸發(fā)懶加載方法,通過綁定scrolltolower方法即可實現(xiàn)縱/橫觸底時觸發(fā)懶加載方法
實現(xiàn)代碼:
<template> <view> <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" @scrolltolower="onReachScollBottom"> <view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}"> {{index}} </view> </scroll-view> </view> </template> <script> export default { data() { return { colorList:["blue","red","yellow"], } }, methods: { onReachScollBottom(){ uni.showToast({ title:"觸發(fā)了觸底事件", duration:1500, icon:"none" }) } } } </script> <style> </style>
效果圖:
下拉刷新事件
scroll-view組件也可以滿足我們下拉刷新的需求、首先通過設(shè)置refresher-enabled為true開啟下拉加載、動態(tài)綁定refresher-triggered對下拉加載的狀態(tài)進(jìn)行控制、綁定refresherrefresh觸發(fā)下拉刷新事件
實現(xiàn)代碼:
<template> <view> <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" refresher-enabled="true" :refresher-triggered="refresh" @refresherrefresh="onRefresh"> <view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}"> {{index}} </view> </scroll-view> </view> </template> <script> export default { data() { return { colorList:["blue","red","yellow"], refresh: false } }, methods: { onRefresh() { this.refresh= true; uni.showToast({ title:"觸發(fā)了下拉刷新", duration:1500, icon:"none" }) // 這里不能直接讓refresh直接為false,否則可能會發(fā)生下拉加載無法復(fù)位的情況 setTimeout(() => { this.refresh = false; }, 500) } } } </script> <style> </style>
效果圖:
總結(jié)
以上就是我整理的scroll-view的基礎(chǔ)用法、想要了解更多的用法可以前往uniapp scroll-view部分進(jìn)行了解
到此這篇關(guān)于uniapp中scroll-view基礎(chǔ)用法的文章就介紹到這了,更多相關(guān)uniapp scroll-view基礎(chǔ)用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layui實現(xiàn)下拉復(fù)選功能的例子(包括數(shù)據(jù)的回顯與上傳)
今天小編大家分享一篇layui實現(xiàn)下拉復(fù)選功能的例子(包括數(shù)據(jù)的回顯與上傳),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09javascript 制作坦克大戰(zhàn)游戲初步 圖片與代碼
javascript 制作坦克大戰(zhàn)游戲初步 圖片與代碼...2007-11-11基于JS實現(xiàn)計算24點(diǎn)算法代碼實例解析
這篇文章主要介紹了基于JS實現(xiàn)計算24點(diǎn)算法代碼實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07javascript innerText和innerHtml應(yīng)用
innerText和innerHtml看字面也應(yīng)該理解的了2010-01-01JS注冊/移除事件處理程序(ExtJS應(yīng)用程序設(shè)計實戰(zhàn))
最常做的事情就是注冊事件處理程序,因為在ExtJS的世界里,幾乎完全由時間組成,下面是處理程序案例,感興趣的朋友可以參考下哈,希望可以幫助到你2013-05-05javascript時間排序算法實現(xiàn)活動秒殺倒計時效果
這篇文章主要介紹了javascript時間排序算法實現(xiàn)活動秒殺倒計時效果,即一個頁面多個倒計時排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03TypeScript?使用?Tuple?Union?聲明函數(shù)重載
這篇文章主要介紹了TypeScript?使用?Tuple?Union?聲明函數(shù)重載,TypeScript 中為函數(shù)添加多個簽名后,依然需要添加相應(yīng)的代碼來判斷并從不同的簽名參數(shù)列表中獲取對應(yīng)的參數(shù),下文就來探索方法和技巧吧2022-04-04