如何在没有(负)滚动条的情况下获取屏幕宽度?

发布于 2024-12-18 20:23:22 字数 441 浏览 3 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

错爱 2024-12-25 20:23:22

.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 
无声情话 2024-12-25 20:23:22

您可以通过简单地编写来使用 vanilla javascript:

var width = el.clientWidth;

您还可以使用它来获取文档的宽度,如下所示:

var docWidth = document.documentElement.clientWidth || document.body.clientWidth;

来源: MDN

您还可以获取整个窗口的宽度,包括滚动条,如下所示:

var fullWidth = window.innerWidth;

但是,并非所有浏览器都支持这一点,因此作为后备,您可以想要使用docWidth 如上,并添加 滚动条宽度

来源:MDN

You can use vanilla javascript by simply writing:

var width = el.clientWidth;

You could also use this to get the width of the document as follows:

var docWidth = document.documentElement.clientWidth || document.body.clientWidth;

Source: MDN

You can also get the width of the full window, including the scrollbar, as follows:

var fullWidth = window.innerWidth;

However this is not supported by all browsers, so as a fallback, you may want to use docWidth as above, and add on the scrollbar width.

Source: MDN

驱逐舰岛风号 2024-12-25 20:23:22

在没有滚动条的情况下获取正确宽度和高度的最安全位置是从 HTML 元素。试试这个:

var width = document.documentElement.clientWidth
var height = document.documentElement.clientHeight

浏览器支持相当不错,IE 9 及更高版本都支持此功能。对于旧版 IE,请使用此处提到的众多后备方案之一。

The safest place to get the correct width and height without the scrollbars is from the HTML element. Try this:

var width = document.documentElement.clientWidth
var height = document.documentElement.clientHeight

Browser support is pretty decent, with IE 9 and up supporting this. For OLD IE, use one of the many fallbacks mentioned here.

苏别ゝ 2024-12-25 20:23:22

以下是一些假设 $elementjQuery 元素的示例:

// Element width including overflow (scrollbar)
$element[0].offsetWidth; // 1280 in your case

// Element width excluding overflow (scrollbar)
$element[0].clientWidth; // 1280 - scrollbarWidth

// Scrollbar width
$element[0].offsetWidth - $element[0].clientWidth; // 0 if no scrollbar

Here are some examples which assume $element is a jQuery element:

// Element width including overflow (scrollbar)
$element[0].offsetWidth; // 1280 in your case

// Element width excluding overflow (scrollbar)
$element[0].clientWidth; // 1280 - scrollbarWidth

// Scrollbar width
$element[0].offsetWidth - $element[0].clientWidth; // 0 if no scrollbar
ぺ禁宫浮华殁 2024-12-25 20:23:22

试试这个:

$('body, html').css('overflow', 'hidden');
var screenWidth1 = $(window).width();
$('body, html').css('overflow', 'visible');
var screenWidth2 = $(window).width();
alert(screenWidth1); // Gives the screenwith without scrollbar
alert(screenWidth2); // Gives the screenwith including scrollbar

您可以使用此代码获取有或没有滚动条的屏幕宽度。
在这里,我更改了 body 的溢出值并获取有和没有滚动条的宽度。

Try this :

$('body, html').css('overflow', 'hidden');
var screenWidth1 = $(window).width();
$('body, html').css('overflow', 'visible');
var screenWidth2 = $(window).width();
alert(screenWidth1); // Gives the screenwith without scrollbar
alert(screenWidth2); // Gives the screenwith including scrollbar

You can get the screen width by with and without scroll bar by using this code.
Here, I have changed the overflow value of body and get the width with and without scrollbar.

素衣风尘叹 2024-12-25 20:23:22

我遇到了类似的问题,并使用 width:100%; 为我解决了这个问题。在尝试回答这个问题并意识到 的本质将使这些 javascript 测量工具在不使用某些复杂功能的情况下变得不准确后,我得出了这个解决方案。 100% 执行是在 iframe 中处理它的一种简单方法。我不知道你的问题,因为我不确定你正在操作哪些 HTML 元素。

I experienced a similar problem and doing width:100%; solved it for me. I came to this solution after trying an answer in this question and realizing that the very nature of an <iframe> will make these javascript measurement tools inaccurate without using some complex function. Doing 100% is a simple way to take care of it in an iframe. I don't know about your issue since I'm not sure of what HTML elements you are manipulating.

二货你真萌 2024-12-25 20:23:22

这些解决方案都不适合我,但是我能够通过获取宽度并减去 滚动条的宽度。我不确定它的跨浏览器兼容性如何。

None of these solutions worked for me, however I was able to fix it by taking the width and subtracting the width of the scroll bar. I'm not sure how cross-browser compatible this is.

铜锣湾横着走 2024-12-25 20:23:22

我知道这是一个老问题,但建议的答案也不适合我。我在网上找到了以下代码,并在 Chrome、Firefox 和 Safari 中进行了测试,似乎运行良好。

if (window.matchMedia("(max-width: 1339px)").matches) { // do some if matches }

我想我会在这里提到它,以防它对其他人有帮助。

I know this is an old question, but the suggested answer didn't work for me either. I found the following code online and tested it in Chrome, Firefox and Safari where it seems to work fine.

if (window.matchMedia("(max-width: 1339px)").matches) { // do something if matches }

Thought I would mention it here in case it helps someone else.

迷你仙 2024-12-25 20:23:22

这是我用的

function windowSizes(){
    var e = window,
        a = 'inner';
    if (!('innerWidth' in window)) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return {
        width: e[a + 'Width'],
        height: e[a + 'Height']
    };  
}

$(window).on('resize', function () {
    console.log( windowSizes().width,windowSizes().height );
});

Here is what I use

function windowSizes(){
    var e = window,
        a = 'inner';
    if (!('innerWidth' in window)) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return {
        width: e[a + 'Width'],
        height: e[a + 'Height']
    };  
}

$(window).on('resize', function () {
    console.log( windowSizes().width,windowSizes().height );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文