Vue实现详情页Header渐隐渐现效果
程序员文章站
2022-05-02 20:21:45
...
<template>
<div>
<div class="box"></div>
<!-- header-icon -->
<div class="header-abs" v-show="visiableAbs">
<div class="header-abs-back"><</div>
</div>
<!-- header-bar -->
<div class="header-fixed" v-show="!visiableAbs" :style="opacityStyle">
<div class="header-icons"><</div>
<div class="header-title">标题</div>
<div class="header-icons"></div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
visiableAbs: true,
opacityStyle: {
opacity: 0
}
}
},
methods: {
handleScroll() {
const top = document.documentElement.scrollTop || document.body.scrollTop
if (top > 60) {
let opacity = top / 140
opacity = opacity > 1 ? 1 : opacity
this.opacityStyle = {
opacity
}
this.visiableAbs = false
} else {
this.visiableAbs = true
}
}
},
mounted() {
window.addEventListener('scroll', this.handleScroll)
},
unmounted() {
window.removeEventListener('scroll', this.handleScroll)
}
}
</script>
<style scoped>
.box {
height: 1000px;
background-color: #fff;
}
.header-abs {
position: absolute;
left: 10px;
top: 10px;
width: 40px;
height: 40px;
line-height: 40px;
border-radius: 20px;
text-align: center;
border-radius: 40%;
background: rgba(0, 0, 0, .8)
}
.header-abs-back {
color: #fff;
font-size: 20px;
}
.header-fixed {
z-index: 2;
display: flex;
position: fixed;
top: 0;
left: 0;
right: 0;
height: 40px;
line-height: 40px;
text-align: center;
color: #fff;
background: #000;
font-size: 16px;
}
.header-icons {
width: 50px;
height: 100%;
font-size: 22px;
}
.header-title {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
}
</style>