上网搜了一下scrollWidth和scrollHeight,大部分都是转帖,也没有具体说清楚,这两个属性值是什么,也没有图。

索性自己测试一下,包含的浏览器有IE 6,IE 7,IE 8,IE 9,Firefox,Chrome,Opera,Safari,顺便把测试的截图也发上来,这样大家看着也明白。

一、scrollWidth

首先,我们先上MSDN上查一下scrollWidth的文档说明。

链接地址:http://msdn.microsoft.com/en-us/library/ms534619%28v=VS.85%29.aspx

scrollWidth Property

.NET Framework 3.0

Retrieves the scrolling width of the object.

返回对象的滚动宽度。(这句就是废话,和没解释一样)

Syntax

HTML N/A
Scripting [ iWidth = ] object.scrollWidth

Possible Values

iWidth Pointer to a nonnegative long integer that specifies the width, in pixels.

The property is read-only.The property has no default value.

这个属性是只读的,并且没有默认值。

Remarks

The width is the distance between the left and right edges of the object's visible content.

这个宽度是指对象的可见内容的左边界到右边界的距离。(这个左边界和右边界是如何理解,也没有说清楚,不过下面给了个链接,我懒的去看。)

For more information about how to access the dimension and location of objects on the page through the Document Object Model (DOM), seeMeasuring Element Dimension and Location with CSSOM in Internet Explorer 9.

下面再来看看火狐的开发者网站MDN是如何解释的。

链接地址:https://developer.mozilla.org/en/DOM/element.scrollWidth

scrollWidth is a read–only property that returns either the width in pixels of the content of an element or the width of the element itself, whichever is greater. If the element is wider than its content area (for example, if there are scroll bars for scrolling through the content), the scrollWidth is larger than theclientWidth.

scrollWidth是只读属性,返回的是元素的内容宽度或者元素本身的宽度,两者之间的大者。如果元素比内容区域宽(例如,如果有滚动条用来滚动内容),scrollWidth是大于clientWidth的。

综上所述,结合IE和Firefox的官方文档的解释,我认为scrollWidth的语义就是当一个元素有滚动条的时候,scrollWidth表示的是元素内容区域的滚动宽度,当没有滚动条的时候,就是元素的本身宽度。

下面我们就来看看各个浏览器,实际是怎么解释的。

测试的html代码很简单:

(1)没有滚动条,没有内容

[html] view plain copy print ?
  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript">
  7. window.onload = function() {
  8. var div = document.getElementById('noScrollbar');
  9. alert(div.scrollWidth);
  10. };
  11. </script>
  12. </head>
  13. <body>
  14. <div id="noScrollbar" style="width: 100px; height: 100px; background-color: green; margin: 100px; padding: 10px;"></div>
  15. </body>
  16. </html>

<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> window.onload = function() { var div = document.getElementById('noScrollbar'); alert(div.scrollWidth); }; </script> </head> <body> <div id="noScrollbar" style="width: 100px; height: 100px; background-color: green; margin: 100px; padding: 10px;"></div> </body> </html>(2)没有滚动条,有内容

[html] view plain copy print ?
  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript">
  7. window.onload = function() {
  8. var div = document.getElementById('noScrollbarWithContent');
  9. alert(div.scrollWidth);
  10. };
  11. </script>
  12. </head>
  13. <body>
  14. <div id="noScrollbarWithContent" style="width: 100px; height: 100px; background-color: green; margin: 100px; padding: 10px;">
  15. <div style="width: 100px; height: 100px; background-color: yellow;"></div>
  16. </div>
  17. </body>
  18. </html>

<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> window.onload = function() { var div = document.getElementById('noScrollbarWithContent'); alert(div.scrollWidth); }; </script> </head> <body> <div id="noScrollbarWithContent" style="width: 100px; height: 100px; background-color: green; margin: 100px; padding: 10px;"> <div style="width: 100px; height: 100px; background-color: yellow;"></div> </div> </body> </html>(3)有滚动条,有内容

[html] view plain copy print ?
  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript">
  7. window.onload = function() {
  8. var div = document.getElementById('scrollbarWithContent');
  9. alert(div.scrollWidth);
  10. };
  11. </script>
  12. </head>
  13. <body>
  14. <div id="scrollbarWithContent" style="width: 100px; height: 100px; background-color: green; margin: 100px; padding: 10px; overflow: auto;">
  15. <div style="width: 200px; height: 83px; background-color: yellow;"></div>
  16. </div>
  17. </body>
  18. </html>

<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> window.onload = function() { var div = document.getElementById('scrollbarWithContent'); alert(div.scrollWidth); }; </script> </head> <body> <div id="scrollbarWithContent" style="width: 100px; height: 100px; background-color: green; margin: 100px; padding: 10px; overflow: auto;"> <div style="width: 200px; height: 83px; background-color: yellow;"></div> </div> </body> </html>

1、IE 6

做中文网站IE 6还是要考虑的,因为IE 6在中国还是有很大份额的。

(1)没有滚动条,没有内容。如下图,scrollWidth = 左内边距 + 右内边距

(2)没有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度 +  右内边距

(3)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度 +  右内边距

综上,IE 6的scrollWidth = 左内边距 + 内容宽度 + 右内边距,这个内容宽度不等于元素的宽度

2、IE 7

(1)没有滚动条,没有内容。如下图,scrollWidth = 左内边距 + 右内边距

(2)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度 +  右内边距

(3)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度 +  右内边距

综上,IE 7的scrollWidth = 左内边距 + 内容宽度 + 右内边距,这个内容宽度不等于元素的宽度

3、IE 8

(1)没有滚动条,没有内容。如下图,scrollWidth = 左内边距 + 内容宽度 + 右内边距

(2)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度 +  右内边距

(3)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度

综上,IE 8的scrollWidth = 左内边距 + 内容宽度

IE 6和IE 7的表现是一致的,IE 8的修正了IE 6和IE 7在解释内容宽度的不正确,但是IE 8的scrollWidth为什么没有了padding-right?真是奇怪!

再来看看firefox是如何表现的。

4、Firefox

(1)没有滚动条,没有内容。如下图,scrollWidth = 左内边距 + 内容宽度 + 右内边距

(2)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度 +  右内边距

(3)有滚动条,有内容。如下图,scrollWidth = 左内边距 + 内容宽度

综上,Firefox的scrollWidth = 左内边距 + 内容宽度

最后,结果是ie8、ie9、firefox、chrome、opera、safari的表现都是一致的,具体我就不截图了。IE 6和IE 7表现一致,但是他们的内容宽度有bug。

我们来看看w3是如何解释的,链接地址:http://www.w3.org/TR/cssom-view/#dom-element-scrollwidth

The scrollWidth attribute must return the result of running these steps:

  1. If the element does not have any associated CSS layout box return zero and terminate these steps.

  2. If the element is the root element and the Document is not inquirks mode return max(document content width, value ofinnerWidth).

  3. If the element is the HTML body element and the Document is inquirks mode return max(document content width, value ofinnerWidth).

  4. Return the computed value of the 'padding-left' property, plus the computed value of the 'padding-right', plus thecontent width of the element.

W3C的解释是scrollWidth应该是计算过的左右padding值加上内容宽度,从上面的测试来看,我觉得所有浏览器都表现的不正确,IE 6和IE 7没有正确计算内容宽度。IE 8及firefox等其他浏览器都没有加上padding-right。

最后,我向bugzilla提交了一个firefox的bug。受理的很快,中午提交,下午就有人回复。

地址在这里:https://bugzilla.mozilla.org/show_bug.cgi?id=718548

Html元素的scrollWidth和scrollHeight详解 .相关推荐

  1. 如何把canvas元素作为网站背景总结详解

    如何把canvas元素作为网站背景总结详解 一.总结 一句话总结:最简单的做法是绝对定位并且z-index属性设置为负数. 1.如何把canvas元素作为网站背景的两种方法? a.设置层级(本例代码就 ...

  2. css伪元素before和after用法详解

    css伪元素before和after用法详解 要想了解伪元素before和after到底是什么,首先就应该打开编译器敲入代码并在浏览器运行检查 <!DOCTYPE html> <ht ...

  3. 窗口分析函数19_Mysql查询窗口函数里第一个 最后一个 第N个元素的值的案例详解(FIRST_VALUE LAST_VALUE NVH_VALUE)

    Mysql查询窗口函数之按序号取元素详解 需求概述 查询以课程(course)分区以分数(score)逆序的窗口里的第一个.最后一个和第2个元素对应的值.示例表数据见下: SELECT '数学' co ...

  4. 如何得到iterator的当前元素_Java中迭代器Iterator详解

    1.定义 Iterator的定义为:对Collection进行迭代的迭代器,Iterator取代了Java Collection Framework中的Enumeration.Iterator与Enu ...

  5. 如何给6个整数的一维数组某个元素赋值_数组指针详解

    文章部分资料来源: Pointer to an Array | Array Pointer - GeeksforGeeks​www.geeksforgeeks.org 介绍数组指针之前,先来回顾指针: ...

  6. php js offset,获取元素的偏移量offset实例详解

    问题:如果获取元素距离文档顶部的距离?[javascript] view plain copy var rect=$('#elem')[0].getBoundingClientRect(); //获取 ...

  7. js删除数组中指定元素_js中数组操作详解

    今天给大家带来一篇有关数组操作方法的文章. 新建数组 方法一:通过new运算符创建一个数组构造函数. var arr = new Array(); 方法二:通过方括号直接创建直接量数组. var ar ...

  8. C语言中二维数组名与数组地址、首行地址、首行首元素地址关系与区别详解(初学者必须掌握)

    C语言作为很多大学理工科都会学习的语言,作为一种编程入门语言. 但是相对于其他高级编程语言来说相对是比较难,尤其是指针,不知道有多少莘莘学子都是因为它,从C语言入门到放弃. 想当年,笔者在大一学习C语 ...

  9. PyTorch搜索Tensor指定维度的前K大个(K小个)元素--------(torch.topk)命令参数详解及举例

    torch.topk 语法 torch.topk(input, k, dim=None, largest=True, sorted=True, *, out = None) 作用 返回输入tensor ...

最新文章

  1. 计算机网络中的协议数据单元的控制信息主要包括哪些内容
  2. SAP MM 采购组与采购组织,岂能没有点关系?
  3. onclick事件中加href
  4. SVM+HOG:从完全不包含人体的图片中随机剪裁出64*128大小的用于人体检测的负样本
  5. 列表、元组、字典与集合
  6. 更新登录SAP后的LOGO
  7. C语言 | C语言实现高精度加法——数组加法(附源代码)
  8. iphone UITableView及UIWebView的使用
  9. re:Invent大会第十年,亚马逊云科技推出了哪些底层自研技术
  10. Excel如何快速根据身份证号码计算周岁?
  11. Xmodem、Ymodem和Zmodem协议是最常用的三种通信协议
  12. 二、制作BOM表格--物料表格--Bill of Materials
  13. P2构型并联混合动力汽车Cruise整车仿真模型
  14. 《猎豹行动》出版一周年了!
  15. DTOJ5057 英雄联盟
  16. 售前的价值在哪里?这个问题不简单
  17. 重写规则(Rewrite Rules)在IIS和Linux服务器的配置区别
  18. 翻牌记忆类H5游戏的春天
  19. html 字体形状,二十款漂亮的CSS字体样式
  20. Echarts实战案例代码(9):图表纹理填充的解决方案(柱图为例)

热门文章

  1. sicily题目分类
  2. 归档日志存在arch_oracle归档日志
  3. linux 学习笔记(二)下载中文语言包
  4. 羊皮卷的故事-第四章
  5. 英语总结-August
  6. eclipse列名无效_sql 列名无效
  7. 谷歌IO大会全面硬钢微软+OpenAI
  8. 扩展CUDA SDK 2.3 の convolutionSeparable
  9. 完了完了 我好像中奖了!(木马病毒)
  10. 工作 5 年观察:快速在职场崛起,拼这 10 个认知