博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
滚动相关知识点总结
阅读量:7113 次
发布时间:2019-06-28

本文共 3077 字,大约阅读时间需要 10 分钟。

获取当前滚动高度

也就是页面顶部超出视口的高度。

function getScrollTop() {  return document.body.scrollTop || document.documentElement.scrollTop;}

document.documentElement获取到的是html标签。IE支持,chrome目前也支持。

document.body获取到的是body标签。chrome/ff支持。

页面滚动的总高度
function getScrollHeight() {  return document.body.scrollHeight || document.documentElement.scrollHeight;}
视口高度
function getClientHeight() {  return Math.max(document.documentElement.clientHeight, window.innerHeight || 0);}

window.innerHeight在IE8-不支持。并且会受到initial-scale缩放的影响。因此需要取一个max值。

如何判断滚动到了顶部

scrollTop的值为0时,则滚动到了顶部。

if (getScrollTop() === 0) {  // 滚动到了顶部}
如何判断滚动到了最低部

当滚动高度scrollTop与视口高度clientHeight之和,大于等于总高度scrollHeight时,则表示滚动到了底部。

var curHeight = getScrollTop() + getClientHeight();if (curHeight >= getScrollHeight()) {  // 滚动到了底部}
如何判断滚动方向
var preTop = 0;var curTop = 0;var timer = null;document.addEventListener('scroll', () => {  clearTimeout(timer);  curTop = getScrollTop();  if (curTop > preTop) {    console.log('向下滚动');  }   if (curTop < preTop) {    console.log('向上滚动');  }  timer = setTimeout(() => {    preTop = curTop;  }, 10);  }, !1);
函数节流

降低函数的触发频率。

原理是通过闭包与setTimeout,缓存一个timer值。 当timer值不为null时,阻止操作重复执行。每一次操作执行完毕,将timer设置为null。这样下一次操作将不会受到阻止。如果我们需要调节执行频率,只需要改变setTimeout的延迟时间即可。

const throttle = (fn, delay) => {  let timer = null;  let isFrist = true;  // 第一次直接执行  return function() {    const args = [].slice.call(arguments);    const self = this;    if (timer) {      return false;    }    if (isFrist) {      fn.apply(self, args);      isFrist = false;    }    timer = setTimeout(() => {      clearTimeout(timer);      timer = null;      fn.apply(self, args);    }, delay || 500)  }}

demo代码

var preTop = 0;var curTop = 0;var timer = null;document.addEventListener('scroll', throttle(() => {  clearTimeout(timer);  curTop = getScrollTop();  console.log(document.documentElement.scrollTop, document.documentElement.scrollHeight);  if (getScrollTop() + getClientHeight() >= getScrollHeight()) {    console.log('到底了兄弟.');  }  if (curTop > preTop) {    console.log('向下滚动');  }   if (curTop < preTop) {    console.log('向上滚动');  }  timer = setTimeout(() => {    preTop = curTop;  }, 10);}, 300), !1);console.log('视口高度: ', window.innerHeight, document.documentElement.clientHeight);function getScrollTop() {  return document.body.scrollTop || document.documentElement.scrollTop;}function getScrollHeight() {  return document.body.scrollHeight || document.documentElement.scrollHeight;}function getClientHeight() {  return Math.max(document.documentElement.clientHeight, window.innerHeight || 0);}function log() {  console.log('xxx');}function throttle(fn, delay) {  let timer = null;  let isFrist = true;  // 第一次直接执行  return function() {    const args = [].slice.call(arguments);    const self = this;    if (timer) {      return false;    }    if (isFrist) {      fn.apply(self, args);      isFrist = false;    }    timer = setTimeout(() => {      clearTimeout(timer);      timer = null;      fn.apply(self, args);    }, delay || 500)  }}
应用场景

滚动加载更多 | 滚动判断某些元素的显示与隐藏 | 视差滚动特效 等。

一次需求中需要用到这些知识点,做了一个小小的总结以便记忆查询,欢迎大家补充,多多交流,一起进步。

clipboard.png

转载地址:http://ujwel.baihongyu.com/

你可能感兴趣的文章
.net MVC使用Aspose.Words 获取文本域获取文档
查看>>
即使没有翅膀,心。。。。。。也要飞翔
查看>>
VMware Esxi5.5中嵌套虚拟机的网络设置方法
查看>>
[转载] 七龙珠第一部——第044话 危机四起
查看>>
[转载] 民兵葛二蛋——第28集
查看>>
Wiki上的Ue4文件结构以及命名规范
查看>>
图片滚动代码
查看>>
ubuntu下安装chrome
查看>>
初阶html学习总结(一)(转)
查看>>
Mysterious Antiques in Sackler Museum(判断长方形)
查看>>
PHP字符串的总结
查看>>
百度搜索附近加盟店等基于LBS云搜索功能的实现
查看>>
89. Gray Code
查看>>
差分约束心得
查看>>
ZooKeeper开发手册中文翻译(转)
查看>>
HDU 5805
查看>>
leetcode shell
查看>>
JavaScript的第一次小结
查看>>
Google Maps 如何接地气地本地化 “两轮车模式” ?
查看>>
转-Java基础全面解析——Java语言基础
查看>>