vue+element-ui監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位方式(雙向),錨點(diǎn)問(wèn)題
vue+element-ui監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位(雙向),錨點(diǎn)問(wèn)題
雙向綁定錨點(diǎn)問(wèn)題是最近項(xiàng)目中遇到的一個(gè)問(wèn)題,網(wǎng)上也有很多方法,不過(guò)還是想自己記錄一下。
需求
滾動(dòng)內(nèi)容為左右布局的右邊內(nèi)容欄內(nèi),內(nèi)容滾動(dòng) 左邊 定位 右邊內(nèi)容滾動(dòng)。
話不多說(shuō),直接看代碼
注意: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>我的證書(shū)</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í)長(zhǎng)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)樣式是無(wú)法點(diǎn)擊的,需求中有點(diǎn)擊步驟條,頁(yè)面滾動(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)是通過(guò)一個(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)文章
15分鐘學(xué)會(huì)vue項(xiàng)目改造成SSR(小白教程)
這篇文章主要介紹了15分鐘學(xué)會(huì)vue項(xiàng)目改造成SSR(小白教程),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12基于Element-Ui封裝公共表格組件的詳細(xì)圖文步驟
在平時(shí)開(kāi)發(fā)的時(shí)候很多情況都會(huì)使用到表格和分頁(yè)功能,下面這篇文章主要給大家介紹了關(guān)于如何基于Element-Ui封裝公共表格組件的詳細(xì)圖文步驟,需要的朋友可以參考下2022-09-09Vue和SpringBoot之間傳遞時(shí)間的方法實(shí)現(xiàn)
本文主要介紹了Vue和SpringBoot之間傳遞時(shí)間的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Vue.js 的移動(dòng)端組件庫(kù)mint-ui實(shí)現(xiàn)無(wú)限滾動(dòng)加載更多的方法
下面小編就為大家分享一篇Vue.js 的移動(dòng)端組件庫(kù)mint-ui實(shí)現(xiàn)無(wú)限滾動(dòng)加載更多的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12vite項(xiàng)目無(wú)法使用zangodb包裝器的解決方案
vite作為新一代工具鏈,具有很多便利之處,配置也非常簡(jiǎn)單,它很好地整合了Rollup和其他復(fù)雜的構(gòu)建項(xiàng),并提供了多種方向的典型腳手架模板,深受大家喜愛(ài),本文給大家介紹了如何解決vite項(xiàng)目無(wú)法使用zangodb包裝器的問(wèn)題,需要的朋友可以參考下2023-10-10vue3 響應(yīng)式對(duì)象如何實(shí)現(xiàn)方法的不同點(diǎn)
這篇文章主要介紹了vue3 響應(yīng)式對(duì)象如何實(shí)現(xiàn)方法的不同點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片滑動(dòng)驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Vue如何基于vue-i18n實(shí)現(xiàn)多國(guó)語(yǔ)言兼容
這篇文章主要介紹了Vue如何基于vue-i18n實(shí)現(xiàn)多國(guó)語(yǔ)言兼容,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07vue父元素點(diǎn)擊事件與子元素點(diǎn)擊事件沖突問(wèn)題
這篇文章主要介紹了vue父元素點(diǎn)擊事件與子元素點(diǎn)擊事件沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Vue3響應(yīng)式對(duì)象是如何實(shí)現(xiàn)的(1)
這篇文章主要介紹了Vue3響應(yīng)式對(duì)象是如何實(shí)現(xiàn)的,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08