下面这三组是关于元素大小、位置相关的属性

一、offset 偏移量

  1、offsetParent

    该属性获取距离当前元素最近的定位父元素,如果没有定位父元素此时是 body 元素

  2、offsetLeft / offsetTop

    该属性获取当前元素距离 offsetParent 的横向偏移纵向偏移

  3、offsetWidth / offsetHeight

    该属性获取当前元素的宽高,宽高包括:内容(content)、边框(border)和内填充(padding)

  

  注意:offset 属性 是只读属性

  Demo:

 // 结构
<div id="box">
<div id="child"> </div>
</div>
// 样式
<style>
body {
margin: 0;
}
#box {
position: relative;
width: 300px;
height: 300px;
background-color: red;
overflow: hidden;
margin: 50px;
}
#child {
width: 100px;
height: 100px;
background-color: blue;
margin: 50px;
border: 10px solid yellow;
padding: 10px;
}
</style>
// js
<script>
// 获取子元素的位置和大小
var child = document.getElementById('child');
console.log(child.offsetParent);
// 获取child的位置 offsetLeft 距离offsetParent的横向偏移
console.log(child.offsetLeft);
console.log(child.offsetTop); // 获取child的大小 包括边框和padding
console.log(child.offsetWidth);
console.log(child.offsetHeight);
</script>

二、client 客户区大小

  1、clientLeft / clientTop

    该属性是获取当前元素的 border-left、border-right 的宽度。(没有 border-right/border-bottom属性)

  2、clientWidth / clientHeight

    该属性是获取当前元素的宽高,宽高包括:内容(content)和 内填充 (padding)

  

  Demo:

 // 结构
<div id="box"> </div>
// 样式
<style>
body {
margin: 0;
} #box {
width: 100px;
height: 100px;
margin: 50px;
border: 30px solid red;
padding: 10px;
background-color: green;
}
</style>
// js
<script>
var box = document.getElementById('box');
// clientLeft 是border-left 的宽度
// clientTop border-top 的宽度
console.log(box.clientLeft);
console.log(box.clientTop); // 获取大小 包括padding 但是不包括边框
console.log(box.clientWidth);
console.log(box.clientHeight);
</script>

三、scroll 滚动偏移

   1、scrollLeft / scrollTop

    该属性获取元素内容滚动出去的横向距离 / 纵向距离

  2、scrollWidth / scrollHeight

    该属性获取元素的大小,指的是:内容(content)+ 内填充(padding)和未显示出来的内容

  

  Demo:

 // 结构
<div id="box">
Javascript,一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的直译语言。它已经由欧洲电脑制造商协会通过ECMAScript实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器(Chrome、IE、FireFox等)支持。
</div>
// 样式
<style>
body {
margin: 0;
} #box {
width: 100px;
height: 100px;
margin: 50px;
border: 30px solid red;
padding: 10px;
background-color: green;
overflow: auto;
}
</style>
// js
<script>
// scroll
var box = document.getElementById('box');
// 当拖动box中的滚动条的时候触发
box.onscroll = function () {
console.log(box.scrollLeft);
console.log(box.scrollTop); }
// 内容的大小,包括padding 和未显示的内容,不包括滚动条
console.log(box.scrollWidth);
console.log(box.scrollHeight); </script>

JavaScript 之 offset 、client、scroll的更多相关文章

  1. JavaScript中的 offset, client,scroll

    在js 中我们要用到的 offset, client, scroll 在这我把自己理解的给大家分享一下. offset div.offsetTop 指div距离上方或上层控件的距离,单位像素 div. ...

  2. offset/client/scroll一些总结

    offset/client/scroll一些总结 1.offset 首先offset共有五个值 1.offsetParent 2.offsetTop 3.offsetLeft 4.offsetWidt ...

  3. js中 offset /client /scroll总结

    offset家族(只能读取,不能操作): offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(就是子元素左边框到父元素左边框的距离). offsetTo ...

  4. js 元素offset,client , scroll 三大系列总结

    1,element.offsetWidth : 包括 padding 和 边框 2,element.clientWidth : 包括 padding ,不包含边框 , 内容超出会溢出盒子的时候,就用s ...

  5. offset client scroll

    offsetHeight offsetWidth返回为元素在屏幕上显示大小,不包括外边距 clientHeight clientWidht 和上面两个类似,不同的是,这两个不包括外边距高度. < ...

  6. bom中的offset,client,scroll

    简单明了

  7. JS 中的offset、scroll、client总结

    经常碰到offset.scroll.client这几个关键字,每次都要各种实验,这里总结一下. 两张图镇楼,随时翻阅 1. offset offset 指偏移,包括这个元素在文档中占用的所有显示宽度, ...

  8. javascript中常用坐标属性offset、scroll、client

    原文:javascript中常用坐标属性offset.scroll.client 今天在学习js的时候觉得这个问题比较容易搞混,所以自己画了一个简单的图,并且用js控制台里面输出测试了下,便于理解. ...

  9. 前端 ---client、offset、scroll系列

    client.offset.scroll系列   1.client系列 代码如下: <!DOCTYPE html> <html> <head> <meta c ...

  10. python 全栈开发,Day52(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)

    昨日作业讲解: 京东购物车 京东购物车效果: 实现原理: 用2个盒子,就可以完整效果. 先让上面的小盒子向下移动1px,此时就出现了压盖效果.小盒子设置z-index压盖大盒子,将小盒子的下边框去掉, ...

随机推荐

  1. Educational Round 64 题解

    前言: 这场太难了……我一个紫名只打出两题……(虽说感觉的确发挥不够好) 一群蓝绿名的dalao好像只打了两题都能升分的样子…… 庆幸的是最后A出锅然后unr了>///< 写一波题解纪念这 ...

  2. 基于web公交查询系统自我安排进度

    这周完成站点信息管理

  3. node.js 路由详解

    路由的基本使用 第一步:获取url跟目录下的字符 var http = require('http'); var url = require('url') http.createServer(func ...

  4. Spring Security教程(五)

    在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...

  5. 第3课 auto类型推导(2)

    第3课 auto类型推导(2) 一.使用auto的优势 (一)避免使用未初始化变量 (二)可简化变量/对象类型的声明 (三) 在某些场合无法判断出类型时,可用auto自动推导(如lambda表达式) ...

  6. 修改ARP缓存表大小

    在下发Mininet的ARP缓存表表项时,出现了如下的错误信息: SIOCSARP: No buffer space available 这是由于ARP表是缓存在内存中的,超过了系统对ARP缓存表大小 ...

  7. 《Linux就该这么学》培训笔记_ch04_Vim编辑器与Shell命令脚本

    <Linux就该这么学>培训笔记_ch04_Vim编辑器与Shell命令脚本 文章最后会post上书本的笔记照片. 文章主要内容: Vim编辑器 Shell脚本 流程控制语句 if语句 f ...

  8. magic模块 :Exception Value:failed to find libmagic. Check your installation

    原因 缺少安装依赖: magic 安装依赖: https://github.com/ahupp/python-magic#dependencies windows下解决方法: https://gith ...

  9. JavaSE 面试题: 方法的参数传递机制

    JavaSE 面试题 方法的参数传递机制 import java.util.Arrays; public class Test { public static void main(String[] a ...

  10. 构造 + 离散数学、重言式 - POJ 3295 Tautology

    Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...