给大家分析一段经典而且兼容各大浏览器的漂浮广告代码,希望对大家有所帮助 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> var ad2 = new AdMove("ad2"); ad2.Run(); //多组漂浮 html部分: 复制代码 代码如下: ,