最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uniapp中scroll-view實現(xiàn)自動滾動到最底部的方法

 更新時間:2023年10月08日 15:34:25   作者:TA遠方  
這篇文章主要給大家介紹了關于uniapp中scroll-view實現(xiàn)自動滾動到最底部的相關資料,在uniapp日常開發(fā)的過程中經(jīng)常會有局部滾動的需求,而scroll-view組件正好可以滿足這一需求,需要的朋友可以參考下

引言

在做uniapp項目中,有個滾動視圖組件scroll-view,跟微信小程序里的組件一樣的,想要實現(xiàn)自動滾動到最底部,是一件容易忽略的,小事情。

問題呈現(xiàn)

官網(wǎng)uniapp文檔上說可以控制滾動條,并沒有自動滾動到底部的設置選項,請看布局源代碼,如下,大多數(shù)可能都是這樣寫的

<template>
	<view>
		<scroll-view class="scroll-view" :style="{height:scrollViewHeight+'px'}" :scroll-y="true" :scroll-top="scrollTop" :scroll-with-animation="true">
				<block v-for="(item,index) in images" :key="index">
					<image class="item" :src="item.src" mode="aspectFill"></image>
				</block>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				images:[],
				scrollTop:0,//滾動條位置
				scrollViewHeight:300,//滾動視圖的高度
				//...
			};
		},
		//...
	}
</script>

雖然可以控制滾動條位置,但是,不知道滾動視圖框內(nèi)的內(nèi)容高度,要怎么精準控制滾動條位置呢

解決方案

通過各種嘗試,認為最好的方案就是,在滾動視圖組件內(nèi)再加一層view視圖,布局改動后,源代碼如下

<template>
	<view>
		<scroll-view class="scroll-view" :style="{height:scrollViewHeight+'px'}" :scroll-y="true" :scroll-top="scrollTop" :scroll-with-animation="true">
			<view id="scroll-view-content">
				<block v-for="(item,index) in images" :key="index">
					<image class="item" :src="item.src" mode="aspectFill"></image>
				</block>
			</view>
		</scroll-view>
	</view>
</template>
<script>
	//此處省略...
</script>

還有,實現(xiàn)滾動底部的處理方法scrollToBottom(),代碼如下

export default {
	data() {
		return {
			images:[],
			scrollTop:0,//滾動條位置
			scrollViewHeight:300,//滾動視圖的高度
			//...
		};
	},
	mounted() {
		let i = 10;
		do{
			this.images.push({
				src:'../../static/test.jpg',
				//...
			});
			i--;
		}while(i>0);
	},
	//...
	methods:{
		scrollToBottom(){
			this.$nextTick(()=>{
				uni.createSelectorQuery().in(this).select('#scroll-view-content').boundingClientRect((res)=>{
					let top = res.height-this.scrollViewHeight;
					if(top>0){
						this.scrollTop=top;
					}
				}).exec()
			})
		}
	}
}

注意事項

需要注意組件scroll-view的屬性設置

  • 需要設置固定高度,這樣視圖里面內(nèi)容當只有超過該高度才會有滾動效果
  • 需要設置scroll-with-animation=true,可以出現(xiàn)慢慢滾動到底部效果

總結

到此這篇關于uniapp中scroll-view實現(xiàn)自動滾動到最底部的文章就介紹到這了,更多相關uniapp scroll-view自動滾動最底部內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

延川县| 正镶白旗| 荣昌县| 庆阳市| 肃北| 彭泽县| 定边县| 岳阳市| 澳门| 安远县| 五家渠市| 探索| 江山市| 安徽省| 库车县| 疏勒县| 双牌县| 抚松县| 永年县| 永胜县| 卢龙县| 南通市| 千阳县| 湾仔区| 丹巴县| 金平| 辽源市| 鱼台县| 宁武县| 金昌市| 含山县| 梓潼县| 全州县| 台中县| 鸡西市| 定远县| 内江市| 开封县| 镇沅| 昂仁县| 吉隆县|