設置 scroll-view
<scroll-view :scroll-y="true" :scroll-with-animation="true" :scroll-top="scrollTop" :style="`height:${height}px`"><view v-for="item in 10" :id="`box${item}`">box {{item}}</view>
</scroll-view>
scroll-y:設置為縱向
scroll-with-animation:在設置滾動條位置時使用動畫過渡
scroll-top:設置豎向滾動條位置
如果是縱向一定要設置高度,不然不生效
scroll-view:scroll-view | uni-app官網
通過onLoad獲取界面高度
const scrollTop = ref(0)
const height = ref(0)onLoad(() => {height.value = uni.getSystemInfoSync().windowHeight
})
通過onReady + nextTick 設置scrollTop
onReady(() => {nextTick(() => {const dom = uni.createSelectorQuery()dom.select(`#box9`).boundingClientRect((e: any) => {scrollTop.value = e.top}).exec()})
})
這里沒有設置 in,有需要可以在in中設置選取范圍,官方文檔:uni.createSelectorQuery() | uni-app官網
將選擇器的選取范圍更改為自定義組件?
component
?內,返回一個?SelectorQuery
?對象實例。(初始時,選擇器僅選取頁面范圍的節點,不會選取任何自定義組件中的節點)。?