scrollWidth到底是什么???
贴上MDN对scrollwidth的定义:
The Element.scrollWidth read-only property is a measurement of the width of an element's content, including content not visible on the screen due to overflow.
自译:element.scrollWidth这个只读属性是用来测量元素内容宽度的一种方式,它包括了被overflow属性隐藏的不可见的宽度.
The scrollWidth value is equal to the minimum width the element would require in order to fit all the content in the viewport without using a horizontal scrollbar. The width is measured in the same way as clientWidth: it includes the element's padding, but not its border, margin or vertical scrollbar (if present). It can also include the width of pseudo-elements such as ::before or ::after. If the element's content can fit without a need for horizontal scrollbar, its scrollWidth is equal to clientWidth.
自译:scrollWidth值等于元素的最小宽度要求内容与视口相适,不使用水平的滚动条(意思应该就元素内容不要太大导致水平滚动条出现吧).这个宽度的测量方式与clientWidth方式一样,包括元素的内边距,不包括边框,外边距,垂直滚动条(如果有出现的话).它也可以包括包括伪元素的宽度例如::before或::after.如果元素的内容刚好合适不需要水平滚动条,那么scrollwidth就等于clientWdth.
看的似懂非懂,直接代码测试:
html部分略
<script>
var box = document.getElementById("box");
// 页面可视区的宽度
var oClientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0; // 页面可视区的高度
var oClientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0; // scrollTop 网页被卷起的高度
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; // scrollLeft 网页左边被卷起的宽度
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0; // scrollWidth 表示整个网页正文的宽度
var scrollWidth = document.documentElement.scrollWidth || document.body.scrollWidth; // scrollHeight 表示整个网页正文的高度
var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; box.onclick = function(e){
var oEvent = e || event;
console.log('相对显示器左边的位置screenX为:' + oEvent.screenX); //95
console.log('相对显示器上边的位置screenY为:' + oEvent.screenY); //152
console.log('相对可视区左边的位置clientX为:' + oEvent.clientX); //95
console.log('相对可视区上边的位置clientY为:' + oEvent.clientY); //61
console.log('可视区宽度clientWidth为:' + oClientWidth); //1920
console.log('可视区高度clientHeight为:' + oClientHeight); //535
console.log('点击位置在页面中的纵坐标为:' + (oEvent.clientY + scrollTop)); //1461
console.log('点击位置在页面中的横坐标为:' + (scrollLeft + oEvent.clientX)); //95
console.log('页面卷起的高度scrollTop为:' + scrollTop); //1400
console.log('页面左边卷起的宽度为scrollLeft为:' + scrollLeft); //0
alert('盒子的宽度为scrollWideth为:' + box.scrollWidth); //3000
alert('页面的宽度为scrollWideth为:' + scrollWidth);
console.log('页面的高度为scrollHeight为:' + scrollHeight); //5000
console.log('点击位置相对页面位置的左边pageX为:' + oEvent.pageX); //95
console.log('点击位置相对页面位置的上边pageY为:' + oEvent.pageY); //1461
//1461
}
</script>
1.盒子大小<body大小<浏览器窗口大小
a) 当body/html调用scrollwidth:
chrome, ie6-ie11:
body.scrollWidth=浏览器可视窗口宽度(clientWidth)--滚动条栏的宽度
b) 当盒子调用:
chrome, ie6-ie11:
box.scrollwidth=width+padding
2.盒子大小<浏览器窗口大小< body大小
a) body/html调用scrollwidth:
chrome, ie6-ie11:
body.scrollWidth=body.width+ body.padding+ body.border+左边的margin
(只对body的值进行累计,如果box有margin撑大了body也把box.margin的宽度不会计算进来)
b) 当盒子调用:
chrome, ie6-ie11:
box.scrollwidth=width+padding;
3.浏览器窗口大小<盒子大小< body大小
同情况2
4.body大小<盒子大小<浏览器窗口大小
chrome,ie11:
body.scrollWidth=浏览器可视窗口宽度--滚动条栏的宽度
box.scrollwidth=width+padding;
5.body大小<浏览器窗口大小<盒子大小
chrome,ie11:
body.scrollWidth=body.width+ body padding+ body.margin+body.leftpadding+body.leftboder+body.leftmargin;
box.scrollWidth=width+padding;
6.浏览器窗口大小 < body大小 <盒子大小
chrome,ie11:
body.scrollWidth= box.width+ box.padding+ box.margin+body.leftpadding+body.leftboder+body.leftmargin;
box.scrollWidth=box.width+box.padding;
总结:
看了很多资料说是clientWidth是可视区域大小其实也是包括了滚动条栏的宽度.
不管啥情况:box.scrollWidth=box.width+box.padding;
如果浏览器的窗口宽度最大:
body.scrollWidth=浏览器窗口大小(clientWidth)-右侧滚动条栏宽度(一般是17px);
如果是body的宽度(border+padding)最大:
body.scrollWidth=body.width+ body.padding+ body.border+左边的margin;
如果是盒子的宽度(border+padding)最大:
body.scrollWidth= box.width+ box.padding+ box.margin+body.leftpadding+body.leftboder+body.leftmargin;
scrollWidth到底是什么???的更多相关文章
- HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解
scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最 ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
- scrollWidth,offsetWidth,clientWidth,width;scrollHeight,offsetHeight,clientHeight,height;offsetTop,scrollTop,top;offsetLeft,scrollLeft,left还有谁
题中的那么多属性让人头都大了,他们到底是什么意思?不同浏览器的实现是一样的吗?以下所有结论来自chrome版本 53.0.2785.89 (64-bit)和firefox版本52.0.2,操作系统ub ...
- H5,PC网页屏幕尺寸相关整理(scrollLeft,scrollWidth,clientWidth,offsetWidth)
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解scrollHeight: 获取对象的滚动高度. scrollLef ...
- 转 HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
- 拨开迷雾,找回自我:DDD 应对具体业务场景,Domain Model 到底如何设计?
写在前面 除了博文内容之外,和 netfocus 兄的讨论,也可以让你学到很多(至少我是这样),不要错过哦. 阅读目录: 迷雾森林 找回自我 开源地址 后记 毫无疑问,领域驱动设计的核心是领域模型,领 ...
- Js new到底发生了什么
在Js中,我们使用了new关键字来进行实例化 那么在这个new的过程中到底发生了什么? 关于构造函数的return 正常来讲构造函数中是不用写return语句的,因为它会默认返回新创建的对象. 但是, ...
- 电信计费业务:预后融合OCS到底应该实扣还是虚扣?
引入OCS的初衷之一是为了让计费系统能够参与到用户的通讯控制中来,也就是所谓的实时信控.用户在没有余额时,通讯就会被停止,不会造成"天价欠费 ",一方面保障用户的利益,一方面也保障 ...
- 港真,到底应该选择OA还是BPM?
越来越多企业意识到流程管理的重要性,但是,选择OA还是BPM,却让他们产生了选择困难症. 一方面,企业皆注重流程的高效运转,最好内外部的业务都能用一个系统来解决.所有流程一天就能上线什么的,那就更好啦 ...
随机推荐
- Qt之QPushButton
简述 前面章节我们分享过Qt之QAbstractButton,讲解了QAbstractButton的基本用法,本节着重讲解QPushButton. 简述 常用状态 效果 源码 QSS 更多参考 关于Q ...
- [Angular] Create a ng-true-value and ng-false-value in Angular by controlValueAccessor
If you're coming from AngularJS (v1.x) you probably remember the ng-true-value and ng-false-value di ...
- 与Greenplum度过的三个星期
5月4日-5月24日.断断续续折腾了三个星期的Greenplum,总算告一段落了:扩容,发现扩不成容.仅仅好升级.升级,发现一堆错误,仅仅好暂停修复数据库:修好了,继续升级.升完级,发现错误.修啊修啊 ...
- Java (JDK7)中的String常量和String.intern的实现
在java中有constantPool常量池,常量池里存放的是类,方法,接口的等常量,而对于存放字符串常量通常存放的符号链接Symbol 或者真实的String的对象的引用. 我们来看一段简单的代码和 ...
- Deleting elements
There are several ways to delete elements from a list. If you know the index of the element you want ...
- python-网络-tcp
python-网络-tcp 标签(空格分隔): python TCP[client]-发送数据 from socket import * s = socket(AF_INET, SOCK_STREAM ...
- TYVJ 1340 折半暴搜+二分
思路: 1. 这 题 不卡常过不去啊-- (先加一个random_shuffle) 首先 我们可以折半 搜这一半可以到达的重量 sort一遍 然后搜另一半 对于路程中每一个解 我们可以二分前一半中加这 ...
- zookeeper伪分布安装配置
1.下载路径为:http://mirrors.cnnic.cn/apache/zookeeper/stable/ 2.安装: 第一步 解压zookeeper压缩包: 进入 zookeeper安装目录 ...
- php生成无限栏目树
栏目数组:$arr=Array( Array('cid' => 2,'cname' => '新闻','pid' => 0), Array('cid' => 4,'cnam ...
- pip源
阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(do ...