Vue監(jiān)聽滾動實(shí)現(xiàn)錨點(diǎn)定位(雙向)示例
在項(xiàng)目需求中需要實(shí)現(xiàn)一個(gè)滾軸聯(lián)動錨點(diǎn)的功能
效果圖如下:
功能代碼demo如下:
<template> <div class="container"> <div class="wrapper"> <div class="section" style="height:500px;width:100%" v-for="(item, index) in list" :key="index" :style="{'height':index==0?'1000px':'500px'}"> <div style="width:100%;height:100%;font-size:30px;text-align:center;font-weight:bold;color:#fff;" :style="{'background':item.backgroundcolor}">{{item.name}}</div> </div> </div> <div id="nac" style="height:500px;"></div> <nav style="position:fixed;right:30px;top:300px;"> <a class="nav1" v-for="(item, index) in navList" :key="index" @click="jump(index)" :class="index==0?'current':''">{{item}}</a> </nav> </div> </template> <script> export default { data(){ return { scroll: '', list: [{ name: "第一條", backgroundcolor: "#90B2A3" }, { name: "第二條", backgroundcolor: "#A593B2" }, { name: "第三條", backgroundcolor: "#A7B293" }, { name: "第四條", backgroundcolor: "#0F2798" }, { name: "第五條", backgroundcolor: "#0A464D" }], navList: [1, 2, 3, 4, 5] } }, methods: { dataScroll: function () { this.scroll = document.documentElement.scrollTop || document.body.scrollTop; }, jump(index) { let jump = document.getElementsByClassName('section'); // 獲取需要滾動的距離 let total = jump[index].offsetTop; // Chrome document.body.scrollTop = total; // Firefox document.documentElement.scrollTop = total; // Safari window.pageYOffset = total; // $('html, body').animate({ // 'scrollTop': total // }, 400); }, loadSroll: function () { var self = this; var $navs = $(".nav1"); var sections = document.getElementsByClassName('section'); for (var i = sections.length - 1; i >= 0; i--) { if (self.scroll >= sections[i].offsetTop - 100) { $navs.eq(i).addClass("current").siblings().removeClass("current") break; } } } }, watch: { scroll: function () { this.loadSroll() } }, mounted() { window.addEventListener('scroll', this.dataScroll); } } </script> <style> * { padding: 0; margin: 0; } .nav1 { display: block; width: 40px; height: 40px; text-align: center; line-height: 40px; background: #eee; margin: 10px 0; } .current { color: #fff; background: red; } </style>
以上這篇Vue監(jiān)聽滾動實(shí)現(xiàn)錨點(diǎn)定位(雙向)示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中如何實(shí)現(xiàn)拖拽調(diào)整順序功能
這篇文章主要介紹了vue中如何實(shí)現(xiàn)拖拽調(diào)整順序功能問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05使用Vue+Django+Ant Design做一個(gè)留言評論模塊的示例代碼
這篇文章主要介紹了使用Vue+Django+Ant Design做一個(gè)留言評論模塊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06如何使用 Vue Router 的 meta 屬性實(shí)現(xiàn)多種功能
在Vue.js中,Vue Router 提供了強(qiáng)大的路由管理功能,通過meta屬性,我們可以在路由定義中添加自定義元數(shù)據(jù),以實(shí)現(xiàn)訪問控制、頁面標(biāo)題設(shè)置、角色權(quán)限管理、頁面過渡效果,本文將總結(jié)如何使用 meta 屬性來實(shí)現(xiàn)這些常見的功能,感興趣的朋友一起看看吧2024-06-06vue?頁面卡死,點(diǎn)擊無反應(yīng)的問題及解決
這篇文章主要介紹了vue?頁面卡死,點(diǎn)擊無反應(yīng)的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10解決vue偵聽器watch,調(diào)用this時(shí)出現(xiàn)undefined的問題
這篇文章主要介紹了解決vue偵聽器watch,調(diào)用this時(shí)出現(xiàn)undefined的問題,具有很好的參考2020-10-10Vue3.x項(xiàng)目開發(fā)的一些常用知識點(diǎn)總結(jié)
目前Vue3.0是會兼容大部分2.x的語法,下面這篇文章主要給大家介紹了關(guān)于Vue3.x項(xiàng)目開發(fā)的一些常用知識點(diǎn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了記一次用ts+vuecli4重構(gòu)項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05