DOM盒模型和位置 client offset scroll 和滚动的关系
DOM盒模型和位置 client offset scroll 和滚动的关系
概览
在dom里面有几个描述盒子位置信息的值,
- pading border margin
- width height
- client
- clientWidth 不要border
- clientHeight 不要border
- offset
- offsetTop
- offsetLeft
- offsetWidth 要border
- offsetHeight 要border
- scroll
- scrollTop
- scrollHeight
- scrollLeft
- scrollWidth
盒模型
生产环境一般使用 box-sizing: border-box,
效果:
width == content.width + pading + border
height == content.height + pading + border
.antd-pro-pages-test-dom-index-box {
box-sizing: border-box;
width: 100px;
height: 100px;
padding: 5px;
border-color: grey;
border-style: solid;
border-width: 5px;
margin: 5px;
}
滚动
<div class="container1" style="overflow: auto; height: 200px; width: 200px">
<ul class="container2" style="position: relative">
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
</div>
// 把item 放在container的可视区域范围内
function scrollToDom(container, item){
// 当前元素 上边框上边 到 基线 的距离
const itemTop = item.offsetTop;
// 当前元素 下边框下边 到 基线 的距离
const itemBottom = itemTop + item.offsetHeight;
// container 上边框下边 距离 基线距离
const containerTop = container.scrollTop;
// container 下边框上边 距离 基线距离
const containerBottom = containerTop + container.clientHeight;
if(itemTop < containerTop){
container.scrollTop = itemTop;
}
if(itemBottom > containerBottom){
container.scrollTop = itemBottom - container.clientHeight;
}
}
此种结构特点
如果垂直滚动条已经出来了
.container1
的上下 padding 区域也会有内容
向上滑动
- 向上滑动, 实质是
.container2
向上滑动了 - 因为.container2.position == relative
li.offsetParent
也是.container2
, 所以.container1.scrollTop
和li.offsetTop
基准线都是.container2
的上边框, 具有可比性
DOM盒模型和位置 client offset scroll 和滚动的关系的更多相关文章
- DOM盒子模型常用属性client,offset和scroll
JS盒子模型属性 在JS中通过相关的属性可以获取(设置)元素的样式信息,这些属性就是盒子模型属性(基本上都是有关于样式的) 属性 值 client top/left/width/height offs ...
- JavaScript概念之screen/client/offset/scroll/inner/avail的width/left 分类: JavaScript HTML+CSS 2015-05-27 16:42 635人阅读 评论(0) 收藏
原文地址:http://caibaojian.com/js-name.html JS中获取各种宽度和距离,常常让我们混淆,各种浏览器的不兼容让我们很头疼,现在就在说说js中有哪些宽度和距离. 1.名词 ...
- JavaScript位置:window&client&offset&scroll&MouseEvent&getBoundingClientRect&计算任意元素滚动条宽度
Window: window.innerWidth:浏览器viewport视口宽,包括垂直滚动条 window.innerHeight:浏览器视口高,包括水平滚动条 window.outerWidth ...
- JS中client/offset/scroll等的宽高解析
原文地址:→传送门 window相关宽高属性 1. window.outerHeight (窗口的外层的高度) / window.outerWidth (窗口的外层的宽度) window.outerH ...
- client , offset , scroll 系列 及百度导航栏案例
1. client 系列 示例 : <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- client,offset,scroll系列
client(客户端),offset(偏移),scroll(滚动)1.client系列 clientTop 内容区域到边框顶部的距离 ,说白了,就是边框的高度 clientLeft 内容区域到边框左部 ...
- JS-特效 ~ 04. client对象、网页可视区域的宽高、client / offset / scroll 三大家族的区别、冒泡事件、事件委托、获取内嵌式和外链式属性getStyle(ele,attr) ;、缓动动画封装
知识点: 模拟滚动条的解除事件问题 : event内置对象,包含 了大量事件: page兼容性: pageX || clientX + scool().top : if (true === a)tr ...
- client offset scroll 之间的区别
一.client 属性 值 clientWidth 元素被设置的宽度 + padding左右内间距 clientHeight 元素被设置的高度 + padding上下内间距 clientLeft 左 ...
- JS ——DOM,BOM(包含盒模型,动画)总结
JS盒模型 content: 通过计算后样式获取padding + content: box.clientWidth | box.clientHeightborder + padding + cont ...
随机推荐
- 42.Pycharm连接数据库出现错误:1045、1044:django.db.utils.OperationalError: (1045, "Access denied for user 'Whois'@'localhost' (using password: YES)”)
1.在pycharm中设置好数据库的连接信息,连接数据库db01, DATABASES = { 'default': { # 指定所使用的的数据库引擎 'ENGINE': 'django.db.bac ...
- nvm,nrm和yarn
nvm Node Version Management nvm list 查看所有已安装的 node 版本 nvm install 版本号 安装指定版本的 node nvm use 版本号 切换到指定 ...
- LAMP(七)之编译安装php(模块化和fpm两种方式)
安装前说明: 安装环境: CentOS6 安装应用程序:httpd2.4 + mariadb + php 安装次序: 先编译安装 httpd2.4和mariadb,最后安装php 编译安装 httpd ...
- 使用微信扫一扫时获取sign签名
private $appId = '你的APPID'; private $appSecret = '你的APPsecret'; /** * 获取签名信息 * @return array */ publ ...
- ubuntu set up 6 - NTFS Mount
1. NTFS Mounted as read-only https://askubuntu.com/questions/1138076/ubuntu-18-04-cant-write-on-ntfs ...
- 如何在 messager/alert/confirm等消息提示框中 获取 / 设置 嵌入 html内容中的 input[type=checkbox]等的选中状态?
总结, 有3点: 不能/不要 在 这些消息框 / 提示框/ 对话框中的 回调函数中去写代码: 获取嵌入 内容中input.checkbox的选中状态, 因为 虽然在这些框存在的时候, 这个 check ...
- unserialize():Error at offset 0 of 96 bytes是什么意思
数据库有个列是数组序列化后存到数据库的,取出来得反序列化, php想要把数组保存到数据库里,有两种序列化方式,分别是: //php系统序列化 $b = serialize($a); //序列化数 ...
- 3级搭建类301-Oracle 11g RAC 双节点搭建(11.2.0.4)非公
项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...
- 【Unity|C#】基础篇(13)——特性(Attribute)
[学习资料] <C#图解教程>(第24章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- Tram POJ - 1847 spfa
#include<iostream> #include<algorithm> #include<queue> #include<cstdio> #inc ...