這篇文章主要講解了vue滑動(dòng)吸頂及錨點(diǎn)定位的用法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
沈北新ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
Vue項(xiàng)目中需要實(shí)現(xiàn)滑動(dòng)吸頂以及錨點(diǎn)定位功能。template代碼如下:
<template> <div class="main"> <div id='menu'> <ul> <li v-for="item in tabList" @click='clickTab'></li> </ul> </div> <div id='div1'></div> <div id='div2'></div> <div id='div3'></div> </div> </template>
(1)滑動(dòng)吸頂:
監(jiān)聽scroll事件,獲取頁(yè)面的滾動(dòng)距離,一旦滾動(dòng)距離大于目標(biāo)值,實(shí)現(xiàn)滑動(dòng)吸頂功能。
public isFixed = false; public mounted() { this.menuTop = (document.getElementById('menu') as any).offsetTop; window.addEventListener('scroll', this.handleScroll); } public handleScroll() { const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; // 獲取滑動(dòng)距離 if (scrollTop < this.menuTop ) { this.isFixed = false; } else { this.isFixed = true; // 設(shè)置fixed定位 } } public destroyed() { window.removeEventListener('scroll', this.handleScroll); }
(2)錨點(diǎn)定位。點(diǎn)擊tab,設(shè)置頁(yè)面滾動(dòng)距離。
public clickTab(index: number) { this.activeIndex = index; this.isFixed = true; const menuHeight= (document.getElementById('menu') as any).offsetHeight; const div1= (document.getElementById('div1') as any).offsetTop; const div2= (document.getElementById('div2') as any).offsetTop; const div3= (document.getElementById('div3') as any).offsetTop; const div4= (document.getElementById('div4') as any).offsetTop; switch (index) { case 0: document.body.scrollTop = document.documentElement.scrollTop = div1 - menuHeight; break; case 1: document.body.scrollTop = document.documentElement.scrollTop = div2 - menuHeight; break; case 2: document.body.scrollTop = document.documentElement.scrollTop = div3 - menuHeight; break; case 3: document.body.scrollTop = document.documentElement.scrollTop = div4 - menuHeight; break; default: document.body.scrollTop = document.documentElement.scrollTop = div1- menuHeight; } }
看完上述內(nèi)容,是不是對(duì)vue滑動(dòng)吸頂及錨點(diǎn)定位的用法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:vue滑動(dòng)吸頂及錨點(diǎn)定位的用法
標(biāo)題鏈接:http://aaarwkj.com/article0/iidgoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、、電子商務(wù)、網(wǎng)站排名、面包屑導(dǎo)航、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)