需求:页面滚动改变顶部导航样式
原样式:导航透明,显示底部广告位
滚动后样式:出现黑色透明遮罩
代码实现:
<a-layout-header :style="{ position: 'fixed', zIndex: 1, width: '100%',background:topNav}">
//这里是业务代码
</a-layout-header>
<script>
import { defineComponent, ref } from 'vue';
import homePage from '../../components/HomePage/HomePage.vue';
export default defineComponent({
setup() {
var topNav =ref('rgba(0, 0, 0,0)');
return {
topNav,
selectedKeys: ref(['1']),
};
},
components: {
homePage,
},
mounted(){
window.addEventListener('scroll',this.handleScroll)
},
methods:{
handleScroll(){
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop ;// 滚动条偏移量
this.topNav = scrollTop > 64 ? 'rgba(0, 0, 0,0.85)' : 'rgba(0, 0, 0,0)';
console.log(this.topNav);
}
}
});
</script>