我有一个水平滚动的网站,其中有一个带有id="bookWrap"
的div,里面填充了inline-block
元素,因此宽度对我来说是未知的,但它远远超出了视口。
当我尝试通过$('#bookWrap').width()
来获取该元素的宽度时,jQuery返回的是我的视口宽度,即1680,而不是元素的实际宽度。
我该如何获取延伸到视口之外的元素的真实宽度?
我有一个水平滚动的网站,其中有一个带有id="bookWrap"
的div,里面填充了inline-block
元素,因此宽度对我来说是未知的,但它远远超出了视口。
当我尝试通过$('#bookWrap').width()
来获取该元素的宽度时,jQuery返回的是我的视口宽度,即1680,而不是元素的实际宽度。
我该如何获取延伸到视口之外的元素的真实宽度?
width()
和outerWidth()
都在某种程度上截断了视口宽度。alert(getTotalWidth("#bookWrap DIV"));
function getTotalWidth(selector) {
var totalWidth = 0;
$(selector).each(function() {
totalWidth += $(this).outerWidth();
});
return totalWidth;
}
$(selector).children().each(..)
。 - ndscvar realWidth = document.getElementById('#bookWrap').scrollWidth;
$('*').each(function () {
var viewportWidth = 768;
if($(this).outerWidth() > viewportWidth ){
//prints element in console so you can check what's the element
console.log($(this)[0]);
}
});
也许你应该计算宽度:
var width = 0;
$('#bookWrap div').each(function(){
width += $(this).width();
});
alert(width);