一文搞懂offset、client、scroll系列及案例

目录

一、offset

1-1、offset系列属性

1-2、offset与style区别

1-3、案例

1-3-1、计算鼠标在盒子内的坐标

1-3-2、拖动模态框

二、client

2-1、client系列属性

三、scroll

3-1、scroll系列属性

3-2、案例

3-2-1、滚动页面一定距离后固定侧边栏


一、offset

offset是偏移量,使用offset系列相关属性可以动态得到元素的位置(偏移)、大小等,可获得:

  • 元素距离带有定位父元素的位置
  • 元素自身的宽度、高度

注意:返回的数值不带单位

1-1、offset系列属性

offset系列属性作用
element.offsetParent返回作为该元素带有定位的父级元素(如果父级都没有定位则返回body)
element.offsetTop返回元素相对带有定位父级元素上方的偏移
element.offsetLeft返回元素相对带有定位父元素左边框的偏移
element.offsetWidth返回自身包括padding、边框、内容区的宽度
element.offsetHeight返回自身包括padding、边框、内容区的高度

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>* {margin: 0;padding: 0;}.father {position: relative;width: 200px;height: 200px;background-color: pink;margin: 150px;}.son {width: 100px;height: 100px;background-color: purple;margin-left: 45px;}.w {height: 200px;width: 200px;background-color: skyblue;margin: 0 auto 200px;padding: 10px;border: 15px solid orange;}</style>
</head><body><div class="father"><div class="son"></div></div><div class="w"></div><script>// offset 系列var father = document.querySelector('.father');var son = document.querySelector('.son');// 1.可以得到元素的偏移 位置 返回的不带单位的数值  console.log(father.offsetTop);// 150console.log(father.offsetLeft);// 150// 它以带有定位的父亲为准// 没有父亲或者父亲没有定位 则以 body 为准,为195console.log(son.offsetLeft);// 45var w = document.querySelector('.w');// 2.可以得到元素的大小 宽度和高度 是包含padding + border + width // 200 + 15*2 + 10*2console.log(w.offsetWidth);// 250console.log(w.offsetHeight); // 250// 3. 返回带有定位的父亲 否则返回的是bodyconsole.log(son.offsetParent); // 返回带有定位的父亲 否则返回的是bodyconsole.log(son.parentNode); // 返回父亲 是最近一级的父亲 亲爸爸 不管父亲有没有定位</script>
</body></html>

1-2、offset与style区别

  1. offset
  • offset系列获得的数值是没有单位的
  • offsetWidth包含padding+border+width
  • offsetWidth等属性是只读属性,只能获取不能赋值
  1. style
  • style.width获得的是带有单位的字符串
  • style.width获得不包含padding和border的值
  • style.width是可读可写属性,可以获取也可以赋值

所以:

  • 想要获取元素大小位置,用offset更合适
  • 想要给元素更改值,用style更合适
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>.box {width: 200px;height: 200px;background-color: pink;padding: 10px;}</style>
</head><body><div class="box" style="width: 200px;"></div><script>// offset与style的区别var box = document.querySelector('.box');console.log(box.offsetWidth); // 200 + 10*2 = 220console.log(box.style.width); // 200pxbox.style.width = '300px'; // 可利用style去设置值</script>
</body></html>

1-3、案例

1-3-1、计算鼠标在盒子内的坐标

原理: 使用鼠标在页面中的坐标(pageX,pageY) - 盒子在页面中的偏移量(offsetLeft,offsetTop)

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>.box {width: 300px;height: 300px;background-color: pink;margin: 200px;}</style>
</head><body><div class="box"></div><script>// 我们在盒子内点击, 想要得到鼠标距离盒子左右的距离。// 首先得到鼠标在页面中的坐标( e.pageX, e.pageY)// 其次得到盒子在页面中的距离(box.offsetLeft, box.offsetTop)// 用鼠标距离页面的坐标减去盒子在页面中的距离, 得到 鼠标在盒子内的坐标const box = document.querySelector('.box');box.addEventListener('mousemove', function (e) {// console.log(e.pageX);// console.log(e.pageY);// console.log(box.offsetLeft);const x = e.pageX - this.offsetLeft;const y = e.pageY - this.offsetTop;this.innerHTML = 'x坐标是' + x + ' y坐标是' + y;})</script>
</body></html>

1-3-2、拖动模态框

原理

  1. 当鼠标按下时(mousedown)拿到鼠标在模态框内的坐标下x,y(原理为1-3-1)

  2. 当鼠标拖动模态框开始移动时(mousemove),分别给模态框的left与top赋值:

    • 模态框的left值为 鼠标距离页面左侧的距离 (pageX)- 鼠标在模态框的横坐标(x)
    • 模态框的top值为 鼠标距离页面顶部的距离(pageY)- 鼠标在模态框的纵坐标(y)
  3. 当鼠标抬起时(mouseup),移除鼠标移动事件

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}a {text-decoration: none;color: #000;}.show-modal {width: 100%;text-align: center;font-size: 24px;cursor: pointer;}.login {/*刚开始隐藏起来*/display: none;position: fixed;width: 520px;height: 290px;border: 1px solid #ccc;left: 50%;top: 50%;transform: translate(-50%, -50%);background: #fff;box-shadow: 0 0 20px #ddd;z-index: 9999;}.login-title {position: relative;width: 100%;height: 40px;line-height: 40px;text-align: center;font-size: 18px;cursor: move;}.login-title span {position: absolute;width: 40px;height: 40px;right: -20px;top: -30px;border-radius: 50%;font-size: 12px;background: #fff;border: 1px solid #ccc;}.login-content {padding: 10px;height: 180px;}.login-content-input {display: flex;}.login-content-input label {width: 100px;text-align: right;}.login-content-input input {width: 350px;height: 35px;line-height: 35px;border: 1px solid #ccc;margin-bottom: 20px;}.login-btn {width: 100%;height: 50px;line-height: 50px;display: block;text-align: center;font-size: 18px;border-top: 1px solid #ccc;}.login-mask {display: none;width: 100%;height: 100%;position: fixed;top: 0;left: 0;background-color: rgba(0, 0, 0, .3);}</style>
</head><body><div class="show-modal">点击展示模态框</div><!--模态框--><div class="login"><div class="login-title">用户登录<span><a class="close-btn" href="#">关闭</a></span></div><div class="login-content"><div class="login-content-input"><label>用户名:</label><input type="text" placeholder="请输入用户名"></div><div class="login-content-input"><label>密码:</label><input type="password" placeholder="请输入密码"></div></div><div><a href="#" class="login-btn">登录</a></div></div><!--模态框遮罩--><div class="login-mask"></div>
</body>
<script>const showLogin = document.querySelector('.show-modal');const login = document.querySelector('.login');const mask = document.querySelector('.login-mask');const closeBtn = document.querySelector('.close-btn');const title = document.querySelector('.login-title');// 点击展示模态框showLogin.addEventListener('click', function () {mask.style.display = 'block'; // 展示遮罩login.style.display = 'block'; // 展示模态窗})// 点击关闭按钮关闭模态框closeBtn.addEventListener('click', function () {mask.style.display = 'none';login.style.display = 'none';})// 拖拽模态框, 按住标题区域开始拖拽title.addEventListener('mousedown', function (e) {// 1 先获取一下鼠标在模态框内的坐标// 鼠标在页面上的坐标 - 模态框距离页面顶部/上部的距离const x = e.pageX - login.offsetLeft;const y = e.pageY - login.offsetTop;// 鼠标移动的监听事件加到document上,方式移动过快甩出模态框document.addEventListener('mousemove', move);document.addEventListener('mouseup', function () {document.removeEventListener('mousemove', move);});// 模态框现在的位置 = 鼠标在页面上的坐标 - 鼠标在模态框内的坐标function move(e) {// !!特别注意要加上pxlogin.style.left = e.pageX - x + 'px';login.style.top = e.pageY - y + 'px';}});</script></html>

二、client

使用client系列的相关属性可获取元素可视区的相关信息,可动态得到元素的边框大小、元素大小等。

2-1、client系列属性

client系列属性作用
element.clientTop返回元素上边框的大小
element.clientLeft返回元素左边框的大小
element.clientWidth返回自身包括padding、内容区的宽度,不包含边框
element.clientHeight返回自身包括padding、内容区的高度,不包含边框

注意:

  • client系列获取的内容也不包含单位

  • clientWidth、clientHeight与offsetWidth、offsetHeight不同的是:

    • offsetWidthoffsetHeight获取的宽高为:padding + 内容区 + 边框
    • clientWidthclientHeight获取的宽高为:padding + 内容区
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>div {width: 200px;height: 200px;background-color: skyblue;border: 10px solid orange;padding: 10px;}</style>
</head><body><div></div><script>// client 宽度 和我们offsetWidth 最大的区别就是 不包含边框var div = document.querySelector('div');console.log(div.clientWidth); // 200 + 10* 20 = 220console.log(div.clientTop); // 10</script>
</body></html>

三、scroll

使用scroll系列的线管属性可以动态得到元素的大小、滚动距离等。

3-1、scroll系列属性

scroll系列属性作用
element.scrollTop返回被卷上去的上侧距离
element.scrollLeft返回被卷去的左侧距离
element.scrollWidth返回自身的实际宽度,不包含边框
element.scrollHeight返回自身的实际高度,不包含边框

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>div {width: 200px;height: 200px;background-color: skyblue;border: 10px solid orange;padding: 10px;overflow: auto;}</style>
</head><body><div>我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容 我是内容</div><script>// scroll 系列var div = document.querySelector('div');console.log(div.scrollHeight); // 314 内容的实际高度console.log(div.clientHeight); // 200 + 10* 2 = 220// scroll滚动事件当我们滚动条发生变化会触发的事件div.addEventListener('scroll', function () {console.log(div.scrollTop);})</script>
</body></html>

 

3-2、案例

3-2-1、滚动页面一定距离后固定侧边栏

实现效果:侧边栏刚开始为相对定位,当页面滚动到一定距离后改为绝对定位并显示返回顶部按钮

原理:

  1. 首先确定页面需滚动的距离(滚动多少后侧边栏固定),以及侧边栏需要固定的位置
  2. 给页面添加监听页面滚动的事件,在事件中获取页面被卷去的距离(页面被卷去的距离通过:window.pageYOffset、window.pageXOffset获取)
  3. 如果卷去的距离大于等于设定的距离那么就固定侧边栏,将侧边栏固定到所需要固定的位置
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>.slider-bar {position: absolute;left: 50%;top: 300px;margin-left: 600px;width: 45px;height: 130px;background-color: pink;}.w {width: 1200px;margin: 10px auto;}.header {height: 150px;background-color: purple;}.banner {height: 250px;background-color: skyblue;}.main {height: 1000px;background-color: yellowgreen;}span {display: none;position: absolute;bottom: 0;}</style>
</head><body><div class="slider-bar"><span class="goBack">返回顶部</span></div><div class="header w">头部区域</div><div class="banner w">banner区域</div><div class="main w">主体部分</div><script>//1. 获取元素const sliderbar = document.querySelector('.slider-bar');const banner = document.querySelector('.banner');// banner.offestTop 就是被卷去头部的大小 一定要写到滚动的外面const bannerTop = banner.offsetTop// 当我们侧边栏固定定位之后应该变化的数值const sliderbarTop = sliderbar.offsetTop - bannerTop;// 获取main 主体元素const main = document.querySelector('.main');const goBack = document.querySelector('.goBack');const mainTop = main.offsetTop;// 2. 页面滚动事件 scrolldocument.addEventListener('scroll', function () {// console.log(11);// window.pageYOffset 页面被卷去的头部// 3 .当我们页面被卷去的头部大于等于了 172 此时 侧边栏就要改为固定定位if (window.pageYOffset >= bannerTop) {sliderbar.style.position = 'fixed';sliderbar.style.top = sliderbarTop + 'px';} else {sliderbar.style.position = 'absolute';sliderbar.style.top = '300px';}// 4. 当我们页面滚动到main盒子,就显示 goback模块if (window.pageYOffset >= mainTop) {goBack.style.display = 'block';} else {goBack.style.display = 'none';}})</script>
</body></html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1543231.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【WRF工具】服务器上使用conda安装NCL

【WRF工具】服务器上使用conda安装NCL NCL概述使用conda下载NCL安装conda安装NCL另&#xff1a;当使用NCL时&#xff0c;则需要激活ncl_stable环境 参考 NCL概述 NCAR Command Language&#xff08;NCL&#xff09; 是由美国大气研究中心&#xff08;NCAR&#xff09;推出的一…

Spring Boot 中实现任务后台处理的几种常见方式

​ 博客主页: 南来_北往 系列专栏&#xff1a;Spring Boot实战 前言 在现代应用程序中&#xff0c;后台处理对于处理发送电子邮件、处理文件、生成报告等任务至关重要。 Spring Boot 提供了多种机制来高效地实现后台任务。本文探讨了在 Spring Boot 中处理后台处理的各…

大数据新视界 --大数据大厂之 Vue.js 与大数据可视化:打造惊艳的数据界面

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

java节假日工具类,判断一个日期是否是法定节假日

java节假日工具类&#xff0c;判断一个日期是否是法定节假日 1.HolidayUtil工具类2.工具类生成的日期json文件3.结果展示 无需链接数据库&#xff0c;无需手写节假日集合列表 1.HolidayUtil工具类 import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.data…

0-10V 电压转光纤

型号&#xff1a;MS-F155-VM(CE /ISO9001&#xff09; 功能概述 MS-F155-VM是将0-10V电压转为光纤信号的模块&#xff0c;分发送和接收两个设备发送模块将电流或者电压信号转变为光信号&#xff0c;通过光纤传输&#xff0c;接收端将光信号还原为电流或者电压信号。可以延长通信…

2024源代码加密软件分享TOP10丨保护源代码安全很重要!

在如今的数字时代&#xff0c;源代码是企业的核心资产之一。无论是开发软件、应用程序&#xff0c;还是自动化系统&#xff0c;源代码都是技术的根基&#xff0c;决定了公司的核心竞争力。然而&#xff0c;源代码泄露或被盗可能会给企业带来巨大的安全风险和经济损失。因此&…

03-Docker下载加速

03-Docker下载加速 docker下载加速 方式1&#xff1a;使用 网易数帆、阿里云等容器镜像仓库进行下载。 网易数帆官网&#xff1a;https://sf.163.com/ 例如&#xff0c;下载网易数帆镜像中的mysql。&#xff08;网易数帆的地址为 hub.c.163.com&#xff0c;网易数帆对dockerh…

光控资本:沪指涨0.72%,煤炭、银行板块拉升,车路云概念活跃

23日早盘&#xff0c;沪指盘中强势上扬&#xff0c;深证成指亦走高&#xff0c;场内超3100股飘红。 到午间收盘&#xff0c;沪指涨0.72%报2756.39点&#xff0c;深证成指涨0.58%&#xff0c;创业板指微涨0.09%&#xff0c;上证50指数涨0.73%&#xff1b;两市估计成交3657亿元。…

TLV解码 - 华为OD统一考试(E卷)

2024华为OD机试&#xff08;E卷D卷C卷&#xff09;最新题库【超值优惠】Java/Python/C合集 题目描述 TLV编码是按 [Tag Length Value] 格式进行编码的&#xff0c;一段码流中的信元用Tag标识&#xff0c;Tag在码流中唯一不重复&#xff0c;Length表示信元Value的长度&#xff…

不敲一行代码!助你快速搭建属于自己的官网博客!-VitePress保姆级教程

文章目录 前言项目搭建首页修改项目配置 前言 我们在阅读官方文档时&#xff0c;经常看到一些项目的文档非常简约精美&#xff0c;并且布局高度相似&#xff0c;其实这些官网是基于vitepress搭建&#xff0c;例如&#xff1a; Vite官方文档 Vue-Use SnowAdmin 这些官网…

从零到一:如何用Ollama和OpenUI构建强大的AI模型库

搭建开源大模型平台的步骤与模型介绍 在这篇文章中&#xff0c;我将分享如何在Windows上使用Ollama和OpenUI搭建开源大模型平台的步骤&#xff0c;并介绍我所部署的几个模型及其擅长的领域。 目录 搭建开源大模型平台的步骤与模型介绍一、搭建平台步骤1. 安装Ollama2. 安装Ope…

VScode配置连接远程服务器configure ssh Hosts

VScode配置连接远程服务器&#xff0c;具体步骤 一、点击VScode左下脚这两个∟的按钮 二、点击完上面的按钮后&#xff0c;出现如下的下拉选项&#xff0c;选择“Connect to Host” 三、选择“Connect to Host”后&#xff0c;下拉选项会更新&#xff0c;选择“Configure SSH …

影刀RPA实战:java结合影刀同步采购订单数据

1.实战目标 本次实战我们用java语言结合影刀&#xff0c;实现从自用ERP系统同步订单到旺店通中&#xff0c;在工作中&#xff0c;有时候我们的运营数据不是直接在旺店通ERP中操作&#xff0c;比如我们有自己的ERP&#xff0c;完成一些特定的内部工作后&#xff0c;再把数据同步…

18937 阿克曼(Ackmann)函数

### 思路 1. **递归定义**&#xff1a;根据阿克曼函数的定义&#xff0c;使用递归来计算函数值。 2. **递归终止条件**&#xff1a; - 当 m 0 时&#xff0c;返回 n 1&#xfffd;&#xfffd; - 当 m > 0 且 n 0 时&#xff0c;返回 ackermann(m - 1, 1)。 - 当…

QT窗口无法激活弹出问题排查记录

问题背景 问题环境 操作系统: 银河麒麟V10SP1qt版本 : 5.12.12 碰见了一个问题应用最小化,然后激活程序窗口无法弹出 这里描述一下代码的逻辑,使用QLocalServer实现一个单例进程,具体的功能就是在已存在一个程序A进程时,再启动这个程序A,新的程序A进程会被杀死,然后激活已存…

Python 从入门到实战25(模块)

我们的目标是&#xff1a;通过这一套资料学习下来&#xff0c;通过熟练掌握python基础&#xff0c;然后结合经典实例、实践相结合&#xff0c;使我们完全掌握python&#xff0c;并做到独立完成项目开发的能力。 上篇文章我们讨论了类继承的相关知识。今天我们将学习一下模块的…

照片写真记录摄影作品记录网站源码

完美适应iPad&#xff0c;平板&#xff0c;手机竖屏不支持lazy&#xff0c;横屏可以&#xff0c;但建议使用平板查看效果&#xff0c; 有服务器直接上传解压使用&#xff0c;环境nginxphp&#xff0c; 没有服务器也没关系&#xff0c;可以直接使用html

模组差分包,可能是你远程升级失败的罪魁祸首!

也许我们已经习惯生活里的问题接连不断。。。但当收到客户的问题反馈&#xff0c;还是会心头一紧&#xff01; 最近有客户反馈在乡村里频繁出现掉线的情况。 我们赶紧排查&#xff1a;换货、换SIM卡&#xff0c;发现只有去年5月22号采购的那批模块在客户环境附近会出现掉线的…

开放式蓝牙耳机哪个品牌好用?行业顶尖五款开放式耳机别错过!

开放式蓝牙耳机哪个品牌好用&#xff1f;行业顶尖五款开放式耳机别错过&#xff01; 随着开放式耳机的流行&#xff0c;越来越多的用户开始青睐这种类型的耳机。尽管有些网友将开放式耳机称为“智商税”&#xff0c;但我相信&#xff0c;对于真正体验过的人来说&#xff0c;它…

基于Python的自然语言处理系列(14):TorchText + biGRU + Attention + Teacher Forcing

在前几篇文章中&#xff0c;我们探索了序列到序列&#xff08;seq2seq&#xff09;模型的基础&#xff0c;并通过使用双向GRU和上下文向量改进了模型的表现。然而&#xff0c;模型仍然依赖一个固定的上下文向量&#xff0c;这意味着它必须从整个源句中压缩信息&#xff0c;导致…