请教大佬VUE中使用 document.getElementById('xxx').onscroll不生效宿舍什么原因呢

代码如下

几个console.log都没触发

还有个问题请问大佬,请问window.onscroll=function(e){} 这个的解绑是让 window.onscroll 直接等于空就行吗

getMore(Boolean){
      var self = this;
            if(Boolean){
                console.log(document.getElementById('SimpleList'))
                //以下
                document.getElementById('SimpleList').onscroll= function (e){
                
                    console.log('111')
                    var htmlHeight =document.body.scrollHeight || document.documentElement.scrollHeight;
                    var clientHeight =window.innerHeight || document.documentElement.clientHeight;
                    var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                    let validH = htmlHeight - clientHeight;
                    let result = ((scrollTop / validH) * 100).toFixed(2);
                    console.log(result)
                    if (self.nullData) {
                        return;
                    } else if (result == 100) {
                        self.isUpData = true;
                        self.page += 1;
                        self.getList();
                    }
                };
阅读 8.6k
3 个回答

1、应该是在触发前绑定事件,而不是触发后,否则会错过
2、onscroll等同于addEventListener,赋值为空函数还是会执行。要移除事件绑定,需要执行的是removeEventListener

你打断点看下Boolean是true吗

这样就可以,离开页面的时候也能解除绑定

图片上传 后有问题

图片描述

clipboard.png

推荐问题
宣传栏