当前位置: 首页 > news >正文

JS-第二十一天-尺寸位置

DOM元素操作基础

元素内容的操作:

let box = document.querySelector('.box')
// 读取内容
console.log(box.innerHTML);
// 写入内容
box.innerHTML = "新的内容"

样式的操作:

// 样式操作(只支持写入)
box.style.borderRadius = "30px"
// 读取样式(只能读取内联样式)
console.log(box.style.height);
console.log(box.style.padding);

元素尺寸系列属性

offset系列

offset包含了宽高+内填充+边框

offset的属性:

  • offsetWidth:元素总宽度

  • offsetHeight:元素总高度

  • offsetLeft:距离父元素左边距离

  • offsetTop:距离父元素顶部距离

    console.log("========offset=======")// offset系列 尺寸:宽高+内填充+边框console.log($box.offsetWidth);console.log($box.offsetHeight);// 位置console.log($box.offsetLeft);console.log($box.offsetTop);

client系列

client包含了宽高和内填充【不包括边框】

client的属性

  • clientWidth:可视区域宽度

  • clientHeight:可视区域高度

  • clientLeft:左边框宽度

  • clientTop:上边框宽度

    console.log("========client=======")// client系列 尺寸:宽高+内填充 [不包含边框]console.log($box.clientWidth);console.log($box.clientHeight);console.log($box.clientLeft);console.log($box.clientTop);

scroll系列

scroll包含了总滚动内容的宽高+内填充【不包含边框】

scroll的属性

  • scrollWidth:内容总宽度

  • scrollHeight:内容总高度

  • scrollLeft:水平滚动距离(可写)

  • scrollTop:垂直滚动距离(可写)

    console.log("========scroll=======")// scroll系列 尺寸:宽高+内填充+外填充console.log($box.scrollWidth);console.log($box.scrollHeight);// 滚动距离console.log($box.scrollLeft);console.log($box.scrollTop);

注意:除了scrollLeftscrollTop,所有属性都是只读的。


网页和窗

1 窗口尺寸

// 窗口可见区域
window.innerWidth    // 窗口宽度
window.innerHeight   // 窗口高度
document.documentElement.clientWidth   // HTML元素宽度
document.documentElement.clientHeight  // HTML元素高度

2 网页总尺寸

// 推荐使用html元素,兼容性问题时取最大值
document.documentElement.scrollWidth   // 网页总宽度
document.documentElement.scrollHeight  // 网页总高度
document.body.scrollWidth              // body总宽度
document.body.scrollHeight             // body总高度

3 滚动位置

// 滚动事件监听
window.onscroll = function () {// 现代浏览器推荐console.log(window.pageXOffset);  // 水平滚动距离console.log(window.pageYOffset);  // 垂直滚动距离// 兼容性写法console.log(document.documentElement.scrollTop);console.log(document.body.scrollTop);
}

滚动控制

平滑滚动到指定位置一共有两种方法:

// 方法1:使用scrollTo
window.scrollTo({top: 0,behavior: "smooth"
})// 方法2:使用scrollIntoView
element.scrollIntoView({behavior: "smooth"
})

回到顶部:

toTop.onclick = function () {window.scrollTo({top: 0,behavior: "smooth"})
}

toTop是设置的类名。


触底判断

触底检测逻辑

window.onscroll = function () {let topHeight = Math.floor(document.documentElement.scrollTop)  // 滚动距离let winHeight = window.innerHeight                              // 窗口高度let pageHeight = document.documentElement.scrollHeight          // 网页总高度// 判断是否触底(预留50px缓冲)if (topHeight + winHeight >= pageHeight - 50) {console.log("到底了");// 执行触底后的操作}
}

楼层滚动导航

1 点击导航跳转

lis.forEach((li, i) => {li.onclick = function () {// 方法1:使用scrollTowindow.scrollTo({top: boxs[i].offsetTop,behavior: "smooth"})// 方法2:使用scrollIntoViewboxs[i].scrollIntoView({behavior: "smooth"})}
})

2 滚动时更新导航状态

window.onscroll = function () {let topNum = Math.ceil(window.pageYOffset)for (let i = 0; i < boxs.length; i++) {if (topNum >= boxs[i].offsetTop && i != index) {lis[index].classList.remove("active")  // 移除当前激活状态lis[i].classList.add("active")         // 添加新的激活状态index = i                              // 更新索引}}
}

总结:

1.尺寸属性的区别:

offset包含边框、client不包含边框、scroll包含滚动的内容

2.滚动控制:

scrollTo:滚动到指定坐标

scrollIntoview:滚动到元素可见

3.兼容性处理:

现代常用的浏览器推荐使用window.pageXOffset/pageYOffsetdocument.documentElement.scrollTop

考虑旧浏览器的兼容性考虑使用document.documentElement.scrollTop document.body.scrollTop

http://www.xdnf.cn/news/1244341.html

相关文章:

  • Android UI 组件系列(十一):RecyclerView 多类型布局与数据刷新实战
  • AI 对话高效输入指令攻略(四):AI+Apache ECharts:生成各种专业图表
  • 【学习笔记】Manipulate-Anything(基于视觉-语言模型的机器人自动化操控系统)
  • 【09】C++实战篇——C++ 生成静态库.lib 及 C++调用lib,及实际项目中的使用技巧
  • javacc学习笔记 02、JavaCC 语法描述文件的格式解析
  • Druid手写核心实现案例 实现一个简单Select 解析,包含Lexer、Parser、AstNode
  • k8s常见问题
  • (论文速读)RMT:Retentive+ViT的视觉新骨干
  • 20250805问答课题-实现TextRank + 问题分类
  • 力扣热题100------21.合并两个有序链表
  • 8.高斯混合模型
  • k8s简介
  • 数据集相关类代码回顾理解 | np.mean\transforms.Normalize\transforms.Compose\xxx.transform
  • Claude Code六周回顾
  • 补:《每日AI-人工智能-编程日报》--2025年7月29日
  • steam Rust游戏 启动错误,删除sys驱动,亲测有效。
  • 机器学习(13):逻辑回归
  • 昇思学习营-模型推理和性能优化学习心得
  • ShowDoc与Docmost对比分析:开源文档管理工具的选择指南
  • 【QT】常⽤控件详解(四)常用显示类控件类 Label LCDNumber ProgressBar Calendar Widget
  • [Oracle] TO_NUMBER()函数
  • HTTPS有哪些优点
  • 【OS】操作系统概述
  • 蓝桥杯----AT24C02
  • 机器学习(12):拉索回归Lasso
  • Docker-07.Docker基础-数据卷挂载
  • 基于SpringBoot的OA办公系统的设计与实现
  • 小鹏汽车前端面经
  • 深度解析:CPU 与 GPU 上的张量运算,为何“快”与“慢”并非绝对?
  • Flutter 对 Windows 不同版本的支持及 flutter_tts 兼容性指南