uniapp中scroll-view基礎(chǔ)用法示例代碼
前言
在uniapp日常開(kāi)發(fā)的過(guò)程中經(jīng)常會(huì)有局部滾動(dòng)的需求,而scroll-view組件正好可以滿足這一需求。需注意在webview渲染的頁(yè)面中,區(qū)域滾動(dòng)的性能不及頁(yè)面滾動(dòng)。
縱向滾動(dòng)
將scroll-view組件中的屬性scroll-y設(shè)定為true開(kāi)啟縱向滾動(dòng)功能,給scroll-view設(shè)置一個(gè)高度,當(dāng)內(nèi)容高度大于scroll-view高度時(shí)即可開(kāi)啟滾動(dòng)功能(內(nèi)容高度小于scroll-view高度時(shí)無(wú)法體現(xiàn)滾動(dòng)功能)
實(shí)現(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>
效果圖:
橫向滾動(dòng)
將scroll-view組件中的屬性scroll-x設(shè)定為true開(kāi)啟橫向滾動(dòng)功能,給scroll-view設(shè)置一個(gè)寬度,當(dāng)內(nèi)容寬度大于scroll-view寬度時(shí)即可開(kāi)啟滾動(dòng)功能(內(nèi)容寬度小于scroll-view寬度時(shí)無(wú)法體現(xiàn)滾動(dòng)功能)
注意:scroll-view本身的display:flex不生效、如果想實(shí)現(xiàn)display:flex功能,則可以給scroll-view加上white-space: nowrap,給內(nèi)容容器加上display:inline-block
實(shí)現(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)入頁(yè)面就需要滾動(dòng)到某一個(gè)元素的時(shí)候,錨點(diǎn)定位就可以很好的幫助我們定位并滾動(dòng)到指定位置
將scroll-with-animation設(shè)定為true開(kāi)啟動(dòng)畫(huà)效果、對(duì)scroll-into-view進(jìn)行動(dòng)態(tài)綁定
注意:scroll-into-view綁定的值得是字符串,使用其他類(lèi)型則會(huì)報(bào)錯(cuò)
實(shí)現(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>
效果圖:
觸底事件
在滑動(dòng)的數(shù)據(jù)需要懶加載的時(shí)候,我們就需要通過(guò)用戶滑動(dòng)到底部時(shí)觸發(fā)懶加載方法,通過(guò)綁定scrolltolower方法即可實(shí)現(xiàn)縱/橫觸底時(shí)觸發(fā)懶加載方法
實(shí)現(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組件也可以滿足我們下拉刷新的需求、首先通過(guò)設(shè)置refresher-enabled為true開(kāi)啟下拉加載、動(dòng)態(tài)綁定refresher-triggered對(duì)下拉加載的狀態(tài)進(jìn)行控制、綁定refresherrefresh觸發(fā)下拉刷新事件
實(shí)現(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,否則可能會(huì)發(fā)生下拉加載無(wú)法復(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layui實(shí)現(xiàn)下拉復(fù)選功能的例子(包括數(shù)據(jù)的回顯與上傳)
今天小編大家分享一篇layui實(shí)現(xiàn)下拉復(fù)選功能的例子(包括數(shù)據(jù)的回顯與上傳),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09javascript 制作坦克大戰(zhàn)游戲初步 圖片與代碼
javascript 制作坦克大戰(zhàn)游戲初步 圖片與代碼...2007-11-11基于JS實(shí)現(xiàn)計(jì)算24點(diǎn)算法代碼實(shí)例解析
這篇文章主要介紹了基于JS實(shí)現(xiàn)計(jì)算24點(diǎn)算法代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Javascript隨機(jī)標(biāo)簽云代碼實(shí)例
這篇文章主要分享一個(gè)Javascript隨機(jī)標(biāo)簽云代碼實(shí)例,需要的朋友可以參考下。2016-06-06javascript innerText和innerHtml應(yīng)用
innerText和innerHtml看字面也應(yīng)該理解的了2010-01-01javascript實(shí)現(xiàn)的上下無(wú)縫滾動(dòng)效果
這篇文章主要介紹了javascript實(shí)現(xiàn)的上下無(wú)縫滾動(dòng)效果,具有無(wú)縫滾動(dòng)及響應(yīng)鼠標(biāo)滑過(guò)停止?jié)L動(dòng)的功能,簡(jiǎn)便實(shí)用,需要的朋友可以參考下2016-09-09JS注冊(cè)/移除事件處理程序(ExtJS應(yīng)用程序設(shè)計(jì)實(shí)戰(zhàn))
最常做的事情就是注冊(cè)事件處理程序,因?yàn)樵贓xtJS的世界里,幾乎完全由時(shí)間組成,下面是處理程序案例,感興趣的朋友可以參考下哈,希望可以幫助到你2013-05-05javascript時(shí)間排序算法實(shí)現(xiàn)活動(dòng)秒殺倒計(jì)時(shí)效果
這篇文章主要介紹了javascript時(shí)間排序算法實(shí)現(xiàn)活動(dòng)秒殺倒計(jì)時(shí)效果,即一個(gè)頁(yè)面多個(gè)倒計(jì)時(shí)排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03TypeScript?使用?Tuple?Union?聲明函數(shù)重載
這篇文章主要介紹了TypeScript?使用?Tuple?Union?聲明函數(shù)重載,TypeScript 中為函數(shù)添加多個(gè)簽名后,依然需要添加相應(yīng)的代碼來(lái)判斷并從不同的簽名參數(shù)列表中獲取對(duì)應(yīng)的參數(shù),下文就來(lái)探索方法和技巧吧2022-04-04