zoukankan      html  css  js  c++  java
  • vue 中scroll事件不触发问题

    在vue项目中需要监听滚动条滚动的位置,结果写了scroll监听事件就是不生效,最后查资料发现是页面有样式设置了over-flow:scroll,去掉之后完美解决.(页面样式中存在over-flow:scroll,over-flow:auto的时候scroll事件监听不起作用,所以排查问题的时候首先需要考虑当前页面样式中是否存在over-flow);

    export default {

    methods: {

    handleScroll() {
    var that = this;
    //滚动条在y轴上的滚动距离
    var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
    //文档的总高度
    var documentScrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    //浏览器窗口的高度
    var getWindowHeight = document.documentElement.clientHeight || document.body.clientHeight;
    if (scrollTop > 10) {
    that.isclass = true;
    that.iswhite= true;
    } else {
    console.log('xiaoyu ');
    that.isclass = false;
    that.iswhite= false;
    }

    //滚动条距离页面顶部的距离大于一屏时触发该方法

    if (scrollTop + getWindowHeight == documentScrollHeight) {

        that.loadMore();
    }
    }

    },

    mounted() {   //监听scroll事件
    window.addEventListener('scroll', this.handleScroll);
    },
    destroyed() { //页面离开后销毁,防止切换路由后上一个页面监听scroll滚动事件会在新页面报错问题


    window.removeEventListener('scroll', this.handleScroll)
    }
    }

  • 相关阅读:
    jmeter单一接口测试
    mac os下载安装jmeter
    十、集成使用redis
    Java之Poi导出Excel文档
    134. Gas Station (Array; DP)
    53. Maximum Subarray (Array; DP)
    36. Valid Sudoku (Array; HashTable)
    37. Sudoku Solver (Array;Back-Track)
    52. N-Queens II (Array; Back-Track)
    51. N-Queens (Array; Back-Track, Bit)
  • 原文地址:https://www.cnblogs.com/h5it/p/10193822.html
Copyright © 2011-2022 走看看