废话不多少,直接上代码:
JS部分:
001 | function addEvent(obj, evtType, func, cap) { |
002 | cap = cap || false ; |
003 | if (obj.addEventListener) { |
004 | obj.addEventListener(evtType, func, cap); |
005 | return true ; |
006 | } else if (obj.attachEvent) { |
007 | if (cap) { |
008 | obj.setCapture(); |
009 | return true ; |
010 | } else { |
011 | return obj.attachEvent( "on" + evtType, func); // @camnpr |
012 | } |
013 | } else { |
014 | return false ; |
015 | } |
016 | } |
017 | function getPageScroll() { |
018 | var xScroll, yScroll; |
019 | if (self.pageXOffset) { |
020 | xScroll = self.pageXOffset; |
021 | } else if (document.documentElement && document.documentElement.scrollLeft) { |
022 | xScroll = document.documentElement.scrollLeft; |
023 | } else if (document.body) { |
024 | xScroll = document.body.scrollLeft; |
025 | } |
026 | if (self.pageYOffset) { |
027 | yScroll = self.pageYOffset; |
028 | } else if (document.documentElement && document.documentElement.scrollTop) { |
029 | yScroll = document.documentElement.scrollTop; |
030 | } else if (document.body) { |
031 | yScroll = document.body.scrollTop; |
032 | } |
033 | arrayPageScroll = new Array(xScroll, yScroll); |
034 | return arrayPageScroll; |
035 | } |
036 | function GetPageSize() { |
037 | var xScroll, yScroll; |
038 | if (window.innerHeight && window.scrollMaxY) { |
039 | xScroll = document.body.scrollWidth; |
040 | yScroll = window.innerHeight + window.scrollMaxY; |
041 | } else if (document.body.scrollHeight > document.body.offsetHeight) { |
042 | xScroll = document.body.scrollWidth; |
043 | yScroll = document.body.scrollHeight; |
044 | } else { |
045 | xScroll = document.body.offsetWidth; |
046 | yScroll = document.body.offsetHeight; //@郑州网建 |
047 | } |
048 | var windowWidth, windowHeight; |
049 | if (self.innerHeight) { |
050 | windowWidth = self.innerWidth; |
051 | windowHeight = self.innerHeight; |
052 | } else if (document.documentElement && document.documentElement.clientHeight) { |
053 | windowWidth = document.documentElement.clientWidth; |
054 | windowHeight = document.documentElement.clientHeight; |
055 | } else if (document.body) { |
056 | windowWidth = document.body.clientWidth; |
057 | windowHeight = document.body.clientHeight; |
058 | } |
059 | if (yScroll < windowHeight) { |
060 | pageHeight = windowHeight; |
061 | } else { |
062 | pageHeight = yScroll; |
063 | } |
064 | if (xScroll < windowWidth) { |
065 | pageWidth = windowWidth; |
066 | } else { |
067 | pageWidth = xScroll; |
068 | } |
069 | arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) |
070 | return arrayPageSize; |
071 | } |
072 | var AdMoveConfig = new Object(); |
073 | AdMoveConfig.IsInitialized = false ; |
074 | AdMoveConfig.ScrollX = 0; |
075 | AdMoveConfig.ScrollY = 0; |
076 | AdMoveConfig.MoveWidth = 0; |
077 | AdMoveConfig.MoveHeight = 0; |
078 | AdMoveConfig.Resize = function () { |
079 | var winsize = GetPageSize(); |
080 | AdMoveConfig.MoveWidth = winsize[2]; |
081 | AdMoveConfig.MoveHeight = winsize[3]; |
082 | AdMoveConfig.Scroll(); |
083 | } |
084 | AdMoveConfig.Scroll = function () { |
085 | var winscroll = getPageScroll(); |
086 | AdMoveConfig.ScrollX = winscroll[0]; |
087 | AdMoveConfig.ScrollY = winscroll[1]; |
088 | } |
089 | addEvent(window, "resize" , AdMoveConfig.Resize); |
090 | addEvent(window, "scroll" , AdMoveConfig.Scroll); |
091 | function AdMove(id) { |
092 | if (!AdMoveConfig.IsInitialized) { |
093 | AdMoveConfig.Resize(); |
094 | AdMoveConfig.IsInitialized = true ; |
095 | } |
096 | var obj = document.getElementById(id); |
097 | obj.style.position = "absolute" ; |
098 | var W = AdMoveConfig.MoveWidth - obj.offsetWidth; |
099 | var H = AdMoveConfig.MoveHeight - obj.offsetHeight; |
100 | var x = W * Math.random(), y = H * Math.random(); |
101 | var rad = (Math.random() + 1) * Math.PI / 6; |
102 | var kx = Math.sin(rad), ky = Math.cos(rad); |
103 | var dirx = (Math.random() < 0.5 ? 1 : -1), diry = (Math.random() < 0.5 ? 1 : -1); |
104 | var step = 1; |
105 | var interval; |
106 | this .SetLocation = function (vx, vy) { x = vx; y = vy; } |
107 | this .SetDirection = function (vx, vy) { dirx = vx; diry = vy; } |
108 | obj.CustomMethod = function () { |
109 | obj.style.left = (x + AdMoveConfig.ScrollX) + "px" ; |
110 | obj.style.top = (y + AdMoveConfig.ScrollY) + "px" ; |
111 | rad = (Math.random() + 1) * Math.PI / 6; |
112 | W = AdMoveConfig.MoveWidth - obj.offsetWidth; |
113 | H = AdMoveConfig.MoveHeight - obj.offsetHeight; |
114 | x = x + step * kx * dirx; |
115 | if (x < 0) { dirx = 1; x = 0; kx = Math.sin(rad); ky = Math.cos(rad); } |
116 | if (x > W) { dirx = -1; x = W; kx = Math.sin(rad); ky = Math.cos(rad); } |
117 | y = y + step * ky * diry; |
118 | if (y < 0) { diry = 1; y = 0; kx = Math.sin(rad); ky = Math.cos(rad); } |
119 | if (y > H) { diry = -1; y = H; kx = Math.sin(rad); ky = Math.cos(rad); } |
120 | } |
121 | this .Run = function () { |
122 | var delay = 10; |
123 | interval = setInterval(obj.CustomMethod, delay); |
124 | obj.onmouseover = function () { clearInterval(interval); } |
125 | obj.onmouseout = function () { interval = setInterval(obj.CustomMethod, delay); } |
126 | } |
127 | } |
128 |
129 | var ad2 = new AdMove( "ad2" ); |
130 | ad2.Run(); |
131 | //多组漂浮 |
html部分:
1 | < DIV id = ad2 style = "Z-INDEX: 5;position:relative" > |
2 | < a href = 'PoliceHTk.aspx' target = '_blank' >< IMG src = "images/fudong/fudong.jpg" width = "230" height = "150" border = "0" > </ a > |
3 | |
4 | < a href = "#" onclick = "document.getElementById('ad2').style.display='none'" >< img border = 0 src = images /fudong/close1.gif /></ a > |
5 | </ DIV > |