vue滑動(dòng)吸頂及錨點(diǎn)定位的示例代碼
在上篇文章給大家介紹了vue實(shí)現(xiàn)吸頂、錨點(diǎn)和滾動(dòng)高亮按鈕效果 感興趣的朋友可以點(diǎn)擊查看http://www.dhdzp.com/article/172365.htm
今天給大家繼續(xù)分享vue滑動(dòng)吸頂及錨點(diǎn)定位的代碼,具體內(nèi)容如下所示:
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事件,獲取頁面的滾動(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è)置頁面滾動(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;
}
}
總結(jié)
到此這篇關(guān)于vue滑動(dòng)吸頂及錨點(diǎn)定位的示例代碼的文章就介紹到這了,更多相關(guān)vue 滑動(dòng)吸頂錨點(diǎn)定位內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Vue axios post請(qǐng)求,后臺(tái)獲取不到數(shù)據(jù)的問題方法
今天小編就為大家分享一篇解決Vue axios post請(qǐng)求,后臺(tái)獲取不到數(shù)據(jù)的問題方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Vue使用 onMounted 確保在組件掛載后執(zhí)行異步操作示例詳解
在 Vue.js 或其他類似框架中,使用 onMounted 是為了確保在組件掛載后執(zhí)行異步操作,這篇文章主要介紹了Vue使用onMounted確保在組件掛載后執(zhí)行異步操作,需要的朋友可以參考下2023-06-06
在Vue組件化中利用axios處理ajax請(qǐng)求的使用方法
這篇文章主要給大家介紹了在Vue組件化中利用axios處理ajax請(qǐng)求的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
vue.js內(nèi)置組件之keep-alive組件使用
keep-alive是Vue.js的一個(gè)內(nèi)置組件。這篇文章主要介紹了vue.js內(nèi)置組件之keep-alive組件使用,需要的朋友可以參考下2018-07-07
swiper/vue 獲取 swiper實(shí)例方法詳解
在網(wǎng)上搜了一下如何調(diào)用swiper實(shí)例,大部分都是通過 swiperRef = new Swiper(‘.swiper’, options) 這種方法初始化swiper,然后直接能用 swiperRef 實(shí)例,這篇文章主要介紹了swiper/vue 獲取 swiper實(shí)例方法詳解,需要的朋友可以參考下2023-12-12
vue 實(shí)現(xiàn)tab切換保持?jǐn)?shù)據(jù)狀態(tài)
這篇文章主要介紹了vue 實(shí)現(xiàn)tab切換保持?jǐn)?shù)據(jù)狀態(tài),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
解決vue中無法動(dòng)態(tài)修改jqgrid組件 url地址的問題
下面小編就為大家分享一篇解決vue中無法動(dòng)態(tài)修改jqgrid組件 url地址的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn)
VuePress為每一個(gè)由它生成的頁面提供預(yù)加載的html,不僅加載速度極佳,同時(shí)對(duì)seo非常友好。這篇文章主要介紹了基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn),需要的朋友可以參考下2018-04-04

