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,却让他们产生了选择困难症. 一方面,企业皆注重流程的高效运转,最好内外部的业务都能用一个系统来解决.所有流程一天就能上线什么的,那就更好啦 ...
随机推荐
- 怎样避免在EF自己主动生成的model中的DataAnnotation被覆盖掉
相信非常多人刚接触EF+MVC的时候,会有这个疑问.就是当我们在model类中加验证信息的时候.会在又一次生成model的时候被重写掉. 这里介绍一个方法: 比方我有个Employee类是从数据库中生 ...
- Bootstrap组件之输入框组
.input-group--设置div为输入框组: .input-group-lg..input-group-sm..input-group-xs--改变输入框组的尺寸: .input-group-a ...
- numeric and int in sql server
类型映射 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings C#关 ...
- thinkphp5项目--个人博客(二)
thinkphp5项目--个人博客(二) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...
- nyoj--767--因子和(模拟)
因子和 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 题目很简单明了,给你一个数n,判断它是不是素数,如果是素数就输出"Yes",如果不是素数则输出 ...
- django 笔记14 中间件
用户请求->中间件->urls->views->返回字符串->中间件->用户浏览器 settings MIDDLEWARE里面都是中间件 有的地方叫管道 请求来的时 ...
- [NOIP2015模拟10.27] [JZOJ4270] 魔道研究 解题报告(动态开点+权值线段树上二分)
Description “我希望能使用更多的魔法.不对,是预定能使用啦.最终我要被大家称呼为大魔法使.为此我决定不惜一切努力.”——<The Grimoire of Marisa>雾雨魔理 ...
- centos 服务器配置注意项
Mysql 出现Table‘xxx’is read only问题 Mysql数据库在由Mssql数据库导入数据文件后出现“ERROR 1036 (HY000): Table 'xxxx' is rea ...
- VS Code(Visual Studio Code)
这次推荐 VS Code,这是个跨平台.免费的代码编辑器,集成开发.调试.编译为一身.单成启动速度,资源占用,就已经赢了各大编辑器(什么 SB.N++之类).以前觉得 VS 2015 是宇宙最强编辑器 ...
- 天津大学各种Latex模板共享链接
多元统计课最后Ppt里说要共享一下模板.这里给出链接 https://www.github.com/6gbluewind 注:写本文时还未上传 ************* 下午终于有网了,于是上传了一 ...