vue 判断滚动条向上向下滚动

曹え 5811 发布于:2022-07-18 09:01:33

向下滚动滚动条的时候隐藏导航,向上滚动的时候显示导航

    export default {
        data(){
            return{
                       i = 0
               }
           },
       mounted () {
            window.addEventListener('scroll', this.handleScroll, true);  
             // 监听(绑定)滚轮 滚动事件
        },
        methods:{
         handleScroll(){
            // 页面滚动距顶部距离
            var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 
                      document.body.scrollTop
            var scroll = scrollTop - this.i;
            this.i = scrollTop;
            if(scroll<0){
                console.log('up')
            }else{
                console.log('down')
            }
        },
}


觉得有用请点个赞吧!
1 1187