browser window event: and browse various size introduction

Source: Internet
Author: User

(trigger) Window.onload;   Window.onscroll;                Window.onresize; (compatible) page viewable area size, full page size, scrolling distance (instance) Ad block Height Dynamic center, back to the top of alert (window.navigator.userAgent); Detecting browser versions
window.location= ' http://www.miaov.com/'; Read-write Address bar browser window event: Window.onload=function () {}//Document post-Load event window.onscroll=function () {}//ScrollBar event Window.onresize=funct Ion () {}//Window size Change Event JS to determine the browser's aspect (can be copied directly using!) )//web viewable area size
var clientwidth = Document.documentElement.clientWidth | | Document.body.clientWidth;
var clientheight = Document.documentElement.clientHeight | | Document.body.clientHeight;
Web visual area size: including Edge document.body.offsetWidth;
Document.body.offsetHeight;
Full page
var scrollwidth = Math.max (Document.documentElement.scrollWidth, document.body.scrollWidth);
var scrollheight = Math.max (Document.documentElement.scrollHeight, document.body.scrollHeight);
Scrolling distance
var scrolltop=document.documentelement.scrolltop| | Document.body.scrollTop;
var scrollleft=document.documentelement.scrollleft| | Document.body.scrollLeft;
Web page Body part
Window.screentop;
Window.screenleft;
Screen resolution
Window.screen.height;
Window.screen.width;
On-Screen Available workspaces
Window.screen.availHeight;
Window.screen.availWidth; JS gets the browser height and width value (multi-browser) JS gets the browser height and width values, as much as possible to consider multi-browser.
in IE:
Document.body.clientWidth ==> Body Object width
Document.body.clientHeight ==> Body Object Height
Document.documentElement.clientWidth ==> Visible Area width
Document.documentElement.clientHeight ==> Visible Area height
in Firefox:
Document.body.clientWidth ==> Body Object width
Document.body.clientHeight ==> Body Object Height
Document.documentElement.clientWidth ==> Visible Area width
Document.documentElement.clientHeight ==> Visible Area height
in Opera:
Document.body.clientWidth ==> Visible Area width
Document.body.clientHeight ==> Visible Area height
Document.documentElement.clientWidth ==> The width of the page object (that is, the body object width plus the margin width)
Document.documentElement.clientHeight ==> Page Object height (i.e. body object height plus margin height)
there is no standard for defining the
IE is:

Document.documentElement.clientWidth ==> 0
Document.documentElement.clientHeight ==> 0
Firefox is:
Document.documentElement.clientWidth ==> The width of the page object (that is, the body object width plus the margin width)
Document.documentElement.clientHeight ==> Page Object height (i.e. body object height plus margin height)
Opera is:
Document.documentElement.clientWidth ==> The width of the page object (that is, the body object width plus the margin width)
Document.documentElement.clientHeight ==> Page Object height (i.e. body object height plus margin height)




html precise positioning: Scrollleft,scrollwidth,clientwidth,offsetwidth
ScrollHeight: Gets the scroll height of the object. ScrollWidth: Gets the scroll width of the object scrollleft: Sets or gets the distance between the left edge of the object and the leftmost of the currently visible content in the window
ScrollTop: Sets or gets the distance between the top of the object and the top of the visible content in the window

Offsetheight: Gets the height of the object relative to the layout or the parent coordinate specified by the parent coordinate OffsetParent property offsetleft: Gets the left position of the calculated object relative to the layout or the parent coordinate specified by the OffsetParent property
OffsetTop: Gets the computed top position of the object relative to the layout or the parent coordinate specified by the OffsetTop property
Event.clientx horizontal coordinates of relative documents
Event.clienty vertical coordinates of relative documents
Event.offsetx horizontal coordinates relative to the container
Event.offsety the vertical coordinate of the relative container
Document.documentElement.scrollTop The vertical scrolling value
Event.clientx+document.documentelement.scrolltop the horizontal coordinates of the relative document + the amount of scrolling in the vertical direction example://------------------------------------- ------------------------------------------------------
Flavor Example: Keep the top and bottom center in the window ************position:fixed (not supported in low version IE) window.onresize=window.onload=window.onscroll= function () {
var Odiv=document.getelementbyid (' Div1 ');
var scrolltop=document.documentelement.scrolltop| | Document.body.scrollTop;
var t (document.documentelement.clientheight-odiv.offsetheight)/2;

odiv.style.top=scrolltop+t+ ' px '; The motion effect is beating, using the next function
Startmove (Odiv,{top:scrolltop+t});
}

Motion function: Start
function GetStyle (obj, attr) {
if (Obj.currentstyle) {
return obj.currentstyle[attr];
} else {
return getComputedStyle (obj, false) [attr];
}
}

function Startmove (obj, JSON, FN) {
Clearinterval (Obj.timer);
Obj.timer = setinterval (function () {
var bstop = true; This time the movement is over--all the values have arrived.
for (Var attr in JSON) {
1. Take the current value
var icur = 0;

if (attr = = ' opacity ') {
Icur = parseint (parsefloat (GetStyle (obj, attr)) * 100);
} else {
Icur = parseint (GetStyle (obj, attr));
}

2. Calculation speed
var ispeed = (json[attr]-icur)/8;
Ispeed = ispeed > 0? Math.ceil (ispeed): Math.floor (Ispeed);

3. Detection Stop
if (icur! = Json[attr]) {
Bstop = false;
}

if (attr = = ' opacity ') {
Obj.style.filter = ' alpha (opacity: ' + (Icur + ispeed) + ') ';
Obj.style.opacity = (icur + ispeed)/100;
} else {
OBJ.STYLE[ATTR] = icur + ispeed + ' px ';
}
}

if (bstop) {
Clearinterval (Obj.timer);

if (FN) {
FN ();
}
}
}, 30)
}
Motion function: End
//-------------------------------------------------------------------------------------------


Example of taste: Back to top *******************window.onload=function ()
{
var Obtn=document.getelementbyid (' btn1 ');
var bsys=true;
var timer=null;

How to detect a user dragging a scroll bar
Window.onscroll=function ()
{
if (!bsys)
{
Clearinterval (timer);
}
Bsys=false;
};

Obtn.onclick=function ()
{
Timer=setinterval (function () {
var scrolltop=document.documentelement.scrolltop| | Document.body.scrollTop;
var ispeed=math.floor (-SCROLLTOP/8);

if (scrolltop==0)
{
Clearinterval (timer);
}

Bsys=true;
Document.documentelement.scrolltop=document.body.scrolltop=scrolltop+ispeed;
}, 30);
};
};//-------------------------------------------------------------------------------------------
Implementation Code
Copy CodeThe code is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<title> Please adjust the browser window </title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</meta>
<body>
&LT;H2 align= "center" > Please resize the browser window <form action= "#" method= "Get" Name= "Form1" Id= "Form1" >
<!--show the actual size of the browser window--
Actual height of browser window: <input type= "text" name= "availheight" size= "4"/><br/>
Actual width of browser window: <input type= "text" name= "availwidth" size= "4"/><br/>
</form>
<script type= "Text/javascript" >
<!--
var winwidth = 0;
var winheight = 0;
function finddimensions ()//functions: Getting dimensions
{
Get window width
if (window.innerwidth)
Winwidth = window.innerwidth;
else if ((document.body) && (document.body.clientWidth))
Winwidth = Document.body.clientWidth;
Get window Height
if (window.innerheight)
Winheight = Window.innerheight;
else if ((document.body) && (document.body.clientHeight))
Winheight = Document.body.clientHeight;
Get the window size by examining the body inside the document
if (document.documentelement && document.documentElement.clientHeight && Document.documentElement.clientWidth)
{
Winheight = Document.documentElement.clientHeight;
Winwidth = Document.documentElement.clientWidth;
}
Results output to two text boxes
Document.form1.availheight.value= Winheight;
Document.form1.availwidth.value= Winwidth;
}
Finddimensions ();
Call a function to get a value
Window.onresize=finddimensions;
-
</script>
</body>
  • JS get screen Various wide-height method (browser compatible)
  • JS and jquery get code for the width and height of various screens
  • JS get Web page height (detailed collation)
  • JS get screen, browser window size, Web height width (Implementation code)
Talking about the ScrollWidth, offsetwidth and clientwidth of Document.documentelement and Document.body

(ScrollHeight, offsetheight, clientheight can also be understood as this article.) )

This is a very complex question, let's imagine:

    • Document.documentElement.scrollWidth
    • Document.documentElement.offsetWidth
    • Document.documentElement.clientWidth
    • Document.body.scrollWidth
    • Document.body.offsetWidth
    • Document.body.clientWidth

There are 6 properties to test, and these 6 attributes should be in 4 cases:

    • No DOCTYPE specified, the content of the Web page does not exceed the window width;
    • No DOCTYPE specified, the content of the webpage exceeds the window width;
    • Specify DOCTYPE, Web content does not exceed window width;
    • Specify DOCTYPE, page content exceeds window width;

These 4 scenarios are then put into several mainstream browsers, assuming there are only 3 types of browsers:

    • Ie
    • Firefox
    • Chrome

Calculate, 6 * 4 * 3, there are 72 kinds of conditions to test, God Ah. And don't expect Firefox to be the same as the Chrome results, and don't expect Firefox to have a confusing result, so it's really annoying.

Simplifying analysis from Application

72 kinds of tests are really annoying, but let's go back and think about what we really want.

I think we want two things:

    • one is scrollwidth (scrollheight), although it is of little use, but it should be much more useful than offsetwidth (offsetheight). It represents the width (height) of the document area, such as a Web page, especially a portal site, which is long, and it is necessary to calculate what is not displayed.
    • The second is the viewport viewport, is ClientWidth, is the window can display the content of the area, that is, we often see the page on the flight ads, flying around, hit the side to bounce the piece.

Test results

The result is very complex, not to mention, here only to say how to use the actual:

    • To use ScrollWidth, take the maximum value of document.documentElement.scrollWidth and document.body.scrollWidth;
    • To use ClientWidth, if document.documentElement.clientWidth > 0, use document.documentElement.clientWidth, otherwise use Document.body.clientWidth.

The expression is:

    • var scrollwidth = Math.max (Document.documentElement.scrollWidth, document.body.scrollWidth);
    • var clientwidth = Document.documentElement.clientWidth | | Document.body.clientWidth;

Do it

ScrollWidth, offsetwidth, clientwidth compatibility test file

browser window event: and browse various size introduction

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.