vue+element-ui監(jiān)聽滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位方式(雙向),錨點(diǎn)問題
vue+element-ui監(jiān)聽滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位(雙向),錨點(diǎn)問題
雙向綁定錨點(diǎn)問題是最近項(xiàng)目中遇到的一個(gè)問題,網(wǎng)上也有很多方法,不過還是想自己記錄一下。
需求
滾動(dòng)內(nèi)容為左右布局的右邊內(nèi)容欄內(nèi),內(nèi)容滾動(dòng) 左邊 定位 右邊內(nèi)容滾動(dòng)。
話不多說,直接看代碼
注意:class=“d_jump”
<userInfo class="d_jump" ref="d_jump" :userInfo="userInfolists"></userInfo>
<el-menu
:default-active="activeName"
class="hk-person-item"
text-color="#333"
mode="horizontal"
@select="handleSelect()"
:style="{top: NavDistance + 'px'}"
>
<el-menu-item @click="jump(0)" index="1">
<span >個(gè)人信息</span>
</el-menu-item>
<el-menu-item @click="jump(1)" index="2">
<span>求職意向</span>
</el-menu-item>
<el-menu-item @click="jump(2)" index="3">
<span>求職狀態(tài)</span>
</el-menu-item>
<el-menu-item @click="jump(3)" index="4">
<span>教育經(jīng)歷</span>
</el-menu-item>
<el-menu-item @click="jump(4)" index="5">
<span>工作經(jīng)歷</span>
</el-menu-item>
<el-menu-item @click="jump(5)" index="6">
<span>項(xiàng)目經(jīng)歷</span>
</el-menu-item>
<el-menu-item @click="jump(6)" index="7">
<span>我的證書</span>
</el-menu-item>
</el-menu>
<div class="hk-details-bar" id="state" ref="myMenu">
<div id="resumes" class="hk-details-title" >
<jwIntention class="d_jump" ref="d_jump" :jwList="jwList" ></jwIntention>
<workState class="d_jump" ref="d_jump" :jobWantState="jobWantState"></workState>
<education class="d_jump" ref="d_jump" :eduLists="educationList"></education>
<workFor class="d_jump" ref="d_jump" :workList="workList"></workFor>
<myProject class="d_jump" ref="d_jump" :projectList="projectList"></myProject>
<myCert class="d_jump" ref="d_jump" :certList="certList"></myCert>
</div>data () {
return {
activeName: '1',
isActive: '1',
scrolls: {},
NavDistance: 321,
navStyle: 0,
scrollHeight: 0,
}
}相關(guān)代碼
mounted: function () {
this.$nextTick(function () {
window.addEventListener('scroll', this.onScroll)
})
},
methods: {
handleSelect (key, keyPath) {
console.log(key, keyPath)
var index = ''
index = key
console.log(index)
// if (index === '2') {
// this.$router.push({path: '/front/project'})
// this.activeName = '3'
// }
},
onScroll () {
let scrolled = document.documentElement.scrollTop || document.body.scrollTop
if (scrolled >= 1300) {
this.activeName = '7'
console.log('7')
} else if (scrolled < 60 && scrolled >= 0) {
this.activeName = '1'
console.log('1')
} else if (scrolled < 150 && scrolled >= 60) {
this.activeName = '2'
console.log('2')
} else if (scrolled < 200 && scrolled >= 100) {
this.activeName = '3'
console.log('3')
} else if (scrolled < 600 && scrolled >= 500) {
this.activeName = '4'
console.log('4')
} else if (scrolled < 700 && scrolled >= 600) {
this.activeName = '5'
console.log('5')
} else if (scrolled < 800 && scrolled >= 700) {
this.activeName = '6'
console.log('6')
}
},
onScrollBehavior (index) {
},
// 錨點(diǎn)實(shí)驗(yàn)
jump (index) {
console.log(index)
let active = index.toString()
this.isActive = active
// 判斷導(dǎo)航高度
// myMenu
let height = this.$refs.myMenu.offsetHeight
// 用 class="d_jump" 添加錨點(diǎn)
let jump = window.document.querySelectorAll('.d_jump')
let total = jump[index].offsetTop
console.log(total)
let distance = document.documentElement.scrollTop || document.body.scrollTop
// 平滑滾動(dòng),時(shí)長500ms,每10ms一跳,共50跳
let step = total / 50
// 判斷小導(dǎo)航距離頂部的位置
let newDistance = this.NavDistance
if (index === 0 && newDistance < total) {
newDistance = 321
} else if (index > 0 || index <= 6) {
this.NavDistance = total
} else {
this.NavDistance = total - 600
}
if (total > distance) {
smoothDown()
} else {
let newTotal = distance - total
step = newTotal / 50
smoothUp()
}
function smoothDown () {
if (distance < total) {
distance += step
document.body.scrollTop = distance
document.documentElement.scrollTop = distance
setTimeout(smoothDown, 10)
} else {
document.body.scrollTop = total
document.documentElement.scrollTop = total
}
}
function smoothUp () {
if (distance > total) {
distance -= step
document.body.scrollTop = distance
document.documentElement.scrollTop = distance
setTimeout(smoothUp, 10)
} else {
document.body.scrollTop = total
document.documentElement.scrollTop = total
}
}
}
},
watch: {
activeName: function () {
console.log(this.activeName)
}
}element步驟條增加錨點(diǎn)的實(shí)現(xiàn)
element的步驟條默認(rèn)樣式是無法點(diǎn)擊的,需求中有點(diǎn)擊步驟條,頁面滾動(dòng)到特定錨點(diǎn)需求
實(shí)現(xiàn)如下:
<el-card shadow="never" :body-style="{ padding: '5px' }" id="mySteps">
<el-steps simple>
<el-step v-for="(step,index) in laSteps" :title="step.title" :icon="step.icon"
:key="index"
:href="step.href" rel="external nofollow" :status="step.status" v-if="step.isShow"></el-step>
</el-steps>
</el-card>1.每個(gè)步驟條中 :href=“step.href” 錨點(diǎn)是通過一個(gè)href與一個(gè)id實(shí)現(xiàn)跳轉(zhuǎn),這里的href僅僅作為一種標(biāo)識(shí)與需要跳轉(zhuǎn)的點(diǎn)id相同即可
$('.el-step__title').click(function () {
var goHref = '#' + $(this).parent().parent().attr('href')
var anchor = that.$el.querySelector(goHref)
$('#djla').animate({
scrollTop: anchor.offsetTop
}, 500);
});2.獲取到href的值,根據(jù)這個(gè)值決定跳轉(zhuǎn)到什么地方.
3.$(’#djla’)為jq選中整個(gè)需滾動(dòng)區(qū)域
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+Express實(shí)現(xiàn)登錄注銷功能的實(shí)例代碼
這篇文章主要介紹了Vue+Express實(shí)現(xiàn)登錄,注銷功能,本文通過實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Vue3實(shí)現(xiàn)大文件(圖片/視頻附件)分片上傳的完整指南
這篇文章主要為大家詳細(xì)介紹了Vue3實(shí)現(xiàn)大文件分片上傳的完整指南,包括圖片,視頻附件的高效傳輸方案,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-12-12
Vue項(xiàng)目使用PostCSS做h5頁面的屏幕適配的配置步驟
PostCSS 是一個(gè)用 JavaScript 編寫的工具,用于將 CSS 轉(zhuǎn)換為另一種 CSS,在做h5頁面的屏幕適配時(shí),結(jié)合 PostCSS 的一些插件能輕松實(shí)現(xiàn),下面以結(jié)合 postcss-pxtorem 插件為例,詳細(xì)介紹配置步驟,需要的朋友可以參考下2025-02-02
VUE配置proxy代理的開發(fā)測(cè)試及生產(chǎn)環(huán)境
這篇文章主要為大家介紹了VUE配置proxy代理的開發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
前端Vue3引入高德地圖并展示行駛軌跡動(dòng)畫的步驟
最近在Vue項(xiàng)目中引入高德地圖,實(shí)現(xiàn)地圖展示與交互的方法和技術(shù),這里跟大家分享下,這篇文章主要給大家介紹了關(guān)于前端Vue3引入高德地圖并展示行駛軌跡動(dòng)畫的相關(guān)資料,需要的朋友可以參考下2024-09-09
vue如何點(diǎn)擊多個(gè)tab標(biāo)簽打開關(guān)閉多個(gè)頁面
這篇文章主要介紹了vue如何點(diǎn)擊多個(gè)tab標(biāo)簽打開關(guān)閉多個(gè)頁面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

