Uni-app 或 VUE 顶部固定

单页应用中,增加了“监听”(addEventListener)事件后,需要在离开页面时“销毁”(removeEventListener)。

mounted () {
    window.addEventListener('scroll', this.handleScroll)
},
onHide() {
    window.removeEventListener("scroll", this.handleScroll);
},
methods: {
  handleScroll () {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
        let offsetTop = document.querySelector('.dynamic-fixed-item').offsetTop
        if (scrollTop > offsetTop) {
        this.isFixed = true
        } else {
        this.isFixed = false
        }
  }
}
.isFixed{
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background-color: #fff;
  z-index: 2;
}

 1,434 total views,  8 views today

Revisions

Comments are closed.