JavaScript page floating ads, compatible Ie/ff browser

Source: Internet
Author: User
Tags cos setinterval sin
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" > Advertising 1 </div>
* <script type= "text/javascript" >var ad1=new admove ("GG1") ad1. Run ();</script>
* Finishing: 13:12 July 4, 2013 Thursday
*/
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.documentelement && document.documentElement.scrollLeft) {
Xscroll = Document.documentElement.scrollLeft;
}
else if (document.body) {
Xscroll = Document.body.scrollLeft;
}
if (Self.pageyoffset) {
Yscroll = Self.pageyoffset;
}
else if (document.documentelement && document.documentElement.scrollTop) {
Yscroll = Document.documentElement.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 windowwidth, WindowHeight;
if (self.innerheight) {
WindowWidth = Self.innerwidth;
WindowHeight = Self.innerheight;
}
else if (document.documentelement && document.documentElement.clientHeight) {
WindowWidth = Document.documentElement.clientWidth;
WindowHeight = Document.documentElement.clientHeight;
}
else if (document.body) {
WindowWidth = Document.body.clientWidth;
WindowHeight = Document.body.clientHeight;
}
if (Yscroll < WindowHeight) {
PageHeight = WindowHeight;
}
else{
PageHeight = Yscroll;
}
if (Xscroll < windowwidth) {
PageWidth = WindowWidth;
}
else{
PageWidth = Xscroll;
}
Arraypagesize = new Array (pagewidth,pageheight,windowwidth,windowheight)
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 13:11 July 4, 2013 Thursday
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.