vue實(shí)現(xiàn)側(cè)邊定位欄
本文實(shí)例為大家分享了vue實(shí)現(xiàn)側(cè)邊定位欄的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)思路:
1.通過點(diǎn)擊側(cè)邊欄,定位到響應(yīng)的內(nèi)容
2.滑動(dòng)滑動(dòng)欄,側(cè)邊欄同步高亮對(duì)應(yīng)的item
效果圖如下:

1. 通過點(diǎn)擊側(cè)邊欄,定位到響應(yīng)的內(nèi)容
如果是用html的話我們可以用 錨點(diǎn) 的辦法進(jìn)行定位;
在vue中,我們可以通過獲取組件的高度,將滑動(dòng)欄定位到對(duì)應(yīng)的位置
在進(jìn)入主題之前我們需要先了解3個(gè)關(guān)于獲取高度的屬性
1.scrollTop 滑動(dòng)欄中的滑塊離視區(qū)最頂部的距離
document.documentElement.scrollTop || document.body.scrollTop
2.clientHeight 視區(qū)的高度
document.documentElement.clientHeight || document.body.clientHeight
3.scrollHeight 滑動(dòng)欄里面的滑動(dòng)塊的高度
document.documentElement.scrollHeight || document.body.scrollHeight

vue中我們可以通過this.$refs.xxx.$el.offsetTop獲取組件距離頁面最頂部的距離,通過賦值給document.documentElement.scrollTop選中組件距離頁面最頂部的高度,控制滑動(dòng)框滑到頁面對(duì)應(yīng)位置。相關(guān)代碼如下:
頁面代碼
// 頁面組件代碼
<div>
? ? <btl-header />
? ? <div class="reports">
? ? ? <side-bar v-bind="sideBarData" />
? ? ? <div class="main">
? ? ? ? <live-overview-part
? ? ? ? ? ref="liveOverview"
? ? ? ? ? :shopNameOptions="allShopName"
? ? ? ? ? :dateSelectorData="dateType_0" />
? ? ? ? <procurenment-alert
? ? ? ? ? ref="procurenmentAlert"
? ? ? ? ? :carBrandOptions="carBrandOptions"
? ? ? ? ? :shopNameOptions="allShopName"
? ? ? ? ? :dateSelectorData="dateType_2" />
? ? ? ? <sales-overview
? ? ? ? ? ref="salesOverview"
? ? ? ? ? :shopNameOptions="allShopName"
? ? ? ? ? :dateSelectorData1="pardsType_0"
? ? ? ? ? :dateSelectorData2="dateType_1" />
? ? ? ? <inquiry-data
? ? ? ? ? ref="inquiryData"
? ? ? ? ? :shopNameOptions="allShopName"
? ? ? ? ? :dateSelectorData="dateType_2" />
? ? ? ? <transaction-data
? ? ? ? ? ref="transactionData"
? ? ? ? ? :carBrandOptions="carBrandOptions"
? ? ? ? ? :shopNameOptions="allShopName"
? ? ? ? ? :qualityOptions="qualityOptions"
? ? ? ? ? :colorSeries="colorSeries"
? ? ? ? ? :dateSelectorData="dateType_2" />
? ? ? </div>
? ? </div>
// ?獲取組件距離頁面頂部高度 ?。?!
?<script>
mounted() {
? ? // !!注意,需要頁面渲染完才能獲取各個(gè)盒子的高度
? ? this.sideBarData.offsetTopliveOverview = this.$refs.liveOverview.$el.offsetTop;
? ? this.sideBarData.offsetTopprocurenmentAlert = this.$refs.procurenmentAlert.$el.offsetTop;
? ? this.sideBarData.offsetTopsalesOverview = this.$refs.salesOverview.$el.offsetTop;
? ? this.sideBarData.offsetTopinquiryData = this.$refs.inquiryData.$el.offsetTop;
? ? this.sideBarData.offsetToptransactionData = this.$refs.transactionData.$el.offsetTop;
? ?},
? </script>側(cè)邊欄實(shí)現(xiàn)代碼
// 側(cè)邊欄代碼
<template>
? <hn-card class="sidebar">
? ? <ul class="nav-tabs">
? ? ? <li
? ? ? ? class="activeTip"
? ? ? ? ref="activeTip"></li>
? ? ? <li
? ? ? ? @click="clickanchor('liveOverview',0)"
? ? ? ? class="item"
? ? ? ? :class="active==='liveOverview'?'active':'noActive'">實(shí)時(shí)總覽</li>
? ? ? <li
? ? ? ? class="item"
? ? ? ? :class="active==='procurenmentAlert'?'active':'noActive'"
? ? ? ? @click="clickanchor('procurenmentAlert',1)">采購預(yù)警</li>
? ? ? <li
? ? ? ? class="item"
? ? ? ? :class="active==='salesOverview'?'active':'noActive'"
? ? ? ? @click="clickanchor('salesOverview',2)">銷售概覽</li>
? ? ? <li
? ? ? ? class="item"
? ? ? ? :class="active==='inquiryData'?'active':'noActive'"
? ? ? ? @click="clickanchor('inquiryData',3)">詢價(jià)數(shù)據(jù)</li>
? ? ? <li
? ? ? ? class="item"
? ? ? ? :class="active==='transactionData'?'active':'noActive'"
? ? ? ? @click="clickanchor('transactionData',4)">交易數(shù)據(jù)</li>
? ? ? <li
? ? ? ? class="item"
? ? ? ? :class="active==='top'?'active':'noActive'"
? ? ? ? @click="clickanchor('top')">返回頂部</li>
? ? </ul>
? </hn-card>
</template>
// 側(cè)邊欄js
<script>
methods: {
// 點(diǎn)擊側(cè)邊欄item時(shí)觸發(fā)
// 通過document.documentElement.scrollTop控制滑動(dòng)欄位置
? ? clickanchor(itemName, i) {
? ? ? if (itemName === 'top') {
? ? ? ? document.documentElement.scrollTop = 0; // 滑動(dòng)欄位置
? ? ? ? this.active = itemName;
? ? ? ? this.$refs.activeTip.style.transform = `translateY(${196}px)`;
? ? ? ? return;
? ? ? }
? ? ? this.$refs.activeTip.style.transform = `translateY(${i * 39}px)`;
? ? ? document.documentElement.scrollTop = this[`offsetTop${itemName}`];
? ? ? this.active = itemName;
? ? },
</script>2. 滑動(dòng)滑動(dòng)欄,側(cè)邊欄同步高亮對(duì)應(yīng)的item
通過監(jiān)聽滑動(dòng)欄滑動(dòng),獲取滑動(dòng)塊距離頁面頂部的高度,和組件距離頁面頂部的高度進(jìn)行對(duì)比,反向設(shè)置滑動(dòng)欄的高亮位置;
// 監(jiān)聽滑動(dòng)欄滾動(dòng),通過監(jiān)聽滾動(dòng)到的位置,到
? ? scrollChange() {
? ? ? const scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
? ? ? const windowHeight = document.documentElement.clientHeight || document.body.clientHeight || 0;
? ? ? const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight || 0;
? ? ? const allContentOffsettop = [
? ? ? ? 'offsetTopliveOverview',
? ? ? ? 'offsetTopprocurenmentAlert',
? ? ? ? 'offsetTopsalesOverview',
? ? ? ? 'offsetTopinquiryData',
? ? ? ? 'offsetToptransactionData'];
? ? ? if (scrollTop === 0) {
? ? ? ? if (this.active !== 'top') {
? ? ? ? ? this.active = 'top';
? ? ? ? ? this.$refs.activeTip.style.transform = `translateY(${196}px)`;
? ? ? ? }
? ? ? } else if (scrollTop + windowHeight > scrollHeight || scrollTop + windowHeight === scrollHeight) {
? ? ? ? if (this.active !== 'transactionData') {
? ? ? ? ? this.active = 'transactionData';
? ? ? ? ? this.$refs.activeTip.style.transform = `translateY(${157}px)`;
? ? ? ? }
? ? ? } else {
? ? ? ? for (let i = 0; i < allContentOffsettop.length; i++) {
? ? ? ? ? if (this[allContentOffsettop[i]] - 1 > scrollTop) {
? ? ? ? ? ? const contentName = allContentOffsettop[i - 1].replace('offsetTop', '');
? ? ? ? ? ? if (this.active !== contentName) {
? ? ? ? ? ? ? this.active = contentName;
? ? ? ? ? ? ? this.$refs.activeTip.style.transform = `translateY(${(i - 1) * 39}px)`;
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? },
? },
};以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue3.0+element Plus實(shí)現(xiàn)頁面布局側(cè)邊欄菜單路由跳轉(zhuǎn)功能
- Vue3實(shí)現(xiàn)簡(jiǎn)約型側(cè)邊欄的示例代碼
- vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式
- Vue3點(diǎn)擊側(cè)邊導(dǎo)航欄完成切換頁面內(nèi)組件全過程(WEB)
- Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問題
- vue框架實(shí)現(xiàn)將側(cè)邊欄完全隱藏
- VuePress 側(cè)邊欄的具體使用
- 如何利用Vue3管理系統(tǒng)實(shí)現(xiàn)動(dòng)態(tài)路由和動(dòng)態(tài)側(cè)邊菜單欄
- vue elementui簡(jiǎn)易側(cè)拉欄的使用小結(jié)
相關(guān)文章
vue 使用 vue-pdf 實(shí)現(xiàn)pdf在線預(yù)覽的示例代碼
這篇文章主要介紹了vue 使用 vue-pdf 實(shí)現(xiàn)pdf在線預(yù)覽的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
vue-element-admin搭建后臺(tái)管理系統(tǒng)的實(shí)現(xiàn)步驟
本文主要介紹了vue-element-admin搭建后臺(tái)管理系統(tǒng)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼詳解
在使用elementUI el-form 中,對(duì)于業(yè)務(wù)不同的時(shí)候可能會(huì)產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個(gè)表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼,需要的朋友可以參考下2019-05-05
Vue 組件參數(shù)校驗(yàn)與非props特性的方法
這篇文章主要介紹了Vue 組件參數(shù)校驗(yàn)與非props特性的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Vue項(xiàng)目中token驗(yàn)證登錄(前端部分)
這篇文章主要為大家詳細(xì)介紹了Vue項(xiàng)目中token驗(yàn)證登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
vue中使用AJAX實(shí)現(xiàn)讀取來自XML文件的信息
這篇文章主要為大家詳細(xì)介紹了vue中如何使用AJAX實(shí)現(xiàn)讀取來自XML文件的信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下2023-12-12
vue?過濾、模糊查詢及計(jì)算屬性?computed詳解
計(jì)算屬性是vue里面為了簡(jiǎn)化在模板語法中對(duì)響應(yīng)式屬性做計(jì)算而存在的,這篇文章主要介紹了vue?過濾、模糊查詢(計(jì)算屬性?computed),需要的朋友可以參考下2022-11-11

