Code for obtaining the window size:
Copy codeThe Code is as follows:
Var pageWidth = window. innerWidth,
Var pageHeight = window. innerHeight;
If (typeof pageWidth! = "Number "){
If (document. compatMode = "number "){
PageWidth = document.doc umentElement. clientWidth;
PageHeight = document.doc umentElement. clientHeight;
} Else {
PageWidth = document. body. clientWidth;
PageHeight = document. body. clientHeight;
}
}
We first pay the values of window. innerWidth and window. innerHeight respectively to pageWidth and pageHeight. Then, check whether the value saved in pageWidth is a numeric value. If not, use document. compatMode to determine whether the page is in standard mode. If yes, use the values of document.documentelement.clientwidthand document.doc umentElement. clientHeight separately. Otherwise, the values of document. body. clientWidth and document. body. clientHeight are used.
Code for obtaining the window position:
Copy codeThe 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 two examples aim to obtain the left and top positions of the window. First, use the binary operator to determine whether the screenLeft and screenTops attributes exist. If they exist (in IE, Safari, Opera, and Chrome ), take the values of these two attributes. If it does not exist (in Firefox), take the screenX and screenY values.