clientHeight & offsetHeight & scrollHeight

scrollWidth/scrollHeight,offsetWidth/offsetHeight,clientWidth/clientHeight

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description scrollBottomAutoLoadMore
* @augments
* @example
* @link
*
*/ // utils
let log = console.log;
let error = console.error; let box = document.querySelector(`[data-dom="outbox"]`); // bottom: 556.59375
// height: 525.59375
// left: 135
// right: 405
// top: 31
// width: 270
// x: 135
// y: 31 // height: 525.59375 ~= 526
// ch = 526 // 1562 - 1036 = 526
// sh = 1562
// st = 1036 // ch = 526
// cw = 253
// oh = 526
// ow = 270
// sh = 1562
// sw = 253
// st = 1036 let isTriggerOnce = true; const scrollBottomAutoLoadMore = (debug = false) => {
for (let i = 0; i < 20; i++) {
let template = `<div class="innerbox" data-dom="innerbox">${i + 1}</div>`;
box.insertAdjacentHTML(`beforeend`, template);
}
let BoundingClientRect = box.getBoundingClientRect();
let ClientRects = box.getClientRects();
// log(`BoundingClientRect =`, BoundingClientRect);
// log(`ClientRects`, ClientRects);
// Event
box.addEventListener(`scroll`, (e) => {
// log(`e =`, e);
// log(`e.target =`, e.target);
// clientHeight, offsetHeight, scrollHeight
let ch = e.target.clientHeight;
let cw = e.target.clientWidth;
// log(`ch =`, ch);
// log(`cw =`, cw);
let oh = e.target.offsetHeight;
let ow = e.target.offsetWidth;
// log(`oh =`, oh);
// log(`ow =`, ow);
let sh = e.target.scrollHeight;
let sw = e.target.scrollWidth;
// log(`sh =`, sh);
// log(`sw =`, sw);
let st = e.target.scrollTop;
// log(`st =`, st);
let trigger = (sh - st) < (ch + 50) ? true : false;
let triggerValue = (sh - st) < (ch + 50);
if (trigger && isTriggerOnce) {
alert(`trigger loadMore function!`);
isTriggerOnce = false;
for (let i = 0; i < 10; i++) {
let template = `<div class="innerbox" data-dom="innerbox">${i + 1}</div>`;
box.insertAdjacentHTML(`beforeend`, template);
}
setTimeout(() => {
alert(`Ajax & fetch data success!`);
isTriggerOnce = true;
}, 1000);
} else {
// do nothing
// log(`sh =`, sh);
// log(`st =`, st);
// log(`ch =`, ch);
log(`(sh - st) =`, (sh - st));
log(`(ch - 50) =`, (ch + 50));
log(`triggerValue =`, triggerValue);
} // clientHeight: 526
// clientWidth: 346 // offsetHeight: 526
// offsetLeft: 182
// offsetTop: 31
// offsetWidth: 363 // scrollHeight: 1562
// scrollLeft: 0
// scrollTop: 1036
// scrollWidth: 346
});
// MouseEvent
// MouseEvent
box.addEventListener(`click`, (e) => {
// log(`e =`, e);
// log(`e.target =`, e.target);
// pageX, pageY, screenX, screenY, clientX, and clientY
let px = e.pageX;
let py = e.pageY;
// log(`px =`, px);
// log(`py =`, py);
// let sx = e.screenX;
// let sy = e.screenY;
// log(`sx =`, sx);
// log(`sy =`, sy); // let cx = e.clientX;
// let cy = e.clientY;
// log(`cx =`, cx);
// log(`cy =`, cy);
let lx = e.layerX;
let ly = e.layerY;
let mx = e.movementX;
let my = e.movementY;
let ox = e.offsetX;
let oy = e.offsetY;
// log(`lx =`, lx);
// log(`ly =`, ly);
// log(`mx =`, mx);
// log(`my =`, my);
// log(`ox =`, ox);
// log(`oy =`, oy);
});
}; window.addEventListener("DOMContentLoaded", (event) => {
console.log("DOM fully loaded and parsed");
scrollBottomAutoLoadMore();
}); // export default scrollBottomAutoLoadMore; // export {
// scrollBottomAutoLoadMore,
// };

new version

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description scrollBottomAutoLoadMore
* @augments
* @example
* @link
*
*/ // utils
let log = console.log;
let error = console.error; let box = document.querySelector(`[data-dom="outbox"]`); let isTriggerOnce = true; const scrollBottomAutoLoadMore = (debug = false) => {
for (let i = 0; i < 20; i++) {
let template = `<div class="innerbox" data-dom="innerbox">${i + 1}</div>`;
box.insertAdjacentHTML(`beforeend`, template);
}
// let BoundingClientRect = box.getBoundingClientRect();
// let ClientRects = box.getClientRects();
// log(`BoundingClientRect =`, BoundingClientRect);
// log(`ClientRects`, ClientRects);
// Event
box.addEventListener(`scroll`, (e) => {
// log(`e =`, e);
// log(`e.target =`, e.target);
// clientHeight, offsetHeight, scrollHeight
let ch = e.target.clientHeight;
// let cw = e.target.clientWidth;
// let oh = e.target.offsetHeight;
// let ow = e.target.offsetWidth;
let sh = e.target.scrollHeight;
let st = e.target.scrollTop;
let trigger = (sh - st) < (ch + 50) ? true : false;
let triggerValue = (sh - st) < (ch + 50);
if (trigger && isTriggerOnce) {
alert(`trigger loadMore function!`);
isTriggerOnce = false;
for (let i = 0; i < 10; i++) {
let template = `<div class="innerbox" data-dom="innerbox">${i + 1}</div>`;
box.insertAdjacentHTML(`beforeend`, template);
}
setTimeout(() => {
alert(`Ajax & fetch data success!`);
isTriggerOnce = true;
}, 1000);
} else {
// do nothing
log(`(sh - st) =`, (sh - st));
log(`(ch - 50) =`, (ch + 50));
log(`triggerValue =`, triggerValue);
}
});
// MouseEvent
box.addEventListener(`click`, (e) => {
// log(`e =`, e);
// log(`e.target =`, e.target);
// pageX, pageY, screenX, screenY, clientX, and clientY
let px = e.pageX;
let py = e.pageY;
// log(`px =`, px);
// log(`py =`, py);
let sx = e.screenX;
let sy = e.screenY;
// log(`sx =`, sx);
// log(`sy =`, sy);
let cx = e.clientX;
let cy = e.clientY;
// log(`cx =`, cx);
// log(`cy =`, cy);
let lx = e.layerX;
let ly = e.layerY;
// log(`lx =`, lx);
// log(`ly =`, ly);
let mx = e.movementX;
let my = e.movementY;
// log(`mx =`, mx);
// log(`my =`, my);
let ox = e.offsetX;
let oy = e.offsetY;
// log(`ox =`, ox);
// log(`oy =`, oy);
});
}; window.addEventListener("DOMContentLoaded", (event) => {
console.log("DOM fully loaded and parsed");
scrollBottomAutoLoadMore();
}); // export default scrollBottomAutoLoadMore; // export {
// scrollBottomAutoLoadMore,
// };

clientHeight, offsetHeight, scrollHeight,

https://stackoverflow.com/questions/21064101/understanding-offsetwidth-clientwidth-scrollwidth-and-height-respectively


pageX,clientX, screenX,

https://stackoverflow.com/questions/6073505/what-is-the-difference-between-screenx-y-clientx-y-and-pagex-y


MDN

https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight





getBoundingClientRect

https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect



getClientRects

https://developer.mozilla.org/en-US/docs/Web/API/Element/getClientRects



window.scrollX & window.scrollY

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollX

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY



.clientX


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description touchSwipe
* @augments
* @example
* @link
*
*/ const touchSwipe = (
dom = ``,
callback = () => console.log(`touch swipe callback`),
debug = false
) => {
let log = console.log;
let div = ``;
if (dom) {
div = document.querySelector(dom);
if (div) {
div.addEventListener("touchstart", handleTouchStart, false);
div.addEventListener("touchmove", handleTouchMove, false);
} else {
log(`dom can not be found!`);
return;
}
}
if (debug) {
log(`touchSwipe init OK!`);
}
let xDown = null;
function getTouches(evt) {
return evt.touches;
}
function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
};
function handleTouchMove(evt) {
if (!xDown) {
return;
}
let xUp = evt.touches[0].clientX;
let xDiff = xDown - xUp;
if (xDiff > 0) {
callback(true);
} else {
callback(false);
}
xDown = null;
};
}; export default touchSwipe; export {
touchSwipe,
};

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有️xgqfrms, 禁止转载 ️,侵权必究️!


clientHeight & offsetHeight & scrollHeight的更多相关文章

  1. JavaScript获取浏览器高度和宽度值(documentElement,clientHeight,offsetHeight,scrollHeight,scrollTop,offsetParent,offsetY,innerHeight)

    IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...

  2. 关于body/documentElement ---->clientHeight, offsetHeight, scrollHeight

    http://blog.csdn.net/woxueliuyun/article/details/8638427 http://blog.sina.com.cn/s/blog_9dd702d50101 ...

  3. clientHeight,offsetHeight,scrollHeight迷一样的三个值

    https://blog.csdn.net/qq_39083004/article/details/78498178 https://www.imooc.com/article/17571  推荐 o ...

  4. offsetHeight, clientHeight与scrollHeight的区别

      在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论.以下结论皆是在标准模式下测试通过的,没有测试quirk模式. clientHeight 大部分浏览器对 clientHe ...

  5. clientHeight,offsetHeight与scrollHeight的相关知识

    在html里,width与height是最常用也是最基础的两个属性,因此,在js里,我们也经常需要操作这两个属性.js关于这两个属性提供了client*,offset*与scroll*,很多同学搞不清 ...

  6. 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别

    二.也是平时经常用到的offsetheight 它返回的高度是内容高+padding+边框,但是注意哦,木有加margin哦,当然一般也木有啥需要把margin加进去的,以上代码为例,结果显示上图h2 ...

  7. 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别一览

    平时,不管在pc端页面还是移动端页面,因为我们一般很少会设置某个块的的高度,但是呢,我有时候有需要取到这些高度以便于我们方便进行判断和下一步的编写.一般这个时候我都是直接的获取一个块的高度.heigh ...

  8. offsetHeight,scrollHeight,clientHeight,scrollTop以及pageX,clientX,offsetX,screenX,offsetLeft,style.left等的区别以及使用详解

    一.写在前面 在阅读本文前,希望大家能针对每个属性亲手测试,网上现有的大量相关博客都有不等的概念错误,毕竟亲手实践才能更好的掌握这些概念. 1.pageX,clientX,screenX与offset ...

  9. clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系

    style.height 包括 元素的滚动条,不包括边框clientHeight 不包括元素的滚动条和边框,只有在没有元素的滚动条的情况下,style.height相等于clientHeightoff ...

随机推荐

  1. RocketMQ 的几个简单问题与答案

    1 单机版消息中心 一个消息中心,最基本的需要支持多生产者.多消费者,例如下: class Scratch { public static void main(String[] args) { // ...

  2. js基础(使用Canvas画图)

    HTML5的元素提供了一组JavaScript API,让我们可以动态地创建图形和图像. 图形是在一个特定的上下文中创建的,而上下文对象目前有两种.第一种是2D上下文,可以执行原始的绘图操作, 比如: ...

  3. Git基本概念,流程,分支,标签及常用命令

    Git基本概念,流程,分支,标签及常用命令 Git一张图 Git基本概念 仓库(Repository) 分支(Branch) Git工作流程 Git分支管理(branch) 列出分支 删除分支 分支合 ...

  4. JDK-7新特性,更优雅的关闭流-java try-with-resource语句使用

    前言 公司最近代码质量整改,需要对大方法进行调整,降低到50行以下,对方法的深度进行降低,然后有些文件涉及到流操作,很多try/catch/finally语句,导致行数超出规范值,使用这个语法可以很好 ...

  5. jvm学习第一天

    视频教程链接 第一部分-jvm初识 0.jvm概览图 JVM由三个主要的子系统构成 类加载子系统 运行时数据区(内存结构) 执行引擎运行时数据区(内存结构) 1.什么是jvm 定义: ①. JVM 是 ...

  6. Flink-v1.12官方网站翻译-P018-Event Time

    事件时间 在本节中,您将学习如何编写时间感知的Flink程序.请看一下及时流处理,了解及时流处理背后的概念. 关于如何在Flink程序中使用时间的信息请参考windowing和ProcessFunct ...

  7. 【STM32】无法下载程序

    错误: 使用keil MDK向STM32下载时出现各种错误 Internal command error.Error:Flash download failed.  Target DLL has be ...

  8. Vuejs 基础学习教程

    (四)构建基础进阶-env文件与环境设置 我们在实际开发中,我们一般会经历项目的开发阶段,测试阶段,和最终上线阶段,每个阶段对于项目代码的需要可能都有所不同,那我们怎么让它在不同阶段呈现不同的效果呢? ...

  9. mysql创建和使用数据库

    mysql连接和断开 mysql -h host -u user -p******** /*建议不要在命令行中输入密码,因为这样做会使其暴露给在您的计算机上登录的其他用户窥探*/ mysql -u u ...

  10. 三、Python基本数据类型

    一.基本算术运算(获取的结果是值) 1 a1=10 2 a2=20#初始赋值 3 a3=a1+a2 #结果30 4 a4=a2-a1 #结果10 5 a5=a1*a2 #结果200 6 a6=a2/a ...