vue 中固定導(dǎo)航欄的實(shí)例代碼
更新時(shí)間:2019年11月01日 10:33:54 作者:徹夜不歸
今天小編就為大家分享一篇vue 中固定導(dǎo)航欄的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
點(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: {
// 添加一個(gè)方法 兼容
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) // 銷毀頁(yè)面時(shí)清除
}
}
</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í)例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue2中,根據(jù)list的id進(jìn)入對(duì)應(yīng)的詳情頁(yè)并修改title方法
今天小編就為大家分享一篇vue2中,根據(jù)list的id進(jìn)入對(duì)應(yīng)的詳情頁(yè)并修改title方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解
這篇文章主要為大家介紹了vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue如何集成raphael.js中國(guó)地圖的方法示例
最近的數(shù)據(jù)統(tǒng)計(jì)項(xiàng)目中要用到中國(guó)地圖,也就是在地圖上動(dòng)態(tài)的顯示某個(gè)時(shí)間段某個(gè)省份地區(qū)的統(tǒng)計(jì)數(shù)據(jù),我們不需要flash,僅僅依靠raphael.js以及SVG圖像就可以完成地圖的交互操作。本文就給大家介紹了關(guān)于利用vue集成raphael.js中國(guó)地圖的相關(guān)資料,需要的朋友可以參考下。2017-08-08
如何使用VuePress搭建一個(gè)類型element ui文檔
這篇文章主要介紹了如何使用VuePress搭建一個(gè)類型element ui文檔,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

