Javascript page floating ads, compatible with IE/ff browsers

Source: Internet
Author: User

This article recommends a good floating page advertisement to all of you. The Calling method is simple. You only need to create a div on the page and specify the id, then you can use js var ad1 = new AdMove ('gg1'); ad1.Run.

The Code is as follows: Copy code

/**
* Floating ads, compatible with IE
* Usage: <div id = "gg1" style = "width: 100px; height: 100px; background-color: red; color: yellow"> advertisement 1 </div>
* <Script type = "text/javascript"> var ad1 = new AdMove ("gg1"); ad1.Run (); </script>
* Sorting:, Thursday, December 12, July 4, 2013
*/
Function addEvent (obj, evtType, func, cap ){
Cap = cap | false;
If (obj. addEventListener ){
Obj. addEventListener (evtType, func, cap );
Return true;
}
Else if (obj. attachEvent ){
If (cap ){
Obj. setCapture ();
Return true;
}
Else {
Return obj. attachEvent ("on" + evtType, func );
}
}
Else {
Return false;
}
}
Function getPageScroll (){
Var xScroll, yScroll;
If (self. pageXOffset ){
XScroll = self. pageXOffset;
}
Else if (document.doc umentElement & document.doc umentElement. scrollLeft ){
XScroll = document.doc umentElement. scrollLeft;
}
Else if (document. body ){
XScroll = document. body. scrollLeft;
}
If (self. pageYOffset ){
YScroll = self. pageYOffset;
}
Else if (document.doc umentElement & document.doc umentElement. scrollTop ){
YScroll = document.doc umentElement. scrollTop;
}
Else if (document. body ){
YScroll = document. body. scrollTop;
}
ArrayPageScroll = new Array (xScroll, yScroll );
Return arrayPageScroll;
}
Function GetPageSize (){
Var xScroll, yScroll;
If (window. innerHeight & window. scrollMaxY ){
XScroll = document. body. scrollWidth;
YScroll = window. innerHeight + window. scrollMaxY;
}
Else if (document. body. scrollHeight> document. body. offsetHeight ){
XScroll = document. body. scrollWidth;
YScroll = document. body. scrollHeight;
}
Else {
XScroll = document. body. offsetWidth;
YScroll = document. body. offsetHeight;
}
Var implements wwidth, implements wheight;
If (self. innerHeight ){
Required wwidth = self. innerWidth;
Optional wheight = self. innerHeight;
}
Else if (document.doc umentElement & document.doc umentElement. clientHeight ){
Required wwidth = document.doc umentElement. clientWidth;
Required wheight = document.doc umentElement. clientHeight;
}
Else if (document. body ){
Required wwidth = document. body. clientWidth;
Optional wheight = document. body. clientHeight;
}
If (yScroll <windowHeight ){
PageHeight = running wheight;
}
Else {
PageHeight = yScroll;
}
If (xScroll <1_wwidth ){
PageWidth = required wwidth;
}
Else {
PageWidth = xScroll;
}
ArrayPageSize = new Array (pageWidth, pageHeight, expires wwidth, expires wheight)
Return arrayPageSize;
}
Var AdMoveConfig = new Object ();
AdMoveConfig. IsInitialized = false;
AdMoveConfig. ScrollX = 0;
AdMoveConfig. ScrollY = 0;
AdMoveConfig. MoveWidth = 0;
AdMoveConfig. MoveHeight = 0;
AdMoveConfig. Resize = function (){
Var winsize = GetPageSize ();
AdMoveConfig. MoveWidth = winsize [2];
AdMoveConfig. MoveHeight = winsize [3];
AdMoveConfig. Scroll ();
}
AdMoveConfig. Scroll = function (){
Var winscroll = getPageScroll ();
AdMoveConfig. ScrollX = winscroll [0];
AdMoveConfig. ScrollY = winscroll [1];
}
AddEvent (window, "resize", AdMoveConfig. Resize );
AddEvent (window, "scroll", AdMoveConfig. Scroll );
Function AdMove (id ){
If (! AdMoveConfig. IsInitialized ){
AdMoveConfig. Resize ();
AdMoveConfig. IsInitialized = true;
}
Var obj = document. getElementById (id );
Obj. style. position = "absolute ";
Var W = AdMoveConfig. MoveWidth-obj.offsetWidth;
Var H = AdMoveConfig. MoveHeight-obj.offsetHeight;
Var x = W * Math. random (), y = H * Math. random ();
Var rad = (Math. random () + 1) * Math. PI/6;
Var kx = Math. sin (rad), ky = Math. cos (rad );
Var dirx = (Math. random () <0.5? 1:-1), diry = (Math. random () <0.5? 1:-1 );
Var step = 1;
Var interval;
This. SetLocation = function (vx, vy ){
X = vx;
Y = vy;
}
This. SetDirection = function (vx, vy ){
Dirx = vx;
Diry = vy;
}
Obj. CustomMethod = function (){
Obj. style. left = (x + AdMoveConfig. ScrollX) + "px ";
Obj. style. top = (y + AdMoveConfig. ScrollY) + "px ";
Rad = (Math. random () + 1) * Math. PI/6;
W = AdMoveConfig. MoveWidth-obj.offsetWidth;
H = AdMoveConfig. MoveHeight-obj.offsetHeight;
X = x + step * kx * dirx;
If (x <0 ){
Dirx = 1;
X = 0;
Kx = Math. sin (rad );
Ky = Math. cos (rad );
}
If (x> W ){
Dirx =-1;
X = W;
Kx = Math. sin (rad );
Ky = Math. cos (rad );
}
Y = y + step * ky * diry;
If (y <0 ){
Diry = 1;
Y = 0;
Kx = Math. sin (rad );
Ky = Math. cos (rad );
}
If (y> H ){
Diry =-1;
Y = H;
Kx = Math. sin (rad );
Ky = Math. cos (rad );
}
}
This. Run = function (){
Var delay = 10;
Interval = setInterval (obj. CustomMethod, delay );
Obj. onmouseover = function (){
ClearInterval (interval );
}
Obj. onmouseout = function (){
Interval = setInterval (obj. CustomMethod, delay );
}
}
}
// End, Thursday, December 11, July 4, 2013

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.