• 注册
当前位置:1313e > vue >正文

原生js实现触底加载下一页(2不引用VUE)

定义一个scroll容器
javascript
  var data =[1, 2, 3, 4];function render() {document.getElementById('app').innerHTML = '';let tempHtml = ''data.forEach(item => {tempHtml += `  
${item}
`;})app.innerHTML = tempHtml;}window.onload = _ => {console.log(app);render();app.addEventListener('scroll', e => {const { scrollTop: upOverHeight, clientHeight, scrollHeight: allHeight } = app;if (upOverHeight + clientHeight + 10 > allHeight) {console.log('滚动到底')data.push(Math.random())data.push(Math.random())data.push(Math.random())render();}console.log(data)console.warn(app.scrollHeight, app.clientHeight, app.scrollTop);});};
样式
  #app {background-color: #fafafa;width: 500px;height: 500px;overflow: auto;box-shadow: 0 0 8px #aaaaaa;}.block {background-color: white;width: 480px;line-height: 200px;height: 200px;margin: 10px 0;text-align: center;}

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录
相关推荐