- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 jQuery 脚本,它通过普通的 CSS 宽度属性将元素的宽度设置为 50%。
如果我然后输出 outerWidth(true)
要获得准确的像素值,它有时与 $(window).width()/2
不对应- 这是基于网站的主题。不幸的是,我无法将这种行为的演示带到网上。
是否有任何特定的 CSS 属性可能导致这种差异?
问题是 outerWidth()
例如,正在报告 564px
,但是当我 checkin Safari 检查器时,它显示 580
.所以那个函数的返回是不正确的,我想知道是什么原因。
演示:
http://users.telenet.be/prullen/this/
http://users.telenet.be/prullen/that/
内容(单击红色选项卡时的灰色区域)应该在加载时通过负边距隐藏,但由于 jquery 的(不正确?)返回,它不在这些主题之一中(略微显示)。
console.log 调用是:
console.log('window width / 2 = ' + $(window).width()/2);
console.log('doc width /2 = ' + $(document).width()/2);
console.log('width = ' + defaults.boxWidth);
console.log('width = ' + $slider.css('width'));
console.log('width = ' + $slider.width());
console.log('width = ' + $slider.outerWidth());
console.log('width = ' + $slider.outerWidth(true));
最佳答案
.outerWidth([includeMargin])
- 返回元素的宽度,以及左右填充、边框和(可选)边距(以像素为单位)。
设置[includeMargin]
true
的选项将边距添加到此数字:
.outerWidth(true)
- 返回元素的宽度,以及左右填充、边框、和边距 , 以像素为单位。
.width()
- 返回一个无单位的像素值 不包括 左右填充、边框或边距。
根据 jQuery 文档,仅限 .width()
之间的区别和 .css(width)
是后者包括width()
的单位('px')只需返回数字(不带“px”)。
因此,如果根据定义,width()
不包括边距、填充和边框,那么 css(width)
也不包括
I have a jQuery script, this sets an element's width to 50% via the normal CSS width attribute.
If I then output the
outerWidth(true)
of that element to get the exact pixel value, it sometimes does not correspond to$(window).width()/2
.
outerWidth(true)
查看相同的元素时,现在您正在为数字添加边距、填充或边框。
$(window).width()/2
将是 580。假设您的元素也是 580 像素,
.outerWidth(true)
将添加(或减去)边距,添加边框并为元素本身添加填充,因此除非边距,填充和边框都为零,否则您不会得到 580。
.width()
代替
.outerWidth(true)
保持所有主题的一致性。
body
上的边距造成你所描述的。在您的
consolidated-screen-2.css
内工作表您有 20 像素的左边距。
.width()
。 (忽略边距)在您的所有计算中。
body
margin 为零。
console.log('width = ' + $slider.outerWidth(true));
= 1188
body
左右边距)= 1228(与标尺相同)
console.log('width = ' + $slider.css('width'));
= 594 像素;
body
左边距)= 614(与标尺相同)
console.log('width = ' + $slider.width());
= 594
body
左边距)= 614(与标尺相同)
关于jquery - $(element).outerWidth(true) 根据主题报告不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983278/
我只是想知道 window.outerWidth 和 $(window).outerWidth() 之间的区别,如果有的话。 我真的无法在 Google 上找到太多关于此的信息,所以我想我会吸引大众。
我有以下元素。 var e=$('', {"class":'someClass',text:'Hello'}); 我想得到它的外部宽度。看来我不能只使用 e.outerWidth() 因为它需要先写入
window.outerHeight/window.outerWidth 这个值不是以像素为单位吗? http://www.w3schools.com/jsref/tryit.asp?filename
我正在使用一些 jQuery 在元素的顶部和左侧添加负边距,以便将其居中对齐到 div 中。 $('.centred').each(function(event) {
我创建了一个弹出框类。每个弹出窗口都可以有可变内容,并出现在可变位置,具体取决于光标打开时的位置,因此弹出窗口看起来像是从单击的链接或按钮中弹出的。 问题是如果光标离边框太近,弹出窗口可能部分位于屏幕
我正在尝试对 jQuery UI MultiSelect Widget 进行一些调整. 我遇到的一个问题是在我刷新控件后主控件有点太宽了。查看代码,宽度设置为: var width = this.el
我有一个 jQuery 脚本,它通过普通的 CSS 宽度属性将元素的宽度设置为 50%。 如果我然后输出 outerWidth(true)要获得准确的像素值,它有时与 $(window).width(
在 MooTools 中是否有与 jQuery 的 outerWidth 等效的东西? 我想得到 block 的width + margin-width + padding-width + borde
我正在使用 window.outerWidth 来获取工作正常的窗口宽度。然而,问题是如果您将页面加载到未聚焦的选项卡中,window.outerWidth 将为 0。有什么解决方法吗? Chrome
精简版 如果我不设置水平边距,我的代码运行良好。这就是为什么我的代码使用 .width() 来获取和设置元素宽度,但我不能对 .outerWidth() 做同样的事情,因为它是只读的。有办法设置吗?
我有一个水平滚动的图片库。 包含所有图像的宽度通过汇总所有子图像的 outerWidths 动态生成。 画廊的设置方式如下: Half Bubble - $50.00 Bub
我很困惑。 http://jsfiddle.net/pyn8s/ 点击登录按钮。等待弹出窗口出现。注意它的位置。再次单击登录按钮。看着它向左移动。 (正确位置)再次单击登录按钮。请注意,它不再移动了。
是否可以使用dom获取元素outerWidth? Ej: var width = document.getElementById('myDiv').outerWidth; 最佳答案 没有,但你可以获得
我检查了 Firebug 中的 window 对象。 window.innerWidth 和 window.outerWidth 均为 1230。 这两个值有什么区别? 最佳答案 来自 Mozilla
另一位 Web 开发人员希望卡住最左边的列并允许滚动表格。他找到了这段代码并将其实现到他客户的网站中。该代码完成其工作并按预期工作。 但是,他添加了一些 @media 查询,以使表格的响应速度更快。当
经过编辑使问题更简洁。 我可以在 .css() 方法中使用 outerWidth() 值来将“#item”的位置偏移其宽度的两倍。 例如: $("#item").css("left", - (2 x
我正在根据这篇文章中建议的内容使用调整 Jqgrid 列宽度, Jqgrid Column width According to Its Content 在 jqgrid 的 loadcomplete
我想绝对定位我的动画扣,这是我的 html: lorem.ipsum LOREMSIT.DOLOR []Lorem ipsum dolor sit amet, consect
在 chrome 中打开控制台(同时在 SO 上)并复制 innerWidth + "|"+outerWidth + "|"+ screen.width,对我来说这将返回 2133|1920|1920
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-top
我是一名优秀的程序员,十分优秀!