JS部分: 复制代码 代码如下: <br>function addEvent(obj, evtType, func, cap) { <br>cap = cap || false; <br>if (obj.addEventListener) { <br>obj.addEventListener(evtType, func, cap); <br>return true; <br>} else if (obj.attachEvent) { <br>if (cap) { <br>obj.setCapture(); <br>return true; <br>} else { <br>return obj.attachEvent("on" + evtType, func); <br>} <br>} else { <br>return false; <br>} <br>} <br>function getPageScroll() { <br>var xScroll, yScroll; <br>if (self.pageXOffset) { <br>xScroll = self.pageXOffset; <br>} else if (document.documentElement && document.documentElement.scrollLeft) { <br>xScroll = document.documentElement.scrollLeft; <br>} else if (document.body) { <br>xScroll = document.body.scrollLeft; <br>} <br>if (self.pageYOffset) { <br>yScroll = self.pageYOffset; <br>} else if (document.documentElement && document.documentElement.scrollTop) { <br>yScroll = document.documentElement.scrollTop; <br>} else if (document.body) { <br>yScroll = document.body.scrollTop; <br>} <br>arrayPageScroll = new Array(xScroll, yScroll); <br>return arrayPageScroll; <br>} <br>function GetPageSize() { <br>var xScroll, yScroll; <br>if (window.innerHeight && window.scrollMaxY) { <br>xScroll = document.body.scrollWidth; <br>yScroll = window.innerHeight + window.scrollMaxY; <br>} else if (document.body.scrollHeight > document.body.offsetHeight) { <br>xScroll = document.body.scrollWidth; <br>yScroll = document.body.scrollHeight; <br>} else { <br>xScroll = document.body.offsetWidth; <br>yScroll = document.body.offsetHeight; <br>} <br>var windowWidth, windowHeight; <br>if (self.innerHeight) { <br>windowWidth = self.innerWidth; <br>windowHeight = self.innerHeight; <br>} else if (document.documentElement && document.documentElement.clientHeight) { <br>windowWidth = document.documentElement.clientWidth; <br>windowHeight = document.documentElement.clientHeight; <br>} else if (document.body) { <br>windowWidth = document.body.clientWidth; <br>windowHeight = document.body.clientHeight; <br>} <br>if (yScroll < windowHeight) { <BR>pageHeight = windowHeight; <BR>} else { <BR>pageHeight = yScroll; <BR>} <BR>if (xScroll < windowWidth) { <BR>pageWidth = windowWidth; <BR>} else { <BR>pageWidth = xScroll; <BR>} <BR>arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) <BR>return arrayPageSize; <BR>} <BR>var AdMoveConfig = new Object(); <BR>AdMoveConfig.IsInitialized = false; <BR>AdMoveConfig.ScrollX = 0; <BR>AdMoveConfig.ScrollY = 0; <BR>AdMoveConfig.MoveWidth = 0; <BR>AdMoveConfig.MoveHeight = 0; <BR>AdMoveConfig.Resize = function () { <BR>var winsize = GetPageSize(); <BR>AdMoveConfig.MoveWidth = winsize[2]; <BR>AdMoveConfig.MoveHeight = winsize[3]; <BR>AdMoveConfig.Scroll(); <BR>} <BR>AdMoveConfig.Scroll = function () { <BR>var winscroll = getPageScroll(); <BR>AdMoveConfig.ScrollX = winscroll[0]; <BR>AdMoveConfig.ScrollY = winscroll[1]; <BR>} <BR>addEvent(window, "resize", AdMoveConfig.Resize); <BR>addEvent(window, "scroll", AdMoveConfig.Scroll); <BR>function AdMove(id) { <BR>if (!AdMoveConfig.IsInitialized) { <BR>AdMoveConfig.Resize(); <BR>AdMoveConfig.IsInitialized = true; <BR>} <BR>var obj = document.getElementById(id); <BR>obj.style.position = "absolute"; <BR>var W = AdMoveConfig.MoveWidth - obj.offsetWidth; <BR>var H = AdMoveConfig.MoveHeight - obj.offsetHeight; <BR>var x = W * Math.random(), y = H * Math.random(); <BR>var rad = (Math.random() + 1) * Math.PI / 6; <BR>var kx = Math.sin(rad), ky = Math.cos(rad); <BR>var dirx = (Math.random() < 0.5 ? 1 : -1), diry = (Math.random() < 0.5 ? 1 : -1); <BR>var step = 1; <BR>var interval; <BR>this.SetLocation = function (vx, vy) { x = vx; y = vy; } <BR>this.SetDirection = function (vx, vy) { dirx = vx; diry = vy; } <BR>obj.CustomMethod = function () { <BR>obj.style.left = (x + AdMoveConfig.ScrollX) + "px"; <BR>obj.style.top = (y + AdMoveConfig.ScrollY) + "px"; <BR>rad = (Math.random() + 1) * Math.PI / 6; <BR>W = AdMoveConfig.MoveWidth - obj.offsetWidth; <BR>H = AdMoveConfig.MoveHeight - obj.offsetHeight; <BR>x = x + step * kx * dirx; <BR>if (x < 0) { dirx = 1; x = 0; kx = Math.sin(rad); ky = Math.cos(rad); } <BR>if (x > W) { dirx = -1; x = W; kx = Math.sin(rad); ky = Math.cos(rad); } <br>y = y + step * ky * diry; <br>if (y < 0) { diry = 1; y = 0; kx = Math.sin(rad); ky = Math.cos(rad); } <BR>if (y > H) { diry = -1; y = H; kx = Math.sin(rad); ky = Math.cos(rad); } <br>} <br>this.Run = function () { <br>var delay = 10; <br>interval = setInterval(obj.CustomMethod, delay); <br>obj.onmouseover = function () { clearInterval(interval); } <br>obj.onmouseout = function () { interval = setInterval(obj.CustomMethod, delay); } <br>} <br>} <br> </p> <p> var ad2 = new AdMove("ad2");<br> ad2.Run();<br> //多组漂浮<br><br> html部分: 复制代码 代码如下: