uniapp中scroll-view基礎(chǔ)用法示例代碼
前言
在uniapp日常開(kāi)發(fā)的過(guò)程中經(jīng)常會(huì)有局部滾動(dòng)的需求,而scroll-view組件正好可以滿(mǎn)足這一需求。需注意在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ò)用戶(hù)滑動(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組件也可以滿(mǎn)足我們下拉刷新的需求、首先通過(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)文章
JS 實(shí)現(xiàn)Json查詢(xún)的方法實(shí)例
曾經(jīng)看過(guò)一個(gè)大牛寫(xiě)的實(shí)現(xiàn)Json的一個(gè)模板類(lèi),今天突然沒(méi)事就來(lái)自己試著寫(xiě)寫(xiě)。還好,一些東西還記得,思路還算清晰。直接上代碼了2013-04-04
d3.js實(shí)現(xiàn)自定義多y軸折線(xiàn)圖的示例代碼
本篇文章主要介紹了d3.js實(shí)現(xiàn)自定義多y軸折線(xiàn)圖的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Parcel.js + Vue 2.x 極速零配置打包體驗(yàn)教程
這篇文章主要介紹了Parcel.js + Vue 2.x 極速零配置打包體驗(yàn) 的相關(guān)資料,需要的朋友可以參考下2017-12-12
select、radio表單回顯功能實(shí)現(xiàn)避免使用jquery載入賦值
select、radio表單回顯避免使用jquery載入賦值,最好的做法是:在jsp頁(yè)面進(jìn)行邏輯判斷,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06
JavaScript的數(shù)據(jù)類(lèi)型轉(zhuǎn)換原則(干貨)
JavaScript是一門(mén)弱類(lèi)型(或稱(chēng)動(dòng)態(tài)類(lèi)型)的語(yǔ)言,即變量的類(lèi)型是不確定的。下面通過(guò)本文給大家分享javascript數(shù)據(jù)類(lèi)型轉(zhuǎn)換小結(jié),包括顯示轉(zhuǎn)換的數(shù)據(jù)類(lèi)型和隱式的數(shù)據(jù)類(lèi)型轉(zhuǎn)換,感興趣的朋友跟隨腳本一起看看吧2018-03-03

