[英]How to get the actual width available for content on the browser window?
I'm positioning my elements using JavaScript. 我正在使用JavaScript定位元素。 In order to do it perfectly I have to get the amount of horizontal space available.
为了做到完美,我必须获得可用的水平空间。 Badly,
$(window).width()
does not take in account the scrollbar width. 严重的是,
$(window).width()
没有考虑滚动条的宽度。 The result is this: 结果是这样的:
bad http://dl.dropbox.com/u/62862049/Screenshots/fb.png 错误的http://dl.dropbox.com/u/62862049/Screenshots/fb.png
Here, "Pagina 1" is contained in a small div that was supposed to align with the right border of the window. 在这里,“ Pagina 1”包含在一个小div中,该div应该与窗口的右边界对齐。 Well, it does - literally - ignoring the scrollbar, which covers part of the div, throwing the "1" of "Página 1" to the next line.
好吧,实际上,它确实忽略了覆盖div一部分的滚动条,将“Página1”的“ 1”抛出到下一行。
This is similar to two previous questions: 这类似于先前的两个问题:
how to get innerWidth of an element in jquery WITHOUT scrollbar 如何在没有滚动条的情况下获取jQuery中元素的innerWidth
and 和
How to get screen width without (minus) scrollbar? 如何在没有(减号)滚动条的情况下获得屏幕宽度?
use this function 使用这个功能
function scrollbar_width() {
var calculation_content = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
jQuery('body').append(calculation_content);
var width_one = jQuery('div', calculation_content).innerWidth();
calculation_content.css('overflow-y', 'scroll');
var width_two = jQuery('div', calculation_content).innerWidth();
jQuery(calculation_content).remove();
return (width_one - width_two);
}
now calculate available with 现在计算可用
var availableWidth = $(window).width() - scrollbar_width();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.