mounted() {
window.addEventListener(“scroll”, this.handleScroll);
},
backTop() {
cancelAnimationFrame(this.timer)
const self = this
self.timer = requestAnimationFrame(function fn() {
const oTop = document.body.scrollTop || document.documentElement.scrollTop
if (oTop > 0) {
document.body.scrollTop = document.documentElement.scrollTop = oTop - 50
self.timer = requestAnimationFrame(fn)
} else {
cancelAnimationFrame(self.timer)
}
})
},
handleScroll() {
this.visible = window.pageYOffset
//滚动距离
var scrollTop =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop;
// 当导航栏滚动距离大于头部图片高度实现吸顶效果
if (scrollTop > 100) {
//为导航栏加上navBarFixed类,实现透明度随滚动改变,并且吸顶改变导航栏样式
this.navBarFixed = true;
this.opacity = Math.abs(Math.round(scrollTop)) / 140;
this.style = {background: rgba(51, 51, 51,${this.opacity})
}
} else {
this.navBarFixed = false;
this.style = ‘’;
}
},