uni-app 滾動到指定位置的操作方法

方法1:使用標簽,可以將頁面橫向(或縱向)滾動到指定位置
無法滾動 將代碼放在setTimeout,nextTick里執(zhí)行
<!-- 左邊 -->
<scroll-view show-scrollbar="false" scroll-y="true" class="left-box"
:scroll-top="scrollLeftTop" scroll-with-animation="true">
<view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
:id="'scroll' + activeLeftTab" :class="{'active':activeLeftTab==item.id}">
{{item.name}}
</view>
</scroll-view>data(){
return {
servicesLeftList:[],
scrollLeftTop:0,,//滾動位置
activeLeftTab:"" //選中的樣式
}
}
getData() {
//接口
getServicesTree().then(res => {
this.servicesLeftList= res.data
res.data.forEach((item, ind) => {
setTimeout(()=>{
uni.createSelectorQuery().in(this).select('#scroll' + item.id)
.boundingClientRect(res => {
scrollLeftTop.value = 65 * ind; // 設(shè)置滾動條距離左側(cè)的距離
}).exec()
},100)
})
})
}方法二 使用uni.pageScrollTo 使頁面縱向滾到到指定位置
建議設(shè)置height為auto :height:auto
<view class="left-box" >
<view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
:class="{'active':activeLeftTab==item.id}">
{{item.name}}
</view>
</view>
//方法
uni.pageScrollTo({
scrollTop: 0,
duration: 500
});方法三 用scroll-view描點
//左邊 :scroll-top="scrollLeftTop"
<scroll-view scroll-y="true" class="left-box" scroll-with-animation="true"
:scroll-into-view="tracingLeftPoint">
<view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
:id="'scroll' + item.id" :class="{'active':activeLeftTab==item.id}">
{{item.name}}
</view>
</scroll-view>
//方法
data(){
retrun {
tracingLeftPoint:"",//描點id
}
}
getData() {
//接口
getServicesTree().then(res => {
this.servicesLeftList= res.data
res.data.forEach((item, ind) => {
setTimeout(()=>{
this.tracingLeftPoint= 'scroll' + item.id
},200)
})
})
}到此這篇關(guān)于uni-app 滾動到指定位置的文章就介紹到這了,更多相關(guān)uni-app 滾動到指定位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js中一個函數(shù)獲取另一個函數(shù)返回值問題探討
在本文將為大家詳細探討下js中一個函數(shù)獲取另一個函數(shù)返回值問題,比較模糊的朋友可以學習下哦2013-11-11
微信小程序使用wx.navigateTo路由跳轉(zhuǎn)層級限制問題小結(jié)
在微信小程序開發(fā)中,wx.navigateTo和wx.redirectTo是兩種頁面跳轉(zhuǎn)方式,wx.navigateTo允許跳轉(zhuǎn)到新頁面并保留當前頁面,適合需要返回的場景,但受頁面棧10層限制,wx.redirectTo則關(guān)閉當前頁面后跳轉(zhuǎn),本文介紹微信小程序使用wx.navigateTo路由跳轉(zhuǎn)層級限制問題2024-10-10
js使用for循環(huán)查詢數(shù)組中是否存在某個值
IE8不支持indexOf,因此寫一個for循環(huán)來判斷是否存在,下面是代碼,經(jīng)測試還不錯2014-08-08
JavaScript數(shù)字和字符串轉(zhuǎn)換示例
這篇文章主要介紹了JavaScript數(shù)字和字符串轉(zhuǎn)換的應(yīng)用,需要的朋友可以參考下2014-03-03

