vue實現(xiàn)側(cè)邊定位欄
本文實例為大家分享了vue實現(xiàn)側(cè)邊定位欄的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)思路:
1.通過點擊側(cè)邊欄,定位到響應(yīng)的內(nèi)容
2.滑動滑動欄,側(cè)邊欄同步高亮對應(yīng)的item
效果圖如下:
1. 通過點擊側(cè)邊欄,定位到響應(yīng)的內(nèi)容
如果是用html的話我們可以用 錨點 的辦法進(jìn)行定位;
在vue中,我們可以通過獲取組件的高度,將滑動欄定位到對應(yīng)的位置
在進(jìn)入主題之前我們需要先了解3個關(guān)于獲取高度的屬性
1.scrollTop 滑動欄中的滑塊離視區(qū)最頂部的距離
document.documentElement.scrollTop || document.body.scrollTop
2.clientHeight 視區(qū)的高度
document.documentElement.clientHeight || document.body.clientHeight
3.scrollHeight 滑動欄里面的滑動塊的高度
document.documentElement.scrollHeight || document.body.scrollHeight
vue中我們可以通過this.$refs.xxx.$el.offsetTop
獲取組件距離頁面最頂部的距離,通過賦值給document.documentElement.scrollTop
選中組件距離頁面最頂部的高度,控制滑動框滑到頁面對應(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() { ? ? // ?。∽⒁?,需要頁面渲染完才能獲取各個盒子的高度 ? ? 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è)邊欄實現(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'">實時總覽</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)">詢價數(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: { // 點擊側(cè)邊欄item時觸發(fā) // 通過document.documentElement.scrollTop控制滑動欄位置 ? ? clickanchor(itemName, i) { ? ? ? if (itemName === 'top') { ? ? ? ? document.documentElement.scrollTop = 0; // 滑動欄位置 ? ? ? ? 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. 滑動滑動欄,側(cè)邊欄同步高亮對應(yīng)的item
通過監(jiān)聽滑動欄滑動,獲取滑動塊距離頁面頂部的高度,和組件距離頁面頂部的高度進(jìn)行對比,反向設(shè)置滑動欄的高亮位置;
// 監(jiān)聽滑動欄滾動,通過監(jiān)聽滾動到的位置,到 ? ? 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; ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? }, ? }, };
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue3.0+element Plus實現(xiàn)頁面布局側(cè)邊欄菜單路由跳轉(zhuǎn)功能
- Vue3實現(xiàn)簡約型側(cè)邊欄的示例代碼
- vue使用動態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式
- Vue3點擊側(cè)邊導(dǎo)航欄完成切換頁面內(nèi)組件全過程(WEB)
- Vue-element-admin平臺側(cè)邊欄收縮控制問題
- vue框架實現(xiàn)將側(cè)邊欄完全隱藏
- VuePress 側(cè)邊欄的具體使用
- 如何利用Vue3管理系統(tǒng)實現(xiàn)動態(tài)路由和動態(tài)側(cè)邊菜單欄
- vue elementui簡易側(cè)拉欄的使用小結(jié)
相關(guān)文章
建立和維護(hù)大型 Vue.js 項目的 10 個最佳實踐
這篇文章小編要與大家分享的是建立和維護(hù)大型 Vue.js 項目的 10 個最佳實踐,需要的小伙伴請和小編一起學(xué)習(xí)下面文章的具體內(nèi)容吧2021-09-09@vue/cli4.x版本的vue.config.js常用配置方式
這篇文章主要介紹了@vue/cli4.x版本的vue.config.js常用配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05