scrollIntoView 作用:使指定元素滾動到頁面可見范圍
使用:
1. 給指定元素分配 class 或 id , 使js中能夠選取到
2. 根據標志,獲取到dom元素
3. scrollIntoView 滾動到指定位置,可選擇滾動到頁面中的位置及過渡動畫
注意:頁面可滾動時方法才生效
實例:
<template>
<div id="aa">
<button @click="goTo(50)">go to 50</button>
<div v-for="i in 100" :key="i" :class="'p'+i">{{i}}</div>
</div>
</template>
<script>
export default {
methods: {
goTo(n) {
var el = document.getElementsByClassName('p' + n)[0]
if (el) {
el.scrollIntoView({block: "start", behavior: "smooth"})
}
}
},
}
</script>
<style>
</style>
