在項(xiàng)目需求中需要實(shí)現(xiàn)一個滾軸聯(lián)動錨點(diǎn)的功能
效果圖如下:
功能代碼demo如下:
<template> <div class="container"> <div class="wrapper"> <div class="section" v-for="(item, index) in list" :key="index" :> <div :>{{item.name}}</div> </div> </div> <div id="nac" ></div> <nav > <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>
網(wǎng)站題目:Vue監(jiān)聽滾動實(shí)現(xiàn)錨點(diǎn)定位(雙向)示例-創(chuàng)新互聯(lián)
URL地址:http://jinyejixie.com/article16/dcgsgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、微信小程序、網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、做網(wǎng)站、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容