Get window position and window size code in JS

Source: Internet
Author: User
Tags numeric value

Get window Size

The code is as follows Copy Code

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 paid the values of Window.innerwidth and Window.innerheight to PageWidth and PageHeight respectively. Then check that the PageWidth is not a numeric value, or, if not, document.compatmode to determine if 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.


Get window Position

The code is as follows Copy Code

var leftpos = (typeof Window.screenleft = "number")? Window.screenLeft:window.screenX;
var toppos = (typeof window.screentop = "number")? Window.screenTop:window.screenY;

These two examples are intended to take the position of the left and the top of the window, first using the two-dollar operator to determine whether the Screenleft property and the Screentops attribute exist, and, if present (in IE, Safari, opera, and Chrome), the values of the two properties. If it does not exist (in Firefox), take the values of Screenx and Screeny.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.