1.offset系列

// offset 系列
var father = document.querySelector('.father');
var son = document.querySelector('.son');
// 1.可以得到元素的偏移 位置 返回的不带单位的数值
console.log(father.offsetTop);
console.log(father.offsetLeft);
// 它以带有定位的父亲为准 如果么有父亲或者父亲没有定位 则以 body 为准
console.log(son.offsetLeft);
var w = document.querySelector('.w');
// 2.可以得到元素的大小 宽度和高度 是包含padding + border + width
console.log(w.offsetWidth);
console.log(w.offsetHeight);
// 3. 返回带有定位的父亲 否则返回的是body
console.log(son.offsetParent); // 返回带有定位的父亲 否则返回的是body
console.log(son.parentNode); // 返回父亲 是最近一级的父亲 亲爸爸 不管父亲有没有定位
ript>

offset和style有关属性的比较

1.是否可读写:

拿offsetWidth和style.width来说,我们执行一下以下代码:

        .box {
width: 200px;
height: 200px;
background-color: pink;
padding: 10px;
}

我们把盒子的长度和宽度都设置为了200px,log一下二者:

        var box = document.querySelector('.box');
console.log(box.offsetWidth);
// 220
console.log(box.style.width);
// 200px

从中我们得到了3个信息

1.二者都是可读的。

2.style会带单位。

并且根据规范对其赋值的时候也应该带单位,所以当我们通过offset获得的值再使用style时应在其后方加上px;

3.offset会把padding包含在内。

再分别对其进行赋值操作:

        var box = document.querySelector('.box');
box.offsetWidth = '300';
console.log(box.offsetWidth); //
console.log(box.style.width); //200px
box.style.width = '300px';
console.log(box.offsetWidth); //
console.log(box.style.width); //300px

发现前者并不能对元素的属性进行写的操作,这也意味着如果我们如果通过offset得到的属性而要对页面元素进行操作的时候,应该选用与style配合的方法;

这里有个动态拖拽框的小案例,有兴趣的朋友不妨试试。

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
} .login_header {
width: 100%;
/* height: 30px; */
line-height: 30px;
text-align: center;
font-size: 24px;
margin-top: 8px;
} a {
text-decoration: none;
color: #000;
} .login {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 512px;
height: 280px;
background-color: #fff;
border: 1px solid #cccccc;
box-shadow: 0px 0px 20px #dddddd;
display: none;
} .login_title {
margin-top: 20px;
text-align: center;
font-size: 20px;
cursor: move;
} .login_uname,
.login_password {
margin: 20px 0;
} .login_uname input,
.login_password input {
outline: none;
text-indent: 5px;
height: 38px;
width: 350px;
border: 1px solid #cccccc;
} .login_button {
text-align: center;
line-height: 40px;
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 256px;
height: 40px;
font-size: 16px;
border: 1px solid #cccccc;
} label {
text-align: right;
display: inline-block;
width: 100px;
height: 35px;
padding-right: 10px;
/* width: 20px; */
} .login_close a {
position: absolute;
top: -15px;
right: -15px;
display: block;
width: 35px;
height: 35px;
line-height: 35px;
text-align: center;
font-size: 14px;
background-color: #fff;
border-radius: 50%;
box-shadow: 0px 0px 20px #dddddd;
} .mask {
position: fixed;
top: 0;
width: 100%;
height: 100%;
background-color: #B2B2B2;
z-index: -99;
display: none;
}
</style>
</head> <body>
<div class="login_header"><a href="javascript:;" id="enter_login">点击,弹出登录框</a></div>
<div class="login">
<div class="login_title" id="title">登录会员</div>
<div class="login_uname">
<label for="uname">用户名:</label>
<input type="text" placeholder="请输入用户名" id="uname">
</div>
<div class="login_password">
<label for="password">登录密码:</label>
<input type="password" placeholder="请输入密码" id="password">
</div>
<a class="login_button" href="javascript:;">登录会员</a>
<div class="login_close">
<a href="javascript:;" id="close">关闭</a>
</div>
</div>
<div class="mask"></div>
<script>
var title = document.querySelector('#title');
var enter_login = document.querySelector('#enter_login');
var login = document.querySelector('.login');
var login_uname = document.querySelector('.login_uname').querySelector('input');
var login_password = document.querySelector('.login_password').querySelector('input');
var close_button = document.querySelector('#close');
var mask = document.querySelector('.mask');
enter_login.addEventListener('click', function() {
login.style.display = 'block';
mask.style.display = 'block'; })
close_button.addEventListener('click', function() {
login.style.display = 'none';
mask.style.display = 'none';
}) function onFocus(input) {
input.addEventListener('focus', function() {
this.style.borderColor = 'skyblue';
})
input.addEventListener('blur', function() {
this.style.borderColor = '#ccc';
})
}
onFocus(login_uname);
onFocus(login_password);
title.addEventListener('mousedown', function(e) {
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop; function move(e) {
var moveX = e.pageX - x;
var moveY = e.pageY - y;
login.style.left = moveX + 'px';
login.style.top = moveY + 'px';
}
title.addEventListener('mousemove', move);
title.addEventListener('mouseup', function() {
title.removeEventListener('mousemove', move);
}) })
</script>
</body> </html>

2.鼠标当前的坐标及其在某盒子内的坐标

在事件的函数中传入e,通过e.pageX,e.pageY获取当前鼠标在页面中的位置坐标。

这里我们综合上面的offset来计算一下鼠标在当前盒子中的坐标:

思路:盒子中的x坐标为鼠标再页面中距离左端的距离e.pagex减去盒子距离左端的长度box.offsetLeft。

        window.addEventListener('load', function() {
var div = document.querySelector('div');
div.addEventListener('mousemove', function(e) {
var x = e.pageX - div.offsetLeft;
var y = e.pageY - div.offsetTop;
console.log(x);
console.log(y);
div.innerHTML = '鼠标在盒子内的X坐标是' + x + ' y坐标是' + y;
})
})

3.Client系列

我们使用 client 系列的相关属性来获取元素可视区的相关信息。通过 client 系列的相关属性可以动态的得到该元素的边框大小、元素大小等。

用法与offset一致,同样含有Left Top Width Height区别在于client不包含边框!

4.Scroll系列

这里我们用一张图来解释一下

scrollTop是指被卷曲的长度,Height是文字的总长度。

Offset等一些类似属性的使用的更多相关文章

  1. JavaScript学习笔记5 之 计时器 & scroll、offset、client系列属性 & 图片无缝滚动

    一.计时器 setInterval ( 函数/名称 , 毫秒数 )表示每经过一定的毫秒后,执行一次相应的函数(重复) setTimeout ( 函数/名称 , 毫秒数 ) 表示经过一定的毫秒后,只执行 ...

  2. JQ的offset().top与JS的getBoundingClientRect区别详解,JS获取元素距离视窗顶部可变距离

     壹 ❀ 引 我在 JQ的offset().top与js的offsetTop区别详解 这篇博客中详细分析了JQ方法offset().top与JS属性offsetTop的区别,并得出了一条offset( ...

  3. OFFSET约束(OFFSET IN 和OFFSET OUT)

    OFFSET 的意思是偏移.对于同步时序电路来说,数据和时钟之间的偏移量是必须要关注的.OFFSET IN和OUT分别对应的是输入和输出FPGA数据和时钟之间的偏移关系,本文将分析这一种关系.阅读本文 ...

  4. 攻城狮在路上(壹) Hibernate(三)--- 属性访问、命名策略、派生属性、指定包名等

    一.hibernate访问持久化类属性的策略: 在<property>元素中的access属性用于指定Hibernate访问持久化类属性的方式. 常见的方式如下: 1.property:默 ...

  5. CSS属性之 -- overflow

    overflow可以实现隐藏超出对象内容,同时也有显示与隐藏滚动条的作用,overflow属性有四个值:visible (默认), hidden, scroll, 和auto.同样有两个overflo ...

  6. jQuery RemoveAttr(checked)之后再Attr(checked)属性无效果的原因分析

    jQuery中attr()和prop()在修改checked属性时的区别 投稿:whsnow 字体:[增加 减小] 类型:转载   使用语句$.attr('checked',true),将复选框的属性 ...

  7. C++学习之路—继承与派生(一):基本概念与基类成员的访问属性

    (本文根据<c++程序设计>(谭浩强)总结而成,整理者:华科小涛@http://www.cnblogs.com/hust-ghtao,转载请注明) 1   基本思想与概念 在传统的程序设计 ...

  8. python_如何创建可管理的对象属性

    案例: 在面向对象编程中,我们把方法作为对象的接口,自己访问对象的属性可能是不安全的,或设计上不灵活,但是使用调用方法在形式上不如访问属性简洁 繁: circle.getRadius() circle ...

  9. 关于table相关的属性,CSS样式

    table属性: 1:border没有设置的话表格没有边框 2:cellpadding单元格和内容的空白 3:cellspacing单元格和单元格之间的空白 4:frame规定外边框可见性 5:rul ...

随机推荐

  1. C - Long Beautiful Integer codeforces 1269C 构造

    题解: 这里的m一定是等于n的,n为数最大为n个9,这n个9一定满足条件,根据题目意思,前k个一定是和原序列前k个相等,因此如果说我们构造出来的大于等于原序列,直接输出就可以了,否则,由于后m-k个一 ...

  2. 如何在Ubuntu 18.04上安装Nginx

    Nginx功能之强大,想必大家比我更清楚. 百度百科:Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是由伊戈尔 ...

  3. RabbitMQ 消息队列入门

    文档 入门 主要的内容:one two three four five six seven 前言 中间件 消息队列 异步处理,注册完发短信 应用解耦,订单接口调用扣库存接口,失败了怎么办? 流量削峰, ...

  4. 详解 Hashtable

    至于HashTable,本人只想说,除了它们各自的特点是截然相反外,其余性质 以及 用法和HashMap的性质几乎一样, (有关Map集合的基本性质,请观看本人博文-- <详解 Map集合> ...

  5. SK-learn实现k近邻算法【准确率随k值的变化】-------莺尾花种类预测

    代码详解: from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split fr ...

  6. 线上Bug无法复现怎么办?老司机教你一招,SpringBoot远程调试不用愁!

    前言 在部署线上项目时,相信大家都会遇到一个问题,线上的 Bug 但是在本地不会复现,多么无奈. 此时最常用的就是取到前端传递的数据用接口测试工具测试,比如 POSTMAN,复杂不,难受不? 今天陈某 ...

  7. Java代码生成器加入postgresql数据库、HikariCP连接池、swagger2支持!

    目录 前言 PostgreSql VS MySql HikariCP VS Druid Swagger2 自定义参数配置一览 结语 前言   最近几天又抽时间给代码生成器增加了几个新功能(预计今晚发布 ...

  8. weblogic补丁升级详细步骤,18.7.17补丁更新

    weblogic打补丁 到weblogic官网下载补丁包 对应的补丁包  如: p22248372_1036012_Generic.zip 一  安装补丁步骤 1.登录linux的weblogic用户 ...

  9. ansible的清单管理与模块应用(三)

  10. 以命令行界面启动 Ubuntu

    1. /etc/default/grub 将GRUB_CMDLINE_LINUX_DEFAULT一行中改为"quiet splash 3" 2. update-grub 3. 重启