MDN上概念

scrollTop:获取或设置一个元素的内容垂直滚动的像素数。

scrollHeight:一个元素内容高度的度量,包括由于溢出导致的视图中不可见内容。

clientHeight:元素内部的高度(单位像素),包含内边距,但不包括水平滚动条、边框和外边距。

只看概念,似懂非懂,还是要自己测试一遍才知道,做个记录,以下为谷歌浏览器测试。

css

  1. .box {
  2. height: 500px;
  3. width: 300px;
  4. overflow: scroll;
  5. color: #fff;
  6. }

html

  1. <div class="box">
  2. <div>鸟语花香</div>
  3. <div>鸟语花香</div>
  4. ...
  5. <div>鸟语花香</div>
  6. </div>

来看看scrollTop、scrollHeight与clientHeight都是多少

  1. console.log("scrollTop:"+div.scrollTop)//页面内容的滚动距离
  2. console.log("scrollHeight:"+div.scrollHeight)//滚动内容的总大小
  3. console.log("clientHeight:"+div.clientHeight)//滚动容器的高

与magin的关系?css加上margin: 30px;

结果如下:



结论:没关系

与padding的关系?css加上padding: 10px;

结果如下:



结论:有关系

scrollHeight=高度height(滚动内容)+上下padding

clientHeight=高度height(滚动容器)+上下padding

与border的关系?css加上border: 14px;

结果如下:

结论:没关系

执行oncroll:

  1. var div = document.querySelector(".box")
  2. var istan = true //加个开关,否则会append好多p标签
  3. div.onscroll = function () {
  4. //距离底部300时执行
  5. if (div.scrollHeight - div.scrollTop < div.clientHeight + 300) {
  6. if (istan) {
  7. var p = document.createElement("p")
  8. p.style.color = "#f40"
  9. p.innerText = '快到底了'
  10. div.appendChild(p)
  11. istan = false
  12. }
  13. }
  14. var hue = 360 - 360 * (div.scrollTop + div.clientHeight) / div.scrollHeight
  15. //改变背景颜色
  16. div.style.backgroundColor = 'hsl(' + hue + ',60%,70%)'

如下图:

判断是否滚动到底

element.scrollHeight等于element.scrollTop+element.clientHeight的时候说明滚动到底了

  1. element.scrollHeight - element.scrollTop === element.clientHeight

注:快滑到最后的时候scrollHeight多了53,是添加的p标签的高度(height+margin)

CSS hsl() 函数

hsl(hue, saturation, lightness)

hue 色相 0 (或 360) 为红色, 120 为绿色, 240 为蓝色

saturation 饱和度 色彩的纯度,越高色彩越纯,低则逐渐变灰,取0-100%的数值

lightness 亮度 取0-100%,增加亮度,颜色会向白色变化;减少亮度,颜色会向黑色变化。

实现效果

jq操作scroll

scrollLeft()

设置或返回匹配元素相对滚动条左侧的偏移。

scrollTop()

设置或返回匹配元素相对滚动条顶部的偏移。

scrollTop、scrollHeight与clientHeight的更多相关文章

  1. scrollTop,scrollHeight,clientTop,clientHeight,offsetTop,offsetHeight实际意义 及 计算方式 附实例说明

    一.滚动距离.高度 scrollTop scrollLeft scrollHeight scrollWidth 二.相对位置.距离 offsetTop offsetLeft offsetHeight ...

  2. 各种位置和高度计算:.position()、.offset()、.outerHeight()、.scrollTop、.scrollHeight、.clientHeight

    1..position()和.offset() jquery的.position()获取相对于最近的position为relative或absolute的父元素的偏移,返回.position().le ...

  3. 【日常总结】scrollTop、scrollHeight与clientHeight的重要关系

    前言 在做一个需求的时候涉及懒加载,百度了一下,发现scrollTop.scrollHeight与clientHeight这三个元素起到了重要作用,以前做过类似demo但是时间过太久忘记了,现在已经完 ...

  4. offsetTop/offsetHeight scrollTop/scrollHeight 的区别

    offsetTop/offsetHeight   scrollTop/scrollHeight  这几个属性困扰了我N久,这次一定要搞定. 假设 obj 为某个 HTML 控件. obj.offset ...

  5. scrollHeight、clientHeight、offsetHeight、scrollTop等的定义以及图解

    开发中经常遇到使用scrollHeight.scrollTop.clientHeight.offsetHeight等的情况,网上有众多关于这些属性的解释,但是并不全面和直观,现在将这些属性结合图例整理 ...

  6. JavaScript--clientX,clientY、pageX,pageY、offsetLeft,offsetTop/offsetWidth,offsetHeight、scrollLeft,scrollTop/scrollWidth,scrollHeight、clientHeight,clientWidth区别

    /*在事件的内部console.dir(event)*/ /** * 事件对象event * clientX/clientY 获取鼠标基于浏览器窗口(可视区域的坐标位置)全兼容 * * pageX/p ...

  7. scrollWidth,offsetWidth,clientWidth,width;scrollHeight,offsetHeight,clientHeight,height;offsetTop,scrollTop,top;offsetLeft,scrollLeft,left还有谁

    题中的那么多属性让人头都大了,他们到底是什么意思?不同浏览器的实现是一样的吗?以下所有结论来自chrome版本 53.0.2785.89 (64-bit)和firefox版本52.0.2,操作系统ub ...

  8. scrollLeft/scrollTop/scrollHeight

    scrollHeight  :  It includes the element's padding, but not its border or margin.This property will ...

  9. offsetHeight、scrollHeight、clientHeight、height

    对这几项进行彻底研究. 第一步:纯净div,没有margin,padding,border,height设置为200px. 添加滚动条,overflow:scroll,结果div的高度被压缩,因为被滚 ...

随机推荐

  1. Redis慢查询配置和优化

    目录 一.介绍 二.参数配置 sql动态配置 配置文件设置 三.sql操作 四.优化 一.介绍 慢查询只记录redis执行时间,并不记录redis服务到客户端之间的网络问题. 超过多少毫秒的才被记录 ...

  2. Typora软件使用教程

    Typora软件的使用教程 Typora软件简介 Typora是一款轻便简洁的Markdown编辑器,支持即时渲染技术,这也是与其他Markdown编辑器最显著的区别.当然重点是免费. Typora编 ...

  3. 利用 trap 在 docker 容器优雅关闭前执行环境清理

    当一个运行中的容器被终止时,如何能够执行一些预定义的操作,比如在容器彻底退出之前清理环境.这是一种类似于 pre stop 的钩子体验.但 docker 本身无法提供这种能力,本文结合 Linux 内 ...

  4. 人工水母搜索算法—matlab代码

    clc clear foj = @ Sphere; Lb = -100; % 搜索空间下界 Ub = 100; % 搜索空间上界 N_iter = 1000; % 最大迭代次数 n_pop = 50; ...

  5. 解决Vulnhub靶机分配不到IP问题

    没法找到他的 ip,可能是网卡配置问题 (之前打开 .ova 文件默认联网方式是桥接,改成NAT) 在开机选择的时候,摁 e 编辑一下 把 ro 改成 rw single init=/bin/bash ...

  6. 拖动条形图设置任务关联(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 仅仅是知悉了四种任务关联,那只是纸上谈兵,要把这四种关联真正用到Project上才行,所以我们就要来设置任务关联了. 这是 ...

  7. 谷歌protobuf(protocol-buffers)各种开发语言数据类型转换说明

    官方文档:https://developers.google.cn/protocol-buffers/docs/proto proto2 proto3

  8. SpringCloud(四) config

    Spring Cloud Config 在分布式系统中,尤其是当我们的分布式项目越来越多,每个项目都有自己的配置文件,对配置文件的统一管理就成了一种需要,而 Spring Cloud Config 就 ...

  9. ubuntu下载源码clang + llvm+lldb 编译+安装

    [本文可能涉及到Ubuntu安装以下工具:] A.g++ B.gcc C.make D.cmake E.clang(10.0.1)(必须) F.llvm(10.0.1)(必须) G.lldb(10.0 ...

  10. 【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 原地变负 日期 题目地址:https://le ...