欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果的實(shí)例代碼

 更新時(shí)間:2020年11月05日 10:58:28   作者:鐵錘妹妹的學(xué)習(xí)日記  
這篇文章主要介紹了Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

新建header.vue組件

引入到父組件Detail.vue中

在這里插入圖片描述

header.vue

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

在這里插入圖片描述

<template>
 <div class="header">
 <router-link tag="div" to="/" class="header-abs">
 <div class="iconfont header-abs-back">&#xe685;</div>
 </router-link>
 <div class="header-fixed">
 <div class="header">
 景點(diǎn)詳情
 <router-link to="/">
  <div class="iconfont header-fixed-back">&#xe685;</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,所以頁(yè)面只要一被展示activated鉤子就會(huì)執(zhí)行
下面圖的意思是綁定一個(gè)“scroll”事件,一旦它被執(zhí)行對(duì)應(yīng)的this.handleScroll方法會(huì)被執(zhí)行

在這里插入圖片描述

addEventListener() 方法,事件監(jiān)聽(tīng)
你可以使用 removeEventListener() 方法來(lái)移除事件的監(jiān)聽(tīng)。

語(yǔ)法:

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">&#xe685;</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">&#xe685;</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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue.config.js配置報(bào)錯(cuò)解決辦法(可能是與webpack混淆)

    vue.config.js配置報(bào)錯(cuò)解決辦法(可能是與webpack混淆)

    在Vue.js開(kāi)發(fā)過(guò)程中,vue.config.js文件是用于配置項(xiàng)目的,特別是對(duì)于開(kāi)發(fā)環(huán)境的設(shè)置,這篇文章主要給大家介紹了關(guān)于vue.config.js配置報(bào)錯(cuò)解決的相關(guān)資料,可能是與webpack混淆,需要的朋友可以參考下
    2024-06-06
  • Vuex中的State使用介紹

    Vuex中的State使用介紹

    今天小編就為大家分享一篇關(guān)于Vuex中的State使用介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 詳解Vscode中使用Eslint終極配置大全

    詳解Vscode中使用Eslint終極配置大全

    這篇文章主要介紹了詳解Vscode中使用Eslint終極配置大全,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Vue登錄功能的實(shí)現(xiàn)流程詳解

    Vue登錄功能的實(shí)現(xiàn)流程詳解

    本文主要介紹了Vue實(shí)現(xiàn)登錄功能全套詳解(含封裝axios),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • vue3+ts實(shí)現(xiàn)樹(shù)形組件(菜單組件)

    vue3+ts實(shí)現(xiàn)樹(shù)形組件(菜單組件)

    本文主要介紹了vue3+ts實(shí)現(xiàn)樹(shù)形組件(菜單組件),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • vue el-table實(shí)現(xiàn)多選框回填的示例代碼

    vue el-table實(shí)現(xiàn)多選框回填的示例代碼

    摘要:Vue多選框回填是實(shí)現(xiàn)表單數(shù)據(jù)高效處理的常見(jiàn)需求,本文主要介紹了vue el-table實(shí)現(xiàn)多選框回填的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Vue使用Multiavatarjs生成自定義隨機(jī)頭像的案例

    Vue使用Multiavatarjs生成自定義隨機(jī)頭像的案例

    這篇文章給大家介紹了Vue項(xiàng)目中使用Multiavatarjs生成自定義隨機(jī)頭像的案例,文中通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-10-10
  • vuex中能直接修改state嗎

    vuex中能直接修改state嗎

    當(dāng)我們使用vuex的時(shí)候,時(shí)不時(shí)能看到“更改Vuex中的store中的狀態(tài)唯一辦法就是提交mutations”,但是有沒(méi)有試想過(guò),我們不提交mutations其實(shí)也能修改state的值?答案是可以的,下面通過(guò)本文介紹下vuex修改state值的方法,感興趣的朋友一起看看吧
    2022-11-11
  • 解決removeEventListener 無(wú)法清除監(jiān)聽(tīng)的問(wèn)題

    解決removeEventListener 無(wú)法清除監(jiān)聽(tīng)的問(wèn)題

    這篇文章主要介紹了解決removeEventListener 無(wú)法清除監(jiān)聽(tīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 一文帶你上手Vue新的狀態(tài)管理Pinia

    一文帶你上手Vue新的狀態(tài)管理Pinia

    Vuex 作為一個(gè)老牌 Vue 狀態(tài)管理庫(kù),大家都很熟悉了,Pinia 是 Vue.js 團(tuán)隊(duì)成員專門為 Vue 開(kāi)發(fā)的一個(gè)全新的狀態(tài)管理庫(kù),本文就來(lái)講講它的具體使用吧
    2023-04-04

最新評(píng)論