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鉤子就會執(zhí)行
下面圖的意思是綁定一個“scroll”事件,一旦它被執(zhí)行對應(yīng)的this.handleScroll方法會被執(zhí)行

addEventListener() 方法,事件監(jiān)聽
你可以使用 removeEventListener() 方法來移除事件的監(jiān)聽。
語法:
element.addEventListener(event, function, useCapture);
第一個參數(shù)是事件的類型 (如 “click” 或 “scroll”).
第二個參數(shù)是事件觸發(fā)后調(diào)用的函數(shù)。
第三個參數(shù)是個布爾值用于描述事件是冒泡還是捕獲。該參數(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)文章
Element中使用ECharts的項(xiàng)目實(shí)踐
本文主要介紹了Element中使用ECharts的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
使用jenkins一鍵打包發(fā)布vue項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了使用jenkins一鍵打包發(fā)布vue項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
vue使用js-audio-recorder實(shí)現(xiàn)錄音功能
這篇文章主要為大家詳細(xì)介紹了vue如何使用js-audio-recorder實(shí)現(xiàn)錄音功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
vue實(shí)現(xiàn)數(shù)據(jù)控制視圖的原理解析
這篇文章主要介紹了vue如何實(shí)現(xiàn)的數(shù)據(jù)控制視圖的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
vue中ref引用操作DOM元素的實(shí)現(xiàn)
本文主要介紹了vue中ref引用操作DOM元素的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Vue實(shí)戰(zhàn)之vue登錄驗(yàn)證的實(shí)現(xiàn)代碼
本篇文章主要介紹了Vue實(shí)戰(zhàn)之vue登錄的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Vue中的v-model綁定修飾符的實(shí)現(xiàn)原理
v-model 是Vue.js中的一個重要指令,通過它我們可以輕松實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,本文介紹一些常用的 v-model 綁定修飾符,并解析它們的實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2024-01-01

