vue雙向錨點(diǎn)實(shí)現(xiàn)過(guò)程簡(jiǎn)易版(scrollIntoView)
一、需求
左邊是內(nèi)容板塊,右邊是目錄結(jié)構(gòu),點(diǎn)擊右邊內(nèi)容跳轉(zhuǎn)到左邊相應(yīng)位置展示,滑動(dòng)左邊內(nèi)容右邊目錄自動(dòng)跳轉(zhuǎn)。

二、實(shí)現(xiàn)
- 左邊每一個(gè)內(nèi)容模塊都給一個(gè)ref和應(yīng)該相同的class類名,方便獲取dom;
- 左邊內(nèi)容區(qū)域使用滑動(dòng)事件@scroll="handleScroll",內(nèi)容區(qū)域滑動(dòng)即觸發(fā)該方法;
- 右邊使用點(diǎn)擊事件@click="goAnchor('anchor-' + index, index)"獲取當(dāng)前點(diǎn)擊的dom;


handleScroll()滾動(dòng)監(jiān)聽方法實(shí)現(xiàn)滑動(dòng)左邊內(nèi)容對(duì)應(yīng)上右邊目錄

goAnchor()右邊錨點(diǎn)選中跳轉(zhuǎn)相應(yīng)內(nèi)容區(qū)域,通過(guò)scrollIntoView({ behavior: 'smooth' })平滑跳轉(zhuǎn)

實(shí)現(xiàn)代碼
<template>
<div class="panel-block flex">
<div class="panel-left" ref="anchorRef" @scroll="handleScroll">
<div class="panel-item anchor-item" ref="anchor-0">
<div class="panel-item-title">內(nèi)容區(qū)域1</div>
xxx
</div>
<div class="panel-item anchor-item" ref="anchor-1">
<div class="panel-item-title">內(nèi)容區(qū)域2</div>
xxx
</div>
<div class="panel-item anchor-item" ref="anchor-2">
<div class="panel-item-title">內(nèi)容區(qū)域3</div>
<div class="panel-item-content">
xxx
</div>
</div>
<div class="panel-item anchor-item" ref="anchor-3">
<div class="panel-item-title">內(nèi)容區(qū)域4</div>
<div class="panel-item-content">
xxx
</div>
</div>
</div>
<div class="panel-right">
<div class="step-line"></div>
<ul class="step-ul">
<li
class="flex_align_item_center"
v-for="(item, index) in stepData"
:key="index"
:class="{ 'step-active': activeBtn == index }"
@click="goAnchor('anchor-' + index, index)"
>
<div class="step-icon"></div>
<div class="step-label">{{ item }}</div>
</li>
</ul>
</div>
</div>
</template><script>
export default {
data() {
return {
activeBtn: 0, //錨點(diǎn)按鈕
stepData: ['區(qū)域1', '區(qū)域2', '區(qū)域3', '區(qū)域4'],
contentDiv: null, //錨點(diǎn)區(qū)域
}
},
methods: {
//錨點(diǎn)跳轉(zhuǎn)
goAnchor(selector, index) {
this.activeBtn = index
this.$refs[selector].scrollIntoView({ behavior: 'smooth' })
},
// 滾動(dòng)監(jiān)聽器
handleScroll(i) {
// 獲取所有錨點(diǎn)元素
const navContents = document.querySelectorAll('.anchor-item')
// 所有錨點(diǎn)元素的 offsetTop
const offsetTopArr = []
navContents.forEach((item) => {
offsetTopArr.push(item.offsetTop)
})
// 獲取當(dāng)前文檔流的 scrollTop
const scrollTop = this.$refs.anchorRef.scrollTop
offsetTopArr.forEach((item, index) => {
if (scrollTop >= item) {
this.activeBtn = index
}
})
},
}
}
</script>-----------------以下為樣式代碼----------------------
<style lang="scss" scoped>
.panel-block {
height: 100%;
.panel-left {
width: calc(100% - 180px);
overflow-y: auto;
height: 100%;
.panel-item {
.panel-item-content {
padding: 10px;
border-top: 0;
min-height: 40px;
.table-params {
.table-header {
height: 40px;
line-height: 40px;
width: 100%;
border: 1px solid #ebeef5;
border-bottom: none;
background: #f5f7fa;
font-weight: 700;
font-size: 14px;
color: #303133;
padding-left: 15px;
}
.table-body-type {
height: 32px;
line-height: 32px;
width: 100%;
border: 1px solid #ebeef5;
border-bottom: none;
color: #303133;
padding-left: 15px;
.type-title {
font-weight: 700;
font-size: 12px;
}
}
}
}
}
}
.panel-right {
padding-top: 10px;
position: fixed;
left: calc(100% - 210px);
.step-line {
position: absolute;
left: 6px;
top: 0;
width: 1px;
background: #dcdfe6;
height: calc(50vh - 85px);
z-index: 0;
}
.step-ul {
li {
padding-bottom: 20px;
.step-icon {
width: 13px;
height: 13px;
margin-right: 20px;
border-radius: 50%;
z-index: 1;
}
.step-label {
font-weight: 700;
font-size: 14px;
line-height: 22px;
color: #303133;
}
}
.step-active {
.step-icon {
border: 1px solid #1989fe;
background: #fff;
}
.step-label {
color: #1989fe;
}
}
}
}
::-webkit-scrollbar {
width: 0 !important;
}
}
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)vue中的input輸入框進(jìn)行郵箱驗(yàn)證方式
這篇文章主要介紹了對(duì)vue中的input輸入框進(jìn)行郵箱驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
簡(jiǎn)單實(shí)現(xiàn)Vue的observer和watcher
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)Vue的observer和watcher,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
electron+vue3實(shí)現(xiàn)自動(dòng)更新的示例代碼
本文主要介紹了electron+vue3實(shí)現(xiàn)自動(dòng)更新的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-10-10
詳解Vue3.0中ElementPlus<input輸入框自動(dòng)獲取焦點(diǎn)>
這篇文章主要給大家介紹了關(guān)于Vue3.0中ElementPlus<input輸入框自動(dòng)獲取焦點(diǎn)>的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue3.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-04-04
vue3如何使用provide實(shí)現(xiàn)狀態(tài)管理詳解
Vue3中有一對(duì)新增的api,provide和inject,熟悉Vue2的朋友應(yīng)該明,這篇文章主要給大家介紹了關(guān)于vue3如何使用provide實(shí)現(xiàn)狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2021-10-10
Vue實(shí)現(xiàn)滾動(dòng)加載更多效果的示例代碼
這篇文章介紹了在?Web?應(yīng)用中處理大量數(shù)據(jù)展示的兩種滾動(dòng)加載更多方案,滾動(dòng)加載更多通過(guò)分頁(yè)工作,優(yōu)點(diǎn)是實(shí)現(xiàn)簡(jiǎn)單、用戶體驗(yàn)流暢,缺點(diǎn)是內(nèi)存占用可能過(guò)高,虛擬列表只渲染視口內(nèi)容,性能好但實(shí)現(xiàn)復(fù)雜,本文介紹了Vue實(shí)現(xiàn)滾動(dòng)加載更多效果,需要的朋友可以參考下2024-12-12
使用kbone解決Vue項(xiàng)目同時(shí)支持小程序問題
這篇文章主要介紹了使用kbone解決Vue項(xiàng)目同時(shí)支持小程序問題,本文通過(guò)一個(gè)todo的例子跟大家詳細(xì)介紹,需要的朋友可以參考下2019-11-11

