vue實現(xiàn)錨點跳轉(zhuǎn)之scrollIntoView()方法詳解
vue錨點跳轉(zhuǎn)scrollIntoView()方法
滾動到某個特定元素
scrollIntoView();這個方法不用獲取右邊小標(biāo)題的高度,啥都不用,有id或者class就行啦,幾乎可以滿足你錨點跳轉(zhuǎn)的所有需求,對齊方式,以及平滑滾動了
這里是v-for循環(huán)出來需要點擊跳轉(zhuǎn)到對應(yīng)div的事件
<p> v-for="(value,index) in data" @click="scrollToPosition(index)">{{...}}</p> ?這就是你點擊scrollToPosition事件需要滾動對應(yīng)的div
<div> v-for="(value,index) in data" class="roll">{{...}}</div> ?js部分
methods:{
? scrollToPosition(index){
? ? ?document.getElementsByClassName('roll')[index].scrollIntoView()
}這樣就利用scrollIntoView()簡單實現(xiàn)了一個錨點跳轉(zhuǎn),下邊來看一個scrollIntoView中的一些屬性
scrollIntoView(true)相等于scrollIntoView();元素的頂端將和其所在滾動區(qū)的可視區(qū)域的頂端對齊
為true時相應(yīng)的 scrollIntoViewOptions: {block: “start”, inline: “nearest”}。這是這個參數(shù)的默認(rèn)值。
scrollIntoView(false)元素的底端將和其所在滾動區(qū)的可視區(qū)域的底端對齊
為false時相應(yīng)的scrollIntoViewOptions: {block: “end”, inline: “nearest”}。
同時他的參數(shù)也可以是一個object對象
? scrollIntoView({
? behavior:auto //定義動畫過渡效果"auto"或 "smooth" 之一。默認(rèn)為 "auto"。
? block:start//定義垂直方向的對齊, "start", "center", "end", 或 "nearest"之一。默認(rèn)為 "start"。
? inline:nearest//"start", "center", "end", 或 "nearest"之一。默認(rèn)為 "nearest"。
? })其中smooth是平滑滾動 start和end是目標(biāo)滾動到的位置
注意:
兼容性的問題多數(shù)主流瀏覽器已經(jīng)支持其基本功能,也就是說,使用true,false兩個參數(shù),來實現(xiàn)木訥的定位(沒有滾動動畫)是沒有任何問題的,但是傳入object參數(shù)時,就不得不說一句,IE各種版本會直接忽略,全部看成true參數(shù)屬性,如果想看到滾動動畫,就只有火狐和chrome
一個簡單的vue錨點跳轉(zhuǎn)demo
拿去直接粘貼就可
<template>
<div class="auto-adjust-edit" >
<!-- 按鈕 -->
<div class="operation-btn">
<div class="btn-item" v-for="(item, index) in partList" :key="index" @click="jump(index)"
:style="{background: activeStep === index ? '#eeeeee' : '#ffffff'}">{{item}}
</div>
</div>
<!-- 滾動區(qū)域 -->
<div class="scroll-content" @scroll="onScroll">
<div class="scroll-item">
<div class="part-title">基本信息</div>
</div>
<div class="scroll-item">
<div class="part-title">風(fēng)險控制</div>
</div>
<div class="scroll-item">
<div class="part-title">成本控制</div>
</div>
<div class="scroll-item">
<div class="part-title">量級控制</div>
</div>
<div class="scroll-item">
<div class="part-title">新計劃管理</div>
</div>
<div class="scroll-item">
<div class="part-title">老計劃管理</div>
</div>
<div class="scroll-item">
<div class="part-title">垃圾計劃清理</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
activeStep: 0,
partList: ['基本信息', '風(fēng)險控制', '成本控制', '量級控制', '新計劃管理', '老計劃管理', '垃圾計劃清理']
}
},
methods: {
// 滾動觸發(fā)按鈕高亮
onScroll (e) {
let scrollItems = document.querySelectorAll('.scroll-item')
for (let i = scrollItems.length - 1; i >= 0; i--) {
// 判斷滾動條滾動距離是否大于當(dāng)前滾動項可滾動距離
let judge = e.target.scrollTop >= scrollItems[i].offsetTop - scrollItems[0].offsetTop - 100;
if (judge) {
this.activeStep = i
break
}
}
},
// 點擊切換錨點
jump (index) {
let target = document.querySelector('.scroll-content')
let scrollItems = document.querySelectorAll('.scroll-item')
// 判斷滾動條是否滾動到底部
if (target.scrollHeight <= target.scrollTop + target.clientHeight) {
this.activeStep = index
}
let total = scrollItems[index].offsetTop - scrollItems[0].offsetTop // 錨點元素距離其offsetParent(這里是body)頂部的距離(待滾動的距離)
let distance = document.querySelector('.scroll-content').scrollTop // 滾動條距離滾動區(qū)域頂部的距離
// let distance = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset // 滾動條距離滾動區(qū)域頂部的距離(滾動區(qū)域為窗口)
// 滾動動畫實現(xiàn), 使用setTimeout的遞歸實現(xiàn)平滑滾動,將距離細分為50小段,10ms滾動一次
// 計算每一小段的距離
let step = total / 50
if (total > distance) {
smoothDown(document.querySelector('.scroll-content'))
} else {
let newTotal = distance - total
step = newTotal / 50
smoothUp(document.querySelector('.scroll-content'))
}
// 參數(shù)element為滾動區(qū)域
function smoothDown (element) {
if (distance < total) {
distance += step
element.scrollTop = distance
setTimeout(smoothDown.bind(this, element), 1)
} else {
element.scrollTop = total
}
}
// 參數(shù)element為滾動區(qū)域
function smoothUp (element) {
if (distance > total) {
distance -= step
element.scrollTop = distance
setTimeout(smoothUp.bind(this, element), 1)
} else {
element.scrollTop = total
}
}
// document.querySelectorAll('.scroll-item').forEach((item, index1) => {
// if (index === index1) {
// item.scrollIntoView({
// block: 'start',
// behavior: 'smooth'
// })
// }
// })
}
}
}
</script>
<style lang="scss" scoped>
.auto-adjust-edit {
flex-basis: 100%;
display: flex;
overflow: hidden;
height: 500px;
height: 100%;
// 側(cè)邊欄
.operation-btn {
width: 9.5%;
height: 95%;
margin-right: 0.5%;
padding-top: 1%;
margin-top: 4px;
background: white;
border: 1px solid rgb(190, 188, 188);
border-radius: 6px;
box-shadow: 0 3px 12px 0 rgba(0, 0, 0, 0.2);
.btn-item {
height: 40px;
line-height: 40px;
padding-left: 20px;
cursor: pointer;
}
}
// 表單內(nèi)容
.scroll-content {
height: 100%;
width: 90%;
overflow: auto;
.scroll-item {
background: white;
border-radius: 8px;
margin-bottom: 6px;
border: 1px solid rgb(216, 214, 214);
min-height: 300px;
// 標(biāo)題
.part-title {
height: 40px;
line-height: 40px;
font-weight: 600;
padding-left: 10px;
}
// 表單
/deep/.el-form {
border: 1px solid rgb(190, 189, 189);
width: 98%;
margin: 10px auto 30px;
border-radius: 6px;
/deep/.el-form-item {
margin-bottom: 12px;
margin-top: 10px;
}
}
}
}
}
</style>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3中g(shù)etCurrentInstance示例講解
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance的相關(guān)資料,文中還介紹了Vue3中關(guān)于getCurrentInstance的大坑,需要的朋友可以參考下2023-03-03
Vue3組件不發(fā)生變化如何監(jiān)聽pinia中數(shù)據(jù)變化
這篇文章主要給大家介紹了關(guān)于Vue3組件不發(fā)生變化如何監(jiān)聽pinia中數(shù)據(jù)變化的相關(guān)資料,pinia是Vue的存儲庫,它允許您跨組件/頁面共享狀態(tài),文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11
vue源碼學(xué)習(xí)之Object.defineProperty對象屬性監(jiān)聽
這篇文章主要介紹了vue源碼學(xué)習(xí)之Object.defineProperty對象屬性監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Vue-cli 使用json server在本地模擬請求數(shù)據(jù)的示例代碼
本篇文章主要介紹了Vue-cli 使用json server在本地模擬請求數(shù)據(jù)的示例代碼,非常具有實用價值,需要的朋友可以參考下2017-11-11

