Popt.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. Array.prototype.unique = function () {//去数组重复
  2. return this.sort().join(",,").replace(/(,|^)([^,]+)(,,\2)+(,|$)/g, "$1$2$4").replace(/,,+/g, ",").replace(/,$/, "").split(",");
  3. }
  4. var Iput = {
  5. confg: {
  6. hand: "0", //0对像位置1鼠标位置divID滚动位置
  7. idIframe: "PoPx", //默认可不用改
  8. idBox: "PoPy", //默认可不用改
  9. content: "", //传过来的内容
  10. ok: null, //弹出框之后执行的函数
  11. id: null, //不能为空一般传this对像而不是对像ID
  12. event: window.event, //这个必写一般为e就可以了
  13. top: 0, //顶部偏移位置
  14. left: 0, //左部偏移位置
  15. bodyHeight: 0, //在被position:absolute元素下得到HTML真实高度
  16. bodyWidth: 0,
  17. width: 0,
  18. soll: null,
  19. pop: null //指定ID点击时不关闭
  20. },
  21. get: function (obj) { return document.getElementById(obj); },
  22. lft: function (e) {
  23. var l = 0;
  24. while (e) { l += e.offsetLeft; e = e.offsetParent; }
  25. return l
  26. },
  27. ltp: function (e) {
  28. var t = 0;
  29. while (e) { t += e.offsetTop; e = e.offsetParent; }
  30. return t
  31. },
  32. clear: function () {
  33. Iput.confg.hand = "0"; Iput.confg.ok = null; Iput.confg.top = 0; Iput.confg.left = 0; Iput.confg.bodyHeight = 0; Iput.confg.bodyWidth = 0; Iput.confg.width = 0; Iput.confg.pop = null;
  34. },
  35. stopBubble: function (e) {
  36. if (e && e.stopPropagation) {
  37. e.stopPropagation(); //w3c
  38. } else {
  39. window.event.cancelBubble = true; //IE
  40. }
  41. },
  42. pop: function () {
  43. var $a = document.getElementsByTagName("body").item(0);
  44. var $c = document.createElement("iframe");
  45. var $b = document.createElement("div");
  46. $c.setAttribute('id', Iput.confg.idIframe);
  47. $c.setAttribute("src", "about:blank");
  48. $c.style.zindex = '100';
  49. $c.frameBorder = "0";
  50. $c.style.width = "0px";
  51. $c.style.height = "0px";
  52. $c.style.position = 'absolute';
  53. $b.setAttribute('id', Iput.confg.idBox);
  54. $b.setAttribute('align', 'left');
  55. $b.style.position = 'absolute';
  56. $b.style.background = 'transparent';
  57. $b.style.zIndex = '20000';
  58. if ($a) {
  59. if (Iput.get(Iput.confg.idIframe)) {
  60. Iput.colse();
  61. }
  62. $a.appendChild($c);
  63. if ($c) {
  64. $c.ownerDocument.body.appendChild($b);
  65. }
  66. Iput.get(Iput.confg.idBox).innerHTML = Iput.confg.content;
  67. Iput.drice(Iput.confg.event);
  68. }
  69. if (!document.all) {
  70. window.document.addEventListener("click", Iput.hide, false);
  71. }
  72. else {
  73. window.document.attachEvent("onclick", Iput.hide);
  74. }
  75. },
  76. drice: function (e) {
  77. var bodyHith = Iput.confg.bodyHeight == 0 ? document.body.scrollHeight : Iput.confg.bodyHeight;
  78. var bodywidth = Iput.confg.bodyWidth == 0 ? document.body.scrollWidth : Iput.confg.bodyWidth;
  79. if (!e) e = window.event;
  80. var top = 0, left = 0;
  81. var a = Iput.get(Iput.confg.idBox);
  82. var b = Iput.get(Iput.confg.idIframe);
  83. var c = Iput.confg.id.offsetHeight;
  84. var d = Iput.confg.id.offsetWidth;
  85. var w = 0;
  86. var st = 0;
  87. var sl = 0;
  88. if (Iput.confg.soll != null) {
  89. st = document.getElementById(Iput.confg.soll).scrollTop;
  90. sl = document.getElementById(Iput.confg.soll).scrollLeft;
  91. }
  92. if (Iput.get(Iput.confg.idIframe)) {
  93. if (Iput.confg.hand == "1") {
  94. top = Iput.confg.top + document.body.scrollTop + document.documentElement.scrollTop + e.clientY;
  95. left = Iput.confg.left + e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  96. if (a.offsetHeight + top > bodyHith) { top = top - a.offsetHeight + Iput.get(Iput.confg.idBox).firstChild.offsetHeight; }
  97. if (a.offsetWidth + left > bodywidth) { left = left - a.offsetWidth + Iput.get(Iput.confg.idBox).firstChild.offsetWidth; }
  98. a.style.top = top - st + "px";
  99. b.style.top = top - st + "px";
  100. a.style.left = left - sl + "px";
  101. b.style.left = left - sl + "px";
  102. }
  103. else if (Iput.confg.hand == "0") {
  104. w = Iput.confg.id.offsetWidth + "px";
  105. a.style.width = w;
  106. b.style.width = w;
  107. height = c;
  108. top = Iput.confg.top + Iput.ltp(Iput.confg.id);
  109. left = Iput.confg.left + Iput.lft(Iput.confg.id);
  110. if (a.firstChild.offsetHeight + top + c > bodyHith) { top = top - a.firstChild.offsetHeight - c; }
  111. if (a.firstChild.offsetWidth + left > bodywidth) { left = left - a.firstChild.offsetWidth + d; }
  112. b.style.top = top - st + "px";
  113. a.style.top = top - st + height + "px";
  114. b.style.left = left - sl + "px";
  115. a.style.left = left - sl + "px";
  116. }
  117. else {
  118. height = c;
  119. top = Iput.confg.top - Iput.get(Iput.confg.hand).scrollTop + Iput.ltp(Iput.confg.id);
  120. left = Iput.confg.left - Iput.get(Iput.confg.hand).scrollLeft + Iput.lft(Iput.confg.id);
  121. if (a.offsetHeight + top > bodyHith) { top = top - a.offsetHeight - c; }
  122. if (a.offsetWidth + left > bodywidth) { left = left - a.offsetWidth - d; }
  123. b.style.top = top - st + height + "px";
  124. a.style.top = top - st + height + "px";
  125. b.style.left = left - sl + "px";
  126. a.style.left = left - sl + "px";
  127. }
  128. }
  129. },
  130. show: function () {
  131. var config = arguments[0]; var that = Iput.confg;
  132. Iput.clear();
  133. for (var i in that) { if (config[i] != undefined) { that[i] = config[i]; } };
  134. Iput.pop();
  135. if (Iput.confg.ok != null) {
  136. Iput.action(Iput.confg.ok());
  137. }
  138. },
  139. colse: function () {
  140. if (Iput.get(Iput.confg.idIframe)) {
  141. document.body.removeChild(Iput.get(Iput.confg.idBox));
  142. document.body.removeChild(Iput.get(Iput.confg.idIframe));
  143. }
  144. if (Iput.get(Iput.confg.pop)) {
  145. Iput.get(Iput.confg.pop).style.display = "none";
  146. }
  147. },
  148. $colse: function () { Iput.colse(); },
  149. hide: function (e) {//点击任何处关闭层
  150. e = window.event || e;
  151. var srcElement = e.srcElement || e.target;
  152. if (Iput.confg.event == undefined) {//输入时用,般在没传入Iput.confg.event请况下使用
  153. Iput.colse();
  154. }
  155. else {
  156. var a = Iput.confg.event.srcElement || Iput.confg.event.target;
  157. var b = Iput.get(Iput.confg.pop);
  158. if (a != srcElement) { Iput.colse(); }
  159. if (b != null) {
  160. if (b != srcElement && a != srcElement) { Iput.colse(); }
  161. }
  162. }
  163. if (Iput.get(Iput.confg.idIframe)) {
  164. Iput.get(Iput.confg.idIframe).onclick = function (e) { Iput.stopBubble(e); };
  165. Iput.get(Iput.confg.idBox).onclick = function (e) { Iput.stopBubble(e); };
  166. }
  167. if (Iput.get(Iput.confg.pop)) {
  168. Iput.get(Iput.confg.pop).onclick = function (e) { Iput.stopBubble(e); };
  169. }
  170. },
  171. action: function (obj) {
  172. eval(obj);
  173. },
  174. cookie: {
  175. Set: function (name, val) {
  176. var Days = 30; //此 cookie 将被保存 30 天
  177. var exp = new Date(); //new Date("December 31, 9998");
  178. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  179. document.cookie = name + "=" + escape(val) + ";expires=" + exp.toGMTString() + "; path=/";
  180. },
  181. Get: function (name) {
  182. var start = document.cookie.indexOf(name);
  183. var end = document.cookie.indexOf(";", start);
  184. return start == -1 ? null : unescape(document.cookie.substring(start + name.length + 1, (end > start ? end : document.cookie.length)));
  185. },
  186. Del: function (name) {
  187. var exp = new Date();
  188. exp.setTime(exp.getTime() - 1);
  189. var cval = this.GetCookie(name);
  190. if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  191. }
  192. },
  193. ischeck: function (bol) {
  194. var objs = form1.getElementsByTagName("input");
  195. if (bol) {
  196. for (var i = 0; i < objs.length; i++) { if (objs[i].type.toLowerCase() == "checkbox") { objs[i].checked = true; } }
  197. }
  198. else {
  199. for (var i = 0; i < objs.length; i++) { if (objs[i].type.toLowerCase() == "checkbox") { objs[i].checked = false; } }
  200. }
  201. },
  202. contains: function (star, end, isIgnoreCase) {
  203. if (isIgnoreCase) {
  204. star = star.toLowerCase();
  205. end = end.toLowerCase();
  206. }
  207. var startChar = end.substring(0, 1);
  208. var strLen = end.length;
  209. for (var j = 0; j < star.length - strLen + 1; j++) {
  210. if (star.charAt(j) == startChar)//如果匹配起始字符,开始查找
  211. {
  212. if (star.substring(j, j + strLen) == end)//如果从j开始的字符与str匹配,那ok
  213. {
  214. return true;
  215. }
  216. }
  217. }
  218. return false;
  219. },
  220. gData: function (name, value) {
  221. var top = window.top, cache = top['_CACHE'] || {};
  222. top['_CACHE'] = cache;
  223. return value ? cache[name] = value : cache[name];
  224. },
  225. rData: function (name) {
  226. var cache = window.top['_CACHE'];
  227. if (cache && cache[name]) delete cache[name];
  228. }
  229. }