Summary of JavaScript window manipulation Methods

Source: Internet
Author: User

In the previous introduction, we discussed the close () method:
Copy codeThe Code is as follows:
Win = window. open ("http://www.jb51.net/", "js ");
Win. close ();

JavaScript provides many methods and attributes that we can use to control the window.
Move, scroll, and change size
The following method (N4 +, IE4 +) is used to move, scroll, and change the size of a specific window:
Copy codeThe Code is as follows:
// Move the screen position of the window to the specified offset x and y (absolute movement)
Window. moveTo (iX, iY)
// Move the screen position of the window to the specified offset x and y (relative movement)
Window. moveBy (iX, iY)
// Scroll the screen position of the window to the specified offset x and y (absolute scroll)
Window. scrollTo (iX, iY)
// Scroll the screen position of the window to the specified offset x and y (relative scrolling)
Window. scrollBy (iX, iY)
// Change the window size to the specified height and width (absolutely change the size)
Window. resizeTo (iWidth, iHeight)
// Change the window size to the specified height and width (relative to the size of the window)
Window. resizeBy (iX, iY)

Note that these methods belong to window objects, so they are intelligently executed in the current window or other Windows that can be referenced. If you want to dynamically set the position and size of the window, you can use the move and resize methods after the window is created.
// Form
Note: It is impossible to control a window containing other server pages.
Maximize window
Now we will introduce how to create a button to maximize the window after clicking.
Let's take a look at the HTML and JavaScript code for this button:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Function maximizeWin (){
If (window. screen ){
Var aw = screen. availWidth;
Var ah = screen. availHeight;
Window. moveTo (0, 0 );
Window. resizeTo (aw, ah );
}
}
// -->
</SCRIPT>
<FORM> <input type = "button" VALUE = "Maximize" onClick = "maximizeWin ()"> </FORM>

Note that the resizeTo () method references the size of the entire window. // Form
A floating Advertisement
On the website, the mobile advertising window can attract the attention of viewers. We can call the following function to make the window move left and right:
Copy codeThe Code is as follows:
Function makeAd (){
Window. open ("adpage.html", "ad", "width = 468, innerWidth = 468, height = 80, innerHeight = 80, left = 0, top = 0 ");
}

The following is the code of the page adpage.html:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Function startAd (){
If (window. screen ){
Pos = 0;
Aw = screen. availWidth;
Window. moveTo (pos, 0 );
TimerID = setInterval ("moveAd ()", 50 );
}
}
Function moveAd (){
If (pos <= 0) inc = 5;
// 5-so it doesn' t pass the right edge
// 10-accounts for the window chrome
If (pos + 468 + 10 + 5> aw) inc =-5;
Pos + = inc; a380fa.com
Window. moveTo (pos, 0 );
}
Window. onload = startAd;
// -->
</SCRIPT>

When the page adpage.html is loaded, the startAD () function is executed. If your browser supports window. screen objects, the window can be moved, because we need to use window. screen to calculate the screen width. The window slides on the upper boundary of the screen from the upper left corner (pos = 0) to the upper right corner.
Use the built-in setInterval () function to move 5 pixels in the advertising window every 50 milliseconds. If you click "stop ",

Related Article

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.