Home > Web Front-end > JS Tutorial > body text

Get the window size, js code compatible with all browsers_javascript skills

WBOY
Release: 2016-05-16 18:03:31
Original
1150 people have browsed it

Code to get the window size:

Copy code The code is as follows:

var pageWidth = window.innerWidth ,
var pageHeight = window.innerHeight;
if(typeof pageWidth != "number"){
if(document.compatMode == "number"){
pageWidth = document.documentElement.clientWidth ;
pageHeight = document.documentElement.clientHeight;
}else{
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
}

We first pay the values ​​of window.innerWidth and window.innerHeight to pageWidth and pageHeight respectively. Then check whether the value saved in pageWidth is a value; if not, use document.compatMode to determine whether the page is in standard mode. If so, the values ​​of document.documentElement.clientWidth and document.documentElement.clientHeight are used respectively. Otherwise, the values ​​of document.body.clientWidth and document.body.clientHeight are used.
Code to get the window position:
Copy code The code is as follows:

var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX;
var topPos = (typeof window.screenTop == "number") ? window.screenTop : window.screenY;

The purpose of these two examples is to obtain the position of the left and top sides of the window. First, use binary operators to determine whether the screenLeft attribute and screenTops attribute exist. If they exist (in IE, Safari, Opera and Chrome), then take The values ​​of these two properties. If not present (in Firefox), the values ​​of screenX and screenY are taken.
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!