vue 中固定導(dǎo)航欄的實(shí)例代碼
更新時間:2019年11月01日 10:33:54 作者:徹夜不歸
今天小編就為大家分享一篇vue 中固定導(dǎo)航欄的實(shí)例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
點(diǎn)擊按鈕回頂
<template lang="html">
<div class="gotop-box">
<i @click="gotop"class="icon topIcon"></i>
</div>
</template>
<script>
export default {
methods: {
gotop: function () { // 點(diǎn)擊回頂按鈕 返回頂部
setTimeout(()=>{
document.body.scrollTop = 0
document.documentElement.scrollTop = 0
},1000)
}
}
}
window.onscroll = function() {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
const goTop= document.querySelector('.gotop-box')
if (scrollTop >200) {
goTop.style.display = 'block'
} else {
goTop.style.display = 'none'
}
}
</script>
<style lang="css">
.topIcon {
position: fixed;
bottom: 5rem;
right: 1rem;
width: 2rem;
height: 2rem;
border-radius: 50%;
background:url(../assets/images/gotop.png)center center no-repeat;
background-size: cover;
}
</style>


固定導(dǎo)航欄案例
<template>
<div>
<div id="topPart">頂部距離</div>
<div id="navBar" :class="{isFixed:istabBar}">固定導(dǎo)航欄</div>
<div id="mainPart">
<ul>
<li>內(nèi)容部分</li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</template>
<script>
export default {
data () {
return {
istabBar: false
}
},
methods: {
// 添加一個方法 兼容
handleScroll () {
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
// 固定導(dǎo)航欄
let topBar = document.querySelector("#topPart");
let navBar = document.querySelector("#navBar");
let mainPart = document.querySelector("#mainPart");
if (scrollTop > topBar.offsetHeight){
this.istabBar = true
mainPart.style.paddingTop = navBar.offsetHeight + "px";
} else {
this.istabBar = false
mainPart.style.paddingTop = "0px";
}
}
},
mounted () {
window.addEventListener('scroll', this.handleScroll); // Dom樹加載完畢
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll) // 銷毀頁面時清除
}
}
</script>
<style>
#topPart{
width: 100%;
height: 100px;
background-color: yellow;
}
.isFixed {
position: fixed;
top: 0;
z-index: 10;
}
#navBar {
width: 100%;
height: 50px;
background-color: red;
}
#mainPart {
width: 100%;
}
#mainPart ul {
width: 100%;
}
#mainPart ul li {
width: 100%;
height: 40px;
background-color: orange;
margin-bottom: 10px;
}
</style>

以上這篇vue 中固定導(dǎo)航欄的實(shí)例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue頁面中使用getElementsByClassName無法獲取元素的解決
這篇文章主要介紹了vue頁面中使用getElementsByClassName無法獲取元素的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
VUE開發(fā)分布式醫(yī)療掛號系統(tǒng)后臺管理頁面步驟
本文從整體上介紹Vue框架的開發(fā)流程,結(jié)合具體的案例,使用Vue框架調(diào)用具體的后端接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
vue+springmvc導(dǎo)出excel數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+springmvc導(dǎo)出excel數(shù)據(jù)的實(shí)現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
詳解vue beforeEach 死循環(huán)問題解決方法
這篇文章主要介紹了vue beforeEach 死循環(huán)問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
vue3清空let?arr?=?reactive([])的實(shí)現(xiàn)示例
本文主要介紹了vue3清空let?arr?=?reactive([])的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
使用Vue3實(shí)現(xiàn)一個簡單的思維導(dǎo)圖組件
思維導(dǎo)圖是一種用于表示信息、想法和概念的圖形化工具,本文將基于 Vue3和VueDraggable實(shí)現(xiàn)一個簡單的思維導(dǎo)圖組件,支持節(jié)點(diǎn)拖拽,編輯及節(jié)點(diǎn)之間的關(guān)系連接,希望對大家有所幫助2025-04-04

