一、渐隐渐现的效果说明:
当我们选择详情页面后在详情页面顶部显示返回,在向上滑动时出现另外一个标头,在向下滑动时恢复原样,如下图所示
二、代码展示及说明
<template>
<div>
// 1、这部分是左边的那个小箭头 用于返回到首页的
<router-link
tag="div"
to="/"
class="header-abs"
// 这是用于判断顶部样式和这个返回键的切换
v-show="showAbs"
>
<div class="iconfont header-abs-back"></div>
</router-link>
// 2、隐藏显示的导航栏
<div
class="header-fixed"
v-show="!showAbs"
// 动态绑定样式,动态改变样式
:style="opacityStyle"
>
<router-link to='/'>
<div class="iconfont header-fixed-back"></div>
</router-link>
景点详情
</div>
</div>
</template>
<script>
export default {
name: 'DetailHeader',
data () {
return {
showAbs: true,
// 3、透明度先设置为0
opacityStyle: {
opacity: 0
}
}
},
methods: {
// 4、定义滚动方法实现渐隐渐现的效果
handleScroll () {
// 滚动条距离顶部的距离 依次来计算透明度的百分比
const top = document.documentElement.scrollTop
if (top > 60) {
let opacity = top / 140
opacity = opacity > 1 ? 1 : opacity
this.opacityStyle = { opacity }
this.showAbs = false
} else {
this.showAbs = true
}
}
},
// 5、定义activated方法监听滚动事件,并给其绑定方法
activated () {
// 监听页面滚动
window.addEventListener('scroll', this.handleScroll)
}
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.header-abs
position: absolute
left: .2rem
top: .2rem
width: .8rem
height: .8rem
line-height: .8rem
border-radius: .4rem
text-align: center
background: rgba(0, 0, 0, 0.8)
.header-abs-back
color: #fff
font-size: .4rem
.header-fixed
position: fixed
top: 0
left: 0
right: 0
height: $headerHeight
line-height: $headerHeight
text-align: center
color: #fff
background: $bgColor
font-size: .32rem
.header-fixed-back
position: absolute
top: 0
left: 0
width: .64rem
text-align: center
font-size: .4rem
color: #fff
</style>