开发者

screen width vs visible portion

开发者 https://www.devze.com 2023-02-10 11:03 出处:网络
I have a little problem with JavaScript, to get the screen width we use screen.width which returns the overall screen resolution, is there a command to get the re开发者_C百科solution of the visible po

I have a little problem with JavaScript, to get the screen width we use screen.width which returns the overall screen resolution, is there a command to get the re开发者_C百科solution of the visible portion of the browser, like when the browser is not maximized?


function width(){
   return window.innerWidth 
       || document.documentElement.clientWidth 
       || document.body.clientWidth 
       || 0;
}

function height(){
   return window.innerHeight 
       || document.documentElement.clientHeight 
       || document.body.clientHeight 
       || 0;
}

Use them to return the height() or width() of the visible window.

JSFiddle example.


The area you're describing is the viewport, and can typically be accessed by using window.innerWidth or window.innerHeight in modern browsers. IE6 is a bit different, but more information on handling the viewport width for all browsers can be found in this tutorial on obtaining viewport size.


I saw that the question was answered, but here is the code I used with an image to ilustrate.

function height(){
return(window.innerHeight)?
window.innerHeight:
document.documentElement.clientHeight||document.body.clientHeight||0;
}
$(document).ready(function(){
$('.first, .second').css('height',height());
});
$(window).resize(function() {
$('.first, .second').css('height',height());
});

JSFiddle Example

0

精彩评论

暂无评论...
验证码 换一张
取 消