gpt4 book ai didi

jquery - 如何在没有(减号)滚动条的情况下获得屏幕宽度?

转载 作者:IT王子 更新时间:2023-10-29 03:25:23 28 4
gpt4 key购买 nike

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

Firebug 告诉我 body 宽度是 1280px。

这些在 Firefox 中都可以正常工作:

console.log($('.element').outerWidth() );
console.log($('.element').outerWidth(true) );

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

它们都返回1263px,这是我要找的值。

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

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

最佳答案

.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

P.S:请注意,要可靠地使用 scrollWidth,您的元素不应水平溢出

jsBin demo


您也可以使用 .innerWidth() 但这仅适用于 body 元素

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

关于jquery - 如何在没有(减号)滚动条的情况下获得屏幕宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8339377/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com