Home > Web Front-end > JS Tutorial > Detailed explanation of usage of js/jquery to obtain browser scroll bar height and width

Detailed explanation of usage of js/jquery to obtain browser scroll bar height and width

伊谢尔伦
Release: 2017-07-19 15:55:36
Original
3391 people have browsed it

Get the height and width of the visible area of ​​the browser window. Friends who need the height of the scroll bar can refer to it.

In IE, the browser display window size can only be obtained as follows: Copy the code as follows

document.body.offsetWidth 
document.body.offsetHeight
Copy after login

In a browser that declares DOCTYPE, you can use the following to obtain the browser display Window size: Copy the code as follows

document.documentElement.clientWidth 
document.documentElement.clientHeight
Copy after login

IE, FF, and Safari all support this method. Although opera supports this attribute, it returns the page size;
At the same time, all browsers except IE This information is saved in the window object and can be obtained with the following: The code is as follows Copy the code

window.innerWidth 
window.innerHeight
Copy after login

The general method of obtaining the size of the entire web page The code is as follows Copy the code

document.body.scrollWidth 
document.body.scrollHeight
Copy after login

The general method of obtaining the screen resolution height Code Copy the code as follows

window.screen.height 
window.screen.width
Copy after login

Summarize the example

function getViewSizeWithoutScrollbar(){//不包含滚动条 
return { 
width : document.documentElement.clientWidth, 
height: document.documentElement.clientHeight 
} 
} 
function getViewSizeWithScrollbar(){//包含滚动条 
if(window.innerWidth){ 
return { 
width : window.innerWidth, 
height: window.innerHeight 
} 
}else if(document.documentElement.offsetWidth == document.documentElement.clientWidth){ 
return { 
width : document.documentElement.offsetWidth, 
height: document.documentElement.offsetHeight 
} 
}else{ 
return { 
width : document.documentElement.clientWidth + getScrollWith(), 
height: document.documentElement.clientHeight + getScrollWith() 
} 
} 
}
Copy after login

Attached is the most commonly used method of obtaining the width and height of the entire page (requires jquery framework)

$(document).width() < $(&#39;body&#39;).width() ? $(document).width() : $(&#39;body&#39;).width(); 
$(document).height() < $(&#39;body&#39;).height() ? $(document).height() : $(&#39;body&#39;).height();
Copy after login
alert($(window).height()); //浏览器时下窗口可视区域高度
alert($(document).height()); //浏览器时下窗口文档的高度
alert($(document.body).height());//浏览器时下窗口文档body的高度
alert($(document.body).outerHeight(true));//浏览器时下窗口文档body的总高度 包括border padding margin
alert($(window).width()); //浏览器时下窗口可视区域宽度
alert($(document).width());//浏览器时下窗口文档对于象宽度
alert($(document.body).width());//浏览器时下窗口文档body的高度
alert($(document.body).outerWidth(true));//浏览器时下窗口文档body的总宽度 包括border padding margin
Copy after login
alert($(document).scrollTop()); //获取滚动条到顶部的垂直高度
alert($(document).scrollLeft()); //获取滚动条到左边的垂直宽度
Copy after login

The above is the detailed content of Detailed explanation of usage of js/jquery to obtain browser scroll bar height and width. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template