Mask.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. Mask = function (w, element) {
  2. this.window = w;
  3. this.element = document.getElementById(element);
  4. this.element.style.display = "none";
  5. this.showing = false;
  6. this.show = jQuery.proxy(this.show, this);
  7. this.move = jQuery.proxy(this.move, this);
  8. this.walk = jQuery.proxy(this.walk, this);
  9. this.resize = jQuery.proxy(this.resize, this);
  10. this.close = jQuery.proxy(this.close, this);
  11. this.active = jQuery.proxy(this.active, this);
  12. }
  13. Mask.prototype = {
  14. cover: null,
  15. ensureCover: function () {
  16. this.cover = document.getElementById("__ess_PopupBackCover");
  17. if (!this.cover) {
  18. var cover = document.createElement("DIV");
  19. cover.id = "__ess_PopupBackCover";
  20. cover.style.position = "absolute";
  21. cover.style.width = "0px";
  22. cover.style.height = "0px";
  23. cover.style.display = "none";
  24. cover.style.backgroundColor = "#666666";
  25. cover.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=50)";
  26. cover.style.top = "0px";
  27. cover.style.left = "0px";
  28. cover.style.opacity = "0.5";
  29. cover.style.mozopacity = "0.5";
  30. document.body.appendChild(cover);
  31. this.cover = document.getElementById("__ess_PopupBackCover");
  32. if (document.attachEvent) {
  33. var ifr = document.createElement("IFRAME");
  34. ifr.frameBorder = "0";
  35. ifr.style.width = "100%";
  36. ifr.style.height = "100%";
  37. this.cover.appendChild(ifr);
  38. var c = document.createElement("DIV");
  39. c.style.position = "absolute";
  40. c.style.width = "100%";
  41. c.style.height = "100%";
  42. c.style.backgroundColor = "#666666";
  43. c.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=50)";
  44. c.style.top = "0px";
  45. c.style.left = "0px";
  46. c.style.opacity = "0.5";
  47. c.style.mozopacity = "0.5";
  48. this.cover.appendChild(c);
  49. }
  50. }
  51. },
  52. show: function () {
  53. this.ensureCover();
  54. this.cover.style.position = "absolute";
  55. this.cover.style.top = "0px";
  56. this.cover.style.left = "0px";
  57. this.cover.style.zIndex = "999999";
  58. this.element.style.position = "absolute";
  59. this.element.style.zIndex = "1000000";
  60. this.cover.style.display = "block";
  61. this.element.style.display = "block";
  62. this.resize();
  63. if (window.attachEvent) {
  64. window.attachEvent("onresize", this.resize);
  65. window.attachEvent("onscroll", this.move);
  66. }
  67. else {
  68. window.addEventListener("resize", this.resize, false);
  69. window.addEventListener("scroll", this.move, false);
  70. }
  71. this.showing = true;
  72. },
  73. close: function (elementOnly) {
  74. if (window.attachEvent) {
  75. window.detachEvent("onresize", this.resize);
  76. window.detachEvent("onscroll", this.move);
  77. }
  78. else {
  79. window.removeEventListener("resize", this.resize, false);
  80. window.removeEventListener("scroll", this.move, false);
  81. }
  82. this.element.style.display = "none";
  83. this.showing = false;
  84. if (elementOnly && elementOnly === true) {
  85. return;
  86. }
  87. this.cover.style.display = "none";
  88. },
  89. resize: function () {
  90. var w, h;
  91. if (window.innerWidth) {
  92. w = window.innerWidth;
  93. h = window.innerHeight;
  94. }
  95. else if (document.documentElement.clientWidth) {
  96. w = document.documentElement.clientWidth;
  97. h = document.documentElement.clientHeight;
  98. }
  99. else {
  100. w = document.body.clientWidth;
  101. h = document.body.clientHeight;
  102. }
  103. this.cover.style.width = w + "px";
  104. this.cover.style.height = h + "px";
  105. this.position();
  106. this.element.style.top = this.top + "px";
  107. this.element.style.left = this.left + "px";
  108. },
  109. move: function () {
  110. if (this.position()) {
  111. if (this.walkHandle) {
  112. clearTimeout(this.walkHandle);
  113. }
  114. this.walkHandle = setTimeout(this.walk, 300);
  115. }
  116. },
  117. position: function () {
  118. var st = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
  119. var sl = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
  120. var w, h;
  121. if (window.innerWidth) {
  122. w = window.innerWidth;
  123. h = window.innerHeight;
  124. }
  125. else if (document.documentElement.clientWidth) {
  126. w = document.documentElement.clientWidth;
  127. h = document.documentElement.clientHeight;
  128. }
  129. else {
  130. w = document.body.clientWidth;
  131. h = document.body.clientHeight;
  132. }
  133. this.top = (h - this.element.clientHeight) / 2 + st;
  134. this.left = (w - this.element.clientWidth) / 2 + sl;
  135. if (this.top < 0) this.top = 0;
  136. if (this.left < 0) this.left = 0;
  137. return h >= this.element.clientHeight && w >= this.element.clientWidth;
  138. },
  139. top: 0,
  140. left: 0,
  141. step: 10,
  142. rate: 10,
  143. walkHandle: null,
  144. walk: function () {
  145. var g = false;
  146. var d = 0;
  147. var t = parseInt(this.element.style.top);
  148. var l = parseInt(this.element.style.left);
  149. d = t > this.top ? -1 : 1;
  150. if (d != 0 && Math.abs(t - this.top) < this.step) {
  151. this.element.style.top = this.top + "px";
  152. }
  153. else {
  154. t += d * this.step;
  155. this.element.style.top = t + "px";
  156. g = true;
  157. }
  158. d = 0;
  159. d = l > this.left ? -1 : 1;
  160. if (d != 0 && Math.abs(l - this.left) < this.step) {
  161. this.element.style.left = this.left + "px";
  162. }
  163. else {
  164. l += d * this.step;
  165. this.element.style.left = l + "px";
  166. g = true;
  167. }
  168. if (this.walkHandle) {
  169. clearTimeout(this.walkHandle);
  170. }
  171. if (g) {
  172. this.walkHandle = setTimeout(this.walk, this.rate);
  173. }
  174. },
  175. active: function () {
  176. this.close();
  177. jQuery(this.window).unload(this.show);
  178. }
  179. }