MDN上概念

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

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

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

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

css

.box {
height: 500px;
width: 300px;
overflow: scroll;
color: #fff;
}

html

<div class="box">
<div>鸟语花香</div>
<div>鸟语花香</div>
...
<div>鸟语花香</div>
</div>

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

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

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

结果如下:



结论:没关系

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

结果如下:



结论:有关系

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

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

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

结果如下:

结论:没关系

执行oncroll:

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

如下图:

判断是否滚动到底

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

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. 【模型推理】量化实现分享一:详解 min-max 对称量化算法实现

      欢迎关注我的公众号 [极智视界],回复001获取Google编程规范   O_o   >_<   o_O   O_o   ~_~   o_O   大家好,我是极智视界,本文剖析一下 m ...

  2. [BUUCTF]REVERSE——[FlareOn4]login

    [FlareOn4]login 附件 步骤: 是个网页,直接打开,查看网页源码 百度了几个函数 charCodeAt(0)是返回当前字符的Unicode 编码 String.fromCharCode返 ...

  3. 解决iwrite无法粘贴问题

    使用iwrite写作的时候,会遇到系统禁止粘贴的障碍 按F12键,再按F1键,在Disable JavaScrip前面的方框里打上勾就可以愉快的粘贴了

  4. CF20B Equation 题解

    Content 解方程 \(ax^2+bx+c=0\). 数据范围:\(-10^5\leqslant a,b,c\leqslant 10^5\). Solution 很明显上求根公式. 先来给大家推推 ...

  5. python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()

    import time  获取当前时间: 指定字符串格式:time.strftime("%Y-%m-%d %H:%M:%S") 当前时间戳:time.time() 当前时间元组格式 ...

  6. Jquery监控audio单选框选中事件(实际通过click)

    $('input:radio[name="pathType"]').click(function(){ var checkValue = $('input:radio[name=& ...

  7. VS2013 or up version +常用插件

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 下载地址: https://github.com/mohistH/vs2013_extens ...

  8. python3实战之字幕vtt与字母srt的相互转换

    关于 0.本文将介绍一个字幕格式vtt与srt相互转换的py脚本. 1.代码大部分出自: https://www.cnblogs.com/BigJ/p/vtt_srt.html 2.但是自己针对上面的 ...

  9. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  10. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...