AdMove.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!--//公共脚本文件 main.js
  2. function addEvent(obj, evtType, func, cap) {
  3. cap = cap || false;
  4. if (obj.addEventListener) {
  5. obj.addEventListener(evtType, func, cap);
  6. return true;
  7. } else if (obj.attachEvent) {
  8. if (cap) {
  9. obj.setCapture();
  10. return true;
  11. } else {
  12. return obj.attachEvent("on" + evtType, func);
  13. }
  14. } else {
  15. return false;
  16. }
  17. }
  18. function removeEvent(obj, evtType, func, cap) {
  19. cap = cap || false;
  20. if (obj.removeEventListener) {
  21. obj.removeEventListener(evtType, func, cap);
  22. return true;
  23. } else if (obj.detachEvent) {
  24. if (cap) {
  25. obj.releaseCapture();
  26. return true;
  27. } else {
  28. return obj.detachEvent("on" + evtType, func);
  29. }
  30. } else {
  31. return false;
  32. }
  33. }
  34. function getPageScroll() {
  35. var xScroll, yScroll;
  36. if (self.pageXOffset) {
  37. xScroll = self.pageXOffset;
  38. } else if (document.documentElement && document.documentElement.scrollLeft) {
  39. xScroll = document.documentElement.scrollLeft;
  40. } else if (document.body) {
  41. xScroll = document.body.scrollLeft;
  42. }
  43. if (self.pageYOffset) {
  44. yScroll = self.pageYOffset;
  45. } else if (document.documentElement && document.documentElement.scrollTop) {
  46. yScroll = document.documentElement.scrollTop;
  47. } else if (document.body) {
  48. yScroll = document.body.scrollTop;
  49. }
  50. arrayPageScroll = new Array(xScroll, yScroll);
  51. return arrayPageScroll;
  52. }
  53. // 获取页面的高度、宽度
  54. function GetPageSize() {
  55. var xScroll, yScroll;
  56. if (window.innerHeight && window.scrollMaxY) {
  57. xScroll = window.innerWidth + window.scrollMaxX;
  58. yScroll = window.innerHeight + window.scrollMaxY;
  59. } else {
  60. if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
  61. xScroll = document.body.scrollWidth;
  62. yScroll = document.body.scrollHeight;
  63. } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  64. xScroll = document.body.offsetWidth;
  65. yScroll = document.body.offsetHeight;
  66. }
  67. }
  68. var windowWidth, windowHeight;
  69. if (self.innerHeight) { // all except Explorer
  70. if (document.documentElement.clientWidth) {
  71. windowWidth = document.documentElement.clientWidth;
  72. } else {
  73. windowWidth = self.innerWidth;
  74. }
  75. windowHeight = self.innerHeight;
  76. } else {
  77. if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  78. windowWidth = document.documentElement.clientWidth;
  79. windowHeight = document.documentElement.clientHeight;
  80. } else {
  81. if (document.body) { // other Explorers
  82. windowWidth = document.body.clientWidth;
  83. windowHeight = document.body.clientHeight;
  84. }
  85. }
  86. }
  87. // for small pages with total height less then height of the viewport
  88. if (yScroll < windowHeight) {
  89. pageHeight = windowHeight;
  90. } else {
  91. pageHeight = yScroll;
  92. }
  93. // for small pages with total width less then width of the viewport
  94. if (xScroll < windowWidth) {
  95. pageWidth = xScroll;
  96. } else {
  97. pageWidth = windowWidth;
  98. }
  99. arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
  100. return arrayPageSize;
  101. }
  102. //广告脚本文件 AdMove.js
  103. /*
  104. 例子 加<div></div> 是为了判断飘窗内容为空时隐藏飘窗
  105. <div id="Div2">
  106. <div></div>
  107. ***** content ******
  108. </div>
  109. </div>
  110. var ad=new AdMove("Div2");
  111. ad.Run();
  112. */
  113. ////////////////////////////////////////////////////////
  114. var AdMoveConfig = new Object();
  115. AdMoveConfig.IsInitialized = false;
  116. AdMoveConfig.AdCount = 0;
  117. AdMoveConfig.ScrollX = 0;
  118. AdMoveConfig.ScrollY = 0;
  119. AdMoveConfig.MoveWidth = 0;
  120. AdMoveConfig.MoveHeight = 0;
  121. AdMoveConfig.Resize = function () {
  122. var winsize = GetPageSize();
  123. AdMoveConfig.MoveWidth = winsize[2];
  124. AdMoveConfig.MoveHeight = winsize[3];
  125. AdMoveConfig.Scroll();
  126. }
  127. AdMoveConfig.Scroll = function () {
  128. var winscroll = getPageScroll();
  129. AdMoveConfig.ScrollX = winscroll[0];
  130. AdMoveConfig.ScrollY = winscroll[1];
  131. }
  132. addEvent(window, "resize", AdMoveConfig.Resize);
  133. addEvent(window, "scroll", AdMoveConfig.Scroll);
  134. function AdMove(id, addCloseButton) {
  135. if (!AdMoveConfig.IsInitialized) {
  136. AdMoveConfig.Resize();
  137. AdMoveConfig.IsInitialized = true;
  138. }
  139. AdMoveConfig.AdCount++;
  140. var obj = document.getElementById(id);
  141. if (!obj) {
  142. this.SetLocation = function () {};
  143. this.SetDirection = function () {};
  144. this.Run = function () {};
  145. return;
  146. }
  147. obj.style.position = "absolute";
  148. obj.style.zIndex = "999";
  149. var W = AdMoveConfig.MoveWidth - obj.offsetWidth;
  150. var H = AdMoveConfig.MoveHeight - obj.offsetHeight;
  151. var x = W * Math.random(), y = H * Math.random();
  152. var rad = (Math.random() + 1) * Math.PI / 6;
  153. var kx = Math.sin(rad), ky = Math.cos(rad);
  154. var dirx = (Math.random() < 0.5 ? 1 : -1), diry = (Math.random() < 0.5 ? 1 : -1);
  155. var step = 1;
  156. var interval;
  157. if (addCloseButton) {
  158. var closebtn = document.createElement("span");
  159. closebtn.className='close_btn';
  160. closebtn.innerHTML="关闭窗口";
  161. obj.appendChild(closebtn);
  162. closebtn.onclick = function () {
  163. obj.style.display = "none";
  164. clearInterval(interval);
  165. closebtn.onclick = null;
  166. obj.onmouseover = null;
  167. obj.onmouseout = null;
  168. obj.MoveHandler = null;
  169. AdMoveConfig.AdCount--;
  170. if (AdMoveConfig.AdCount <= 0) {
  171. removeEvent(window, "resize", AdMoveConfig.Resize);
  172. removeEvent(window, "scroll", AdMoveConfig.Scroll);
  173. AdMoveConfig.Resize = null;
  174. AdMoveConfig.Scroll = null;
  175. AdMoveConfig = null;
  176. }
  177. }
  178. /*判断飘窗内容是否为空,为空就隐藏*/
  179. function removeHTMLTag(str) {
  180. //str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
  181. str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
  182. str = str.replace(/[\r\n]/g,""); //去除多余空行
  183. str=str.replace(/ /ig,'');//去掉&nbsp;
  184. return str;
  185. }
  186. var oDiv=obj.getElementsByTagName('div')[0];
  187. if(oDiv && removeHTMLTag(oDiv.innerHTML) ===""){
  188. obj.style.display = "none";
  189. }
  190. }
  191. obj.MoveHandler = function () {
  192. obj.style.left = (x + AdMoveConfig.ScrollX) + "px";
  193. obj.style.top = (y + AdMoveConfig.ScrollY) + "px";
  194. rad = (Math.random() + 1) * Math.PI / 6;
  195. W = AdMoveConfig.MoveWidth - obj.offsetWidth;
  196. H = AdMoveConfig.MoveHeight - obj.offsetHeight;
  197. x = x + step * kx * dirx;
  198. if (x < 0) { dirx = 1; x = 0; kx = Math.sin(rad); ky = Math.cos(rad); }
  199. if (x > W) { dirx = -1; x = W; kx = Math.sin(rad); ky = Math.cos(rad); }
  200. y = y + step * ky * diry;
  201. if (y < 0) { diry = 1; y = 0; kx = Math.sin(rad); ky = Math.cos(rad); }
  202. if (y > H) { diry = -1; y = H; kx = Math.sin(rad); ky = Math.cos(rad); }
  203. }
  204. this.SetLocation = function (vx, vy) { x = vx; y = vy; }
  205. this.SetDirection = function (vx, vy) { dirx = vx; diry = vy; }
  206. this.Run = function () {
  207. var delay = 10;
  208. interval = setInterval(obj.MoveHandler, delay);
  209. obj.onmouseover = function () { clearInterval(interval); }
  210. obj.onmouseout = function () { interval = setInterval(obj.MoveHandler, delay); }
  211. }
  212. }
  213. //-->