如何获取屏幕宽度没有(减)滚动条?(How to get screen width without (minus) scrollbar?)

编程入门 行业动态 更新时间:2024-10-26 05:21:25
如何获取屏幕宽度没有(减)滚动条?(How to get screen width without (minus) scrollbar?)

我有一个元素,需要它的宽度没有(!)垂直滚动条。

Firebug告诉我身体宽度是1280px。

在Firefox中都可以正常工作

console.log($('.element').outerWidth() ); console.log($('.element').outerWidth(true) ); $detour = $('.child-of-element').offsetParent(); console.log( $detour.innerWidth() );

他们都返回1263px ,这是我正在寻找的价值。

但是所有其他浏览器给我1280px

有没有(!)垂直滚动条的跨浏览器方式获得全屏宽度?

I have an element and need it's width without(!) vertical scrollbar.

Firebug tells me body width is 1280px.

Either of these work fine in Firefox:

console.log($('.element').outerWidth() ); console.log($('.element').outerWidth(true) ); $detour = $('.child-of-element').offsetParent(); console.log( $detour.innerWidth() );

They all return 1263px, which is the value I'm looking for.

However all other browser give me 1280px.

Is there a cross browser way to get a fullscreen width without(!) vertical scrollbar?

最满意答案

.prop("clientWidth")和.prop("scrollWidth")

var actualInnerWidth = $("body").prop("clientWidth"); // El. width minus scrollbar width var actualInnerWidth = $("body").prop("scrollWidth"); // El. width minus scrollbar width

JavaScript中

var actualInnerWidth = document.body.clientWidth; // El. width minus scrollbar width var actualInnerWidth = document.body.scrollWidth; // El. width minus scrollbar width

PS:请注意,为了可靠地使用scrollWidth元素不应该水平溢出

jsBin演示


你也可以使用.innerWidth()但这只能在body元素上工作

var innerWidth = $('body').innerWidth(); // Width PX minus scrollbar

.prop("clientWidth") and .prop("scrollWidth")

var actualInnerWidth = $("body").prop("clientWidth"); // El. width minus scrollbar width var actualInnerWidth = $("body").prop("scrollWidth"); // El. width minus scrollbar width

in JavaScript:

var actualInnerWidth = document.body.clientWidth; // El. width minus scrollbar width var actualInnerWidth = document.body.scrollWidth; // El. width minus scrollbar width

P.S: Note that to use scrollWidth reliably your element should not overflow horizontally

jsBin demo


You could also use .innerWidth() but this will work only on the body element

var innerWidth = $('body').innerWidth(); // Width PX minus scrollbar

更多推荐

px,宽度,log,console,width,电脑培训,计算机培训,IT培训"/> <meta name="descr

本文发布于:2023-07-14 23:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   滚动条   屏幕   scrollbar   width

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!