Vue實現Header漸隱漸現效果的例項程式碼
阿新 • • 發佈:2020-11-06
新建header.vue元件
引入到父元件Detail.vue中
header.vue
通過router-link
標籤設定to屬性
為地址,實現點選返回首頁
tag標籤設為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"> 景點詳情 <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.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>
邏輯部分
呼叫activated鉤子函式,因為我們用了keepalive,所以頁面只要一被展示activated鉤子就會執行
下面圖的意思是繫結一個“scroll”事件,一旦它被執行對應的this.handleScroll方法會被執行
addEventListener() 方法,事件監聽
你可以使用 removeEventListener() 方法來移除事件的監聽。
語法:
element.addEventListener(event,function,useCapture);
第一個引數是事件的型別 (如 “click” 或 “scroll”).
第二個引數是事件觸發後呼叫的函式。
第三個引數是個布林值用於描述事件是冒泡還是捕獲。該引數是可選的。
注意:不要使用 “on” 字首。 例如,使用 “click”,而不是使用 “onclick”。
漸隱漸現效果
這裡用到了三元表示式,讓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"> 景點詳情 <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.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>
到此這篇關於Vue實現Header漸隱漸現效果的例項程式碼的文章就介紹到這了,更多相關Vue漸隱漸現效果內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!