Implementation Code of random and non-targeted floating js Images

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Interesting floating. You don't need too much code to share with us.
<Html>
<Head> random floating
<Style type = "text/css">
# Divimg {
/*
Absolute positioning of images
*/
Position: absolute;
}
</Style>
</Head>
<Body>
<Div id = "divimg">

</Div>
<Script language = "javascript" type = "text/javascript">
// Obtain all div objects of an image
Var divimg = document. getElementById ("divimg ");
// Set the start position
Var x = 0, y = 0;
// Set the forward speed of the image
Var xSpeed = 8, ySpeed = 2;
// Set the floating height and width of the image
Var w=document.doc umentElement. clientWidth-205, h = document. body. clientHeight-108;
// Alert (w );
Function floatimg (){
// Compare whether the border is reached. If the border is reached, the direction of the image is reversed. If the border is not reached, move forward.
If (x <0 | x> w) xSpeed =-xSpeed;
If (y <0 | y> h) ySpeed =-ySpeed;
X + = xSpeed;
Y + = ySpeed;
SetTimeout ("floatimg ()", 1000 );
Var n = divimg. style. top = Math. round (Math. random () * h) + "px ";
Var m = divimg. style. left = Math. round (Math. random () * w) + "px ";
}
Floatimg ();
</Script>
</Body>
</Html>

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.