2011-11-17 138 views
13

有没有办法可靠地告诉浏览器的包含滚动条的浏览窗口宽度,但浏览器窗口的其余部分不是)?如何通过滚动条可靠地获取屏幕宽度

无属性的上市here告诉我屏幕的宽度,包括滚动条(如果存在)

+0

为什么需要包含滚动条的宽度?更不用说,一些浏览器在视口的左右两侧也有1-4个像素的边界。我甚至不明白为什么人们需要'outerWidth'和'outerHeight' ... – animuson

+0

@animuson:我动态地设置了一些元素的宽度,并且取决于浏览器呈现布局的方式(版本,屏幕大小等) ),有时会出现一个滚动条,后来消失,页面上出现“滚动条阴影”。如果我提前知道滚动条的宽度,这将会简单得多。 – Goro

+0

参见http://stackoverflow.com/q/8339377/1136253 –

回答

5

只要身体是100%,document.body.scrollWidth将工作。

演示:http://jsfiddle.net/ThinkingStiff/5j3bY/

HTML:

<div id="widths"></div> 

CSS:

body, html 
{ 
margin: 0; 
padding: 0; 
width: 100%; 
} 

div 
{ 
height: 1500px; 
} 

脚本:

var widths = 'viewport width (body.scrollWidth): ' 
    + document.body.scrollWidth + '<br />' 
    + 'window.innerWidth: ' + window.innerWidth + '<br />'; 

document.getElementById('widths').innerHTML = widths; 

我把一个高大的DIV在演示强制滚动酒吧。

+0

这似乎并不一致。 JSFiddle给了我body.scrollWidth:580,window.innerWidth:588.我将我自己的解决方案添加到这个线程。 – YoYoYonnY

+0

@YoYoYonnY我认为这可能是JSFiddle的iFrame问题。让我知道你发现了什么。 – ThinkingStiff

+0

问题仍然存在于Ubuntu Chrome: 'document.body.scrollWidth:1351,window.innerWidth:1366' – YoYoYonnY

0

使用jQuery,您可以通过获取宽度差计算浏览器的滚动条的宽度时overflow: hidden设置和overflow: scroll设置。 宽度的差异将是滚动条的大小。

这是一个simple example显示你如何做到这一点。

+0

我在网上看到了很多关于这方面的例子,但对此很谨慎,因为它好像和其他一些样式表有冲突该页面,它可能不会给出正确的结果。 – Goro

+0

确定你必须尝试它是否适合你的页面。正如ThinkingStiff提到的,将身体宽度设置为100%很重要。 – Fabian

6

我假设你想知道包含滚动条的视口宽度,因为它自己的屏幕没有滚动条。事实上,屏幕宽度和高度将是电脑屏幕分辨率本身,所以我不确定你的意思是屏幕宽度与滚动条

但是,只有页面(和滚动条)呈现给用户的区域,即没有浏览器菜单,没有书签或任何其他内容,只有页面呈现的区域才是这种滚动条可能存在的地方。

假设您需要这样做,您可以在考虑滚动条大小的同时测量客户端浏览器视口大小。

首先不要忘记将身体标记设置为100%的宽度和高度,以确保测量的准确性。

body { 
width: 100%; 
// if you wish to also measure the height don't forget to also set it to 100% just like this one. 
} 

之后,您可以随意测量宽度。

样品

// First you forcibly request the scroll bars to be shown regardless if you they will be needed or not. 
$('body').css('overflow', 'scroll'); 

// Viewport width with scroll bar. 
var widthWithScrollBars = $(window).width(); 

// Now if you wish to know how many pixels the scroll bar actually has 
// Set the overflow css property to forcibly hide the scroll bar. 
$('body').css('overflow', 'hidden'); 

// Viewport width without scroll bar. 
var widthNoScrollBars = $(window).width(); 

// Scroll bar size for this particular client browser 
var scrollbarWidth = widthWithScrollBars - widthNoScrollBars; 

// Set the overflow css property back to whatever value it had before running this code. (default is auto) 
$('body').css('overflow', 'auto'); 

希望它能帮助。

8

我想出如何使用一些代码准确地获得与滚动视口宽度:http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript

内将这个你$(document).ready(function()

$(document).ready(function(){ 
    $(window).on("resize", function(){ 
     function viewport() { 
      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' ] }; 
     } 
    }); 
    // Get the correct window sizes with these declarations 
    windowHeight = viewport().height; 
    windowWidth = viewport().width; 
}); 

它做什么:

当您的页面“准备就绪”或调整大小时,函数将计算正确的窗口高度和宽度(包括滚动条)。

0

你可以得到滚动窗口宽度,这样:

function scrollbar_width() { 
      if (jQuery('body').height() > jQuery(window).height()) { 

       /* Modified from: http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php */ 
       var calculation_content = jQuery('<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); 
      } 
      return 0; 
     } 
1

目前新大众VH css3属性将显示全尺寸,包括滚动条。

body { width:100vw; height:100vh; }

有一些讨论,网上,如果这是一个错误或没有。

1

滚动条所以“窗口其余部分”是什么?

但是,有一种方法可以做到另一种包装div,在一切正常的身体上,身体上有overflow:none; height:100%; width:100%;,包装div也有100%的宽度和高度。并溢出滚动。所以现在...包装的宽度将视口的宽度

见例http://jsfiddle.net/techsin/8fvne9fz/

html,body { 
    height: 100%; 
    overflow: hidden; 
} 

.wrapper { 
    width: 100%; 
    height: 100%; 
    overflow: auto; 
} 
0

这是我去掉“滚动条阴影”的解决方案,因为scrollWidth没有工作对我来说:

canvas.width = element.offsetWidth; 
canvas.height = element.offsetHeight; 
canvas.width = element.offsetWidth; 
canvas.height = element.offsetHeight; 

这很容易,但它的工作原理。请务必添加注释,解释为什么您分配两次相同的值:)