Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果的實(shí)例代碼
新建header.vue組件
引入到父組件Detail.vue中

header.vue
通過router-link標(biāo)簽設(shè)置to屬性為地址,實(shí)現(xiàn)點(diǎn)擊返回首頁
tag標(biāo)簽設(shè)為div,就有了div的屬性

<template>
<div class="header">
<router-link tag="div" to="/" class="header-abs">
<div class="iconfont header-abs-back"></div>
</router-link>
<div class="header-fixed">
<div class="header">
景點(diǎn)詳情
<router-link to="/">
<div class="iconfont header-fixed-back"></div>
</router-link>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'DetailHeader'
}
</script>
<style lang="scss" scoped>
@import '~styles/varibles.scss';
.header-abs {
position: absolute;
left: 0.2rem;
top: 0.2rem;
width: 0.8rem;
height: 0.8rem;
line-height: 0.8rem;
text-align: center;
border-radius: 0.4rem;
background: rgba(0, 0, 0, 0.8);
.header-abs-back {
color: #fff;
font-size: 0.4rem;
}
}
.header-fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
height: $HeaderHeight;
line-height: $HeaderHeight;
text-align: center;
color: #fff;
background: $bgColor;
.header-fixed-back {
position: absolute;
top: 0;
left: 0;
color: #fff;
width: 0.64rem;
text-align: center;
font-size: 0.4rem;
}
}
</style>
邏輯部分
調(diào)用activated鉤子函數(shù),因?yàn)槲覀冇昧?strong>keepalive,所以頁面只要一被展示activated鉤子就會(huì)執(zhí)行
下面圖的意思是綁定一個(gè)“scroll”事件,一旦它被執(zhí)行對應(yīng)的this.handleScroll方法會(huì)被執(zhí)行

addEventListener() 方法,事件監(jiān)聽
你可以使用 removeEventListener() 方法來移除事件的監(jiān)聽。
語法:
element.addEventListener(event, function, useCapture);
第一個(gè)參數(shù)是事件的類型 (如 “click” 或 “scroll”).
第二個(gè)參數(shù)是事件觸發(fā)后調(diào)用的函數(shù)。
第三個(gè)參數(shù)是個(gè)布爾值用于描述事件是冒泡還是捕獲。該參數(shù)是可選的。
注意:不要使用 “on” 前綴。 例如,使用 “click” ,而不是使用 “onclick”。
漸隱漸現(xiàn)效果

這里用到了三元表達(dá)式,讓opacity最大值只能是1

F12審查元素可看到style被添加到div上了

<template>
<div class="header">
<router-link tag="div" to="/" class="header-abs" v-show="showAbs">
<div class="iconfont header-abs-back"></div>
</router-link>
<div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
<div class="header">
景點(diǎn)詳情
<router-link to="/">
<div class="iconfont header-fixed-back"></div>
</router-link>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'DetailHeader',
data() {
return {
showAbs: true,
opacityStyle: {
opacity: 0
}
}
},
methods: {
handleScroll() {
console.log('scroll')
// console.log(document.documentElement.scrollTop)
const top = document.documentElement.scrollTop
if (top > 60) {
let opacity = top / 140
// 讓 opacity 最大值只能是 1
opacity = opacity > 1 ? 1 : opacity
this.opacityStyle = { opacity }
this.showAbs = false
} else {
this.showAbs = true
}
}
},
activated() {
window.addEventListener('scroll', this.handleScroll)
},
deactivated() {
window.removeEventListener('scroll', this.handleScroll)
}
}
</script>
<style lang="scss" scoped>
@import '~styles/varibles.scss';
.header-abs {
position: absolute;
left: 0.2rem;
top: 0.2rem;
width: 0.8rem;
height: 0.8rem;
line-height: 0.8rem;
text-align: center;
border-radius: 0.4rem;
background: rgba(0, 0, 0, 0.8);
.header-abs-back {
color: #fff;
font-size: 0.4rem;
}
}
.header-fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
height: $HeaderHeight;
line-height: $HeaderHeight;
text-align: center;
color: #fff;
background: $bgColor;
.header-fixed-back {
position: absolute;
top: 0;
left: 0;
color: #fff;
width: 0.64rem;
text-align: center;
font-size: 0.4rem;
}
}
</style>
到此這篇關(guān)于Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果的實(shí)例代碼的文章就介紹到這了,更多相關(guān)Vue漸隱漸現(xiàn)效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.config.js配置報(bào)錯(cuò)解決辦法(可能是與webpack混淆)
在Vue.js開發(fā)過程中,vue.config.js文件是用于配置項(xiàng)目的,特別是對于開發(fā)環(huán)境的設(shè)置,這篇文章主要給大家介紹了關(guān)于vue.config.js配置報(bào)錯(cuò)解決的相關(guān)資料,可能是與webpack混淆,需要的朋友可以參考下2024-06-06
vue3+ts實(shí)現(xiàn)樹形組件(菜單組件)
本文主要介紹了vue3+ts實(shí)現(xiàn)樹形組件(菜單組件),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
vue el-table實(shí)現(xiàn)多選框回填的示例代碼
摘要:Vue多選框回填是實(shí)現(xiàn)表單數(shù)據(jù)高效處理的常見需求,本文主要介紹了vue el-table實(shí)現(xiàn)多選框回填的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
Vue使用Multiavatarjs生成自定義隨機(jī)頭像的案例
這篇文章給大家介紹了Vue項(xiàng)目中使用Multiavatarjs生成自定義隨機(jī)頭像的案例,文中通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10
解決removeEventListener 無法清除監(jiān)聽的問題
這篇文章主要介紹了解決removeEventListener 無法清除監(jiān)聽的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

