weefloat.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * weefloat.js
  3. * @category javascript
  4. * @package jquery
  5. * @author ePim <WanJiDong@gmail.com>
  6. * @version
  7. */
  8. (function($) {
  9. var weefloat = function(content,options) {
  10. var self = this;
  11. this.dh = null;
  12. this.closeable = true;
  13. this.delay = 3;
  14. this.direction = 'full';
  15. this.options = null;
  16. this._content = content || '';
  17. this._options = options || {};
  18. this._defaults = {
  19. boxid: null,
  20. width: 0,
  21. height: 0,
  22. timeout: 0,
  23. focus: null,
  24. blur: null,
  25. contentType: 'text',
  26. zIndex: 998,
  27. onclose: null
  28. };
  29. //初始化选项
  30. this.initOptions = function() {
  31. self._options = self._options || {};
  32. self._options.contentType = self._options.contentType || "";
  33. if (self._options.contentType == "") {
  34. self._options.contentType = (self._content.substr(0,1) == '#') ? 'selector' : 'text';
  35. }
  36. self.options = $.extend({}, self._defaults, self._options);
  37. self._options = null;
  38. self._defaults = null;
  39. };
  40. //初始化弹窗Box
  41. this.initBox = function() {
  42. html = '<div class="weefloat">' +
  43. ' <div class="dialog-content"></div>' +
  44. ' <div class="dialog-close"></div>' +
  45. '</div>';
  46. self.dh = $(html).appendTo('body').hide().css({
  47. position: 'absolute',
  48. overflow: 'hidden',
  49. zIndex: self.options.zIndex
  50. });
  51. if (self.options.boxid) {
  52. self.dh.attr('id', self.options.boxid);
  53. }
  54. if (self.options.height>0) {
  55. self.dh.css('height', self.options.height);
  56. }
  57. if (self.options.width>0) {
  58. self.dh.css('width', self.options.width);
  59. }
  60. self.dh.bgiframe();
  61. }
  62. //初始化弹窗内容
  63. this.initContent = function(content) {
  64. if (self.options.contentType == "selector") {
  65. self.selector = self._content;
  66. self._content = $(self.selector).html();
  67. self.setContent(self._content);
  68. $(self.selector).empty();
  69. self.show();
  70. self.focus();
  71. } else if (self.options.contentType == "ajax") {
  72. self.ajaxurl = self._content;
  73. self.setLoading();
  74. self.show();
  75. if (self.options.cache == false) {
  76. if (self.ajaxurl.indexOf('?') == -1) {
  77. self.ajaxurl += "?_t="+Math.random();
  78. } else {
  79. self.ajaxurl += "&_t="+Math.random();
  80. }
  81. }
  82. $.get(self.ajaxurl, function(data) {
  83. self._content = data;
  84. self.setContent(self._content);
  85. self.show();
  86. self.focus();
  87. });
  88. } else if (self.options.contentType == "iframe") { /*加入iframe使程序可以直接引用其它页面 by ePim*/
  89. self.setContent('<iframe src="'+self._content+'" width="100%" height="100%" frameborder="no"></iframe>');
  90. self.show();
  91. self.focus();
  92. } else {
  93. self.setContent(self._content);
  94. self.show();
  95. self.focus();
  96. }
  97. }
  98. //初始化弹窗事件
  99. this.initEvent = function() {
  100. self.dh.find(".dialog-close").unbind('click').click(function(){self.close()});
  101. if (self.options.timeout>0) {
  102. window.setTimeout(self.close, (self.options.timeout * 1000));
  103. }
  104. }
  105. //设置onOnclose事件
  106. this.setOnclose = function(fn) {
  107. self.options.onclose = fn;
  108. }
  109. //显示弹窗
  110. this.show = function() {
  111. if (self.options.showButton) {
  112. self.dh.find('.dialog-button').show();
  113. }
  114. if (self.options.position == 'center') {
  115. self.setCenterPosition();
  116. } else {
  117. self.setElementPosition();
  118. }
  119. if (typeof self.options.showAnimate == "string") {
  120. self.dh.show(self.options.animate);
  121. } else {
  122. self.dh.animate(self.options.showAnimate.animate, self.options.showAnimate.speed);
  123. }
  124. if (self.mh) {
  125. self.mh.show();
  126. }
  127. }
  128. this.hide = function(fn) {
  129. if (typeof self.options.hideAnimate == "string") {
  130. self.dh.hide(self.options.animate, fn);
  131. } else {
  132. self.dh.animate(self.options.hideAnimate.animate, self.options.hideAnimate.speed, "", fn);
  133. }
  134. }
  135. //在弹窗内查找元素
  136. this.find = function(selector) {
  137. return self.dh.find(selector);
  138. }
  139. //设置加载加状态
  140. this.setLoading = function() {
  141. self.setContent('<div class="dialog-loading"></div>');
  142. self.dh.find(".dialog-button").hide();
  143. if (self.dc.height()<90) {
  144. self.dc.height(Math.max(90, self.options.height));
  145. }
  146. if (self.dh.width()<200) {
  147. self.dh.width(Math.max(200, self.options.width));
  148. }
  149. }
  150. this.setWidth = function(width) {
  151. self.dh.width(width);
  152. }
  153. //设置内容
  154. this.setContent = function(content) {
  155. self.dc.html(content);
  156. if (self.options.height>0) {
  157. self.dc.css('height', self.options.height);
  158. } else {
  159. self.dc.css('height','');
  160. }
  161. if (self.options.width>0) {
  162. self.dh.css('width', self.options.width);
  163. } else {
  164. self.dh.css('width','');
  165. }
  166. if (self.options.showButton) {
  167. self.dh.find(".dialog-button").show();
  168. }
  169. }
  170. //取得内容
  171. this.getContent = function() {
  172. return self.dh.html();
  173. }
  174. //关闭弹窗
  175. this.close = function(n) {
  176. if (typeof(self.options.onclose) == "function") {
  177. self.options.onclose(self);
  178. }
  179. if (self.options.contentType == 'selector') {
  180. if (self.options.contentChange) {
  181. //if have checkbox do
  182. var cs = self.find(':checkbox');
  183. $(self.selector).html(self.getContent());
  184. if (cs.length > 0) {
  185. $(self.selector).find(':checkbox').each(function(i){
  186. this.checked = cs[i].checked;
  187. });
  188. }
  189. } else {
  190. $(self.selector).html(self._content);
  191. }
  192. }
  193. //设置关闭后的焦点
  194. if (self.options.blur) {
  195. $(self.options.blur).focus();
  196. }
  197. //从数组中删除
  198. for(i=0;i<arrweebox.length;i++) {
  199. if (arrweebox[i].dh.get(0) == self.dh.get(0)) {
  200. arrweebox.splice(i, 1);
  201. break;
  202. }
  203. }
  204. self.hide();
  205. self.dh.remove();
  206. if (self.mh) {
  207. self.mh.remove();
  208. }
  209. }
  210. //将弹窗显示在中间位置
  211. this.setCenterPosition = function() {
  212. var wnd = $(window), doc = $(document),
  213. pTop = doc.scrollTop(), pLeft = doc.scrollLeft();
  214. pTop += (wnd.height() - self.dh.height()) / 2;
  215. pLeft += (wnd.width() - self.dh.width()) / 2;
  216. self.dh.css({top: pTop, left: pLeft});
  217. }
  218. //根据元素设置弹窗显示位置
  219. this.setElementPosition = function() {
  220. var trigger = $(self.options.position.refele);
  221. var reftop = self.options.position.reftop || 0;
  222. var refleft = self.options.position.refleft || 0;
  223. var adjust = (typeof self.options.position.adjust=="undefined")?true:self.options.position.adjust;
  224. var top = trigger.offset().top + trigger.height();
  225. var left = trigger.offset().left;
  226. var docWidth = document.documentElement.clientWidth || document.body.clientWidth;
  227. var docHeight = document.documentElement.clientHeight|| document.body.clientHeight;
  228. var docTop = document.documentElement.scrollTop|| document.body.scrollTop;
  229. var docLeft = document.documentElement.scrollLeft|| document.body.scrollLeft;
  230. var docBottom = docTop + docHeight;
  231. var docRight = docLeft + docWidth;
  232. if (adjust && left + self.dh.width() > docRight) {
  233. left = docRight - self.dh.width() - 1;
  234. }
  235. if (adjust && top + self.dh.height() > docBottom) {
  236. top = docBottom - self.dh.height() - 1;
  237. }
  238. left = Math.max(left+refleft, 0);
  239. top = Math.max(top+reftop, 0);
  240. self.dh.css({top: top, left: left});
  241. }
  242. this.initOptions();
  243. this.initMask();
  244. this.initBox();
  245. this.initContent();
  246. this.initEvent();
  247. }
  248. var weefloats = function() {
  249. var self = this;
  250. this._onbox = false;
  251. this._opening = false;
  252. this.zIndex = 999;
  253. this.length = function() {
  254. return arrweebox.length;
  255. }
  256. this.open = function(content, options) {
  257. self._opening = true;
  258. if (typeof(options) == "undefined") {
  259. options = {};
  260. }
  261. if (options.boxid) {
  262. for(var i=0; i<arrweebox.length; i++) {
  263. if (arrweebox[i].dh.attr('id') == options.boxid) {
  264. arrweebox[i].close();
  265. break;
  266. }
  267. }
  268. }
  269. options.zIndex = self.zIndex;
  270. self.zIndex += 10;
  271. var box = new weebox(content, options);
  272. box.dh.click(function(){self._onbox = true;});
  273. arrweebox.push(box);
  274. /*-----解决在ie下页面过大时出现部分阴影没有覆盖的问题-----by ePim*/
  275. if (box.options.position != "center"){
  276. box.setElementPosition();
  277. }
  278. if (box.mh) {
  279. box.mh.css({
  280. width: box.bwidth(),
  281. height: box.bheight()
  282. });
  283. }
  284. /*-----解决在ie下页面过大时出现部分没有遮罩的问题-----by ePim(WanJiDong@gmail.com)*/
  285. return box;
  286. }
  287. //关闭最上层窗体,程序调用方法:jQuery.weeboxs.close();
  288. this.close = function(){
  289. var closingBox = this.getTopBox();
  290. if(false!=closingBox) {
  291. closingBox.close();
  292. }
  293. }
  294. $(window).scroll(function() {
  295. if (arrweebox.length > 0) {
  296. for(i=0;i<arrweebox.length;i++) {
  297. var box = arrweebox[i];//self.getTopBox();
  298. /*if (box.options.position == "center") {
  299. box.setCenterPosition();
  300. }*/
  301. if (box.options.position != "center"){
  302. box.setElementPosition();
  303. }
  304. if (box.mh) {
  305. box.mh.css({
  306. width: box.bwidth(),
  307. height: box.bheight()
  308. });
  309. }
  310. }
  311. }
  312. }).resize(function() {
  313. if (arrweebox.length > 0) {
  314. var box = self.getTopBox();
  315. if (box.options.position == "center") {
  316. box.setCenterPosition();
  317. }
  318. if (box.mh) {
  319. box.mh.css({
  320. width: box.bwidth(),
  321. height: box.bheight()
  322. });
  323. }
  324. }
  325. });
  326. }
  327. $.extend({weefloats: new weefloats()});
  328. })(jQuery);