jqModal

jqModal plugin

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/3595/10865/jqModal.js

  1. /*
  2. * jqModal - Minimalist Modaling with jQuery
  3. *
  4. * Copyright (c) 2007-2014 Brice Burgess @IceburgBrice
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. *
  9. * $Version: 1.1.0 (2014.07.03 +r21)
  10. * Requires: jQuery 1.2.3+
  11. */
  12.  
  13. (function($) {
  14. /**
  15. * Initialize a set of elements as "modals". Modals typically are popup dialogs,
  16. * notices, modal windows, &c.
  17. *
  18. * @name jqm
  19. * @param options user defined options, augments defaults.
  20. * @type jQuery
  21. * @cat Plugins/jqModal
  22. */
  23. $.fn.jqm=function(options){
  24. return this.each(function(){
  25. var e = $(this),
  26. jqm = e.data('jqm') || $.extend({ID: I++}, $.jqm.params),
  27. o = $.extend(jqm,options);
  28. // add/extend options to modal and mark as initialized
  29. e.data('jqm',o).addClass('jqm-init');
  30. // ... Attach events to trigger showing of this modal
  31. o.trigger && e.jqmAddTrigger(o.trigger);
  32. });
  33. };
  34. /**
  35. * Matching modals will have their jqmShow() method fired by attaching a
  36. * onClick event to elements matching `trigger`.
  37. *
  38. * @name jqmAddTrigger
  39. * @param trigger a selector String, jQuery collection of elements, or a DOM element.
  40. */
  41. $.fn.jqmAddTrigger=function(trigger){
  42. return this.each(function(){
  43. if(!addTrigger($(this), 'jqmShow', trigger))
  44. err("jqmAddTrigger must be called on initialized modals")
  45. });
  46. };
  47. /**
  48. * Matching modals will have their jqmHide() method fired by attaching an
  49. * onClick event to elements matching `trigger`.
  50. *
  51. * @name jqmAddClose
  52. * @param trigger a selector String, jQuery collection of elements, or a DOM element.
  53. */
  54. $.fn.jqmAddClose=function(trigger){
  55. return this.each(function(){
  56. if(!addTrigger($(this), 'jqmHide', trigger))
  57. err("jqmAddClose must be called on initialized modals")
  58. });
  59. };
  60. /**
  61. * Open matching modals (if not shown)
  62. */
  63. $.fn.jqmShow=function(trigger){
  64. return this.each(function(){ !this._jqmShown&&show($(this), trigger); });
  65. };
  66. /**
  67. * Close matching modals
  68. */
  69. $.fn.jqmHide=function(trigger){
  70. return this.each(function(){ this._jqmShown&&hide($(this), trigger); });
  71. };
  72. // utility functions
  73. var
  74. err = function(msg){
  75. if(window.console && window.console.error) window.console.error(msg);
  76. }, show = function(e, t){
  77. /**
  78. * e = modal element (as jQuery object)
  79. * t = triggering element
  80. *
  81. * o = options
  82. * z = z-index of modal
  83. * v = overlay element (as jQuery object)
  84. * h = hash (for jqModal <= r15 compatibility)
  85. */
  86. var o = e.data('jqm'),
  87. t = t || window.event,
  88. z = (parseInt(e.css('z-index'))),
  89. z = (z > 0) ? z : 3000,
  90. v = $('<div></div>').addClass(o.overlayClass).css({height:'100%',width:'100%',position:'fixed',left:0,top:0,'z-index':z-1,opacity:o.overlay/100}),
  91. // maintain legacy "hash" construct
  92. h = {w: e, c: o, o: v, t: t};
  93. e.css('z-index',z);
  94.  
  95. if(o.ajax){
  96. var target = o.target || e,
  97. url = o.ajax;
  98. target = (typeof target == 'string') ? $(target,e) : $(target);
  99. if(url.substr(0,1) == '@') url = $(t).attr(url.substring(1));
  100. // Load the Ajax Content (and once loaded);
  101. // Fire the onLoad callback (if exists),
  102. // Attach closing events to elements inside the modal that match the closingClass,
  103. // and Execute the jqModal default Open Callback
  104. target.html(o.ajaxText).load(url,function(){
  105. o.onLoad && o.onLoad.call(this,h);
  106. open(h);
  107. });
  108. }
  109. else { open(h); }
  110. }, hide = function(e, t){
  111. /**
  112. * e = modal element (as jQuery object)
  113. * t = triggering element
  114. *
  115. * o = options
  116. * h = hash (for jqModal <= r15 compatibility)
  117. */
  118. var o = e.data('jqm'),
  119. t = t || window.event,
  120. // maintain legacy "hash" construct
  121. h = {w: e, c: o, o: e.data('jqmv'), t: t};
  122. close(h);
  123. }, onShow = function(hash){
  124. // onShow callback. Responsible for showing a modal and overlay.
  125. // return false to stop opening modal.
  126. // hash object;
  127. // w: (jQuery object) The modal element
  128. // c: (object) The modal's options object
  129. // o: (jQuery object) The overlay element
  130. // t: (DOM object) The triggering element
  131. // display the overlay (prepend to body) if not disabled
  132. if(hash.c.overlay > 0)
  133. hash.o.prependTo('body');
  134. // make modal visible
  135. hash.w.show();
  136. // call focusFunc (attempts to focus on first input in modal)
  137. $.jqm.focusFunc(hash.w);
  138. return true;
  139. }, onHide = function(hash){
  140. // onHide callback. Responsible for hiding a modal and overlay.
  141. // return false to stop closing modal.
  142. // hash object;
  143. // w: (jQuery object) The modal element
  144. // c: (object) The modal's options object
  145. // o: (jQuery object) The overlay element
  146. // t: (DOM object) The triggering element
  147. // hide modal and if overlay, remove overlay.
  148. hash.w.hide() && hash.o && hash.o.remove();
  149. return true;
  150. }, addTrigger = function(e, key, trigger){
  151. // addTrigger: Adds a jqmShow/jqmHide (key) event click on modal (e)
  152. // to all elements that match trigger string (trigger)
  153. var jqm = e.data('jqm');
  154. // return false if e is not an initialized modal element
  155. if(!e.data('jqm')) return false;
  156. return $(trigger).each(function(){
  157. this[key] = this[key] || [];
  158. // register this modal with this trigger only once
  159. if($.inArray(jqm.ID,this[key]) < 0) {
  160. this[key].push(jqm.ID);
  161. // register trigger click event for this modal
  162. $(this).click(function(){
  163. var trigger = this;
  164. e[key](this);
  165. // stop trigger click event from bubbling
  166. return false;
  167. });
  168. }
  169. });
  170. }, open = function(h){
  171. // open: executes the onOpen callback + performs common tasks if successful
  172.  
  173. // transform legacy hash into new var shortcuts
  174. var e = h.w,
  175. v = h.o,
  176. o = h.c;
  177.  
  178. // execute onShow callback
  179. if(o.onShow(h) !== false){
  180. // mark modal as shown
  181. e[0]._jqmShown = true;
  182. // if modal dialog
  183. //
  184. // Bind the Keep Focus Function [F] if no other Modals are open (!A[0]) +
  185. // Add this modal to the opened modals stack (A) for nested modal support
  186. //
  187. // else, close dialog when overlay is clicked
  188. if(o.modal){ !A[0]&&F('bind'); A.push(e); }
  189. else e.jqmAddClose(v);
  190. // Attach closing events to elements inside the modal that match the closingClass
  191. o.closeClass&&e.jqmAddClose($('.' + o.closeClass,e));
  192. // IF toTop is true and overlay exists;
  193. // Add placeholder element <span id="jqmP#ID_of_modal"/> before modal to
  194. // remember it's position in the DOM and move it to a child of the body tag (after overlay)
  195. o.toTop&&v&&e.before('<span id="jqmP'+o.ID+'"></span>').insertAfter(v);
  196. // remember overlay (for closing function)
  197. e.data('jqmv',v);
  198. }
  199. }, close = function(h){
  200. // close: executes the onHide callback + performs common tasks if successful
  201.  
  202. // transform legacy hash into new var shortcuts
  203. var e = h.w,
  204. v = h.o,
  205. o = h.c;
  206.  
  207. // execute onShow callback
  208. if(o.onHide(h) !== false){
  209. // mark modal as !shown
  210. e[0]._jqmShown = false;
  211. // If modal, remove from modal stack.
  212. // If no modals in modal stack, unbind the Keep Focus Function
  213. if(o.modal){A.pop();!A[0]&&F('unbind');}
  214. // IF toTop was passed and an overlay exists;
  215. // Move modal back to its "remembered" position.
  216. o.toTop&&v&&$('#jqmP'+o.ID).after(e).remove();
  217. }
  218. }, F = function(t){
  219. // F: The Keep Focus Function (for modal: true dialos)
  220. // Binds or Unbinds (t) the Focus Examination Function (X) to keypresses and clicks
  221. $(document)[t]("keypress keydown mousedown",X);
  222. }, X = function(e){
  223. // X: The Focus Examination Function (for modal: true dialogs)
  224.  
  225. var modal = $(e.target).data('jqm') || $(e.target).parents('.jqm-init:first').data('jqm'),
  226. activeModal = A[A.length-1].data('jqm');
  227. // allow bubbling if event target is within active modal dialog
  228. if(modal && modal.ID == activeModal.ID) return true;
  229.  
  230. // else, trigger focusFunc (focus on first input element and halt bubbling)
  231. return $.jqm.focusFunc(activeModal);
  232. },
  233. I = 0, // modal ID increment (for nested modals)
  234. A = []; // array of active modals (used to lock interactivity to appropriate modal)
  235. // $.jqm, overridable defaults
  236. $.jqm = {
  237. /**
  238. * default options
  239. *
  240. * (Integer) overlay - [0-100] Translucency percentage (opacity) of the body covering overlay. Set to 0 for NO overlay, and up to 100 for a 100% opaque overlay.
  241. * (String) overlayClass - Applied to the body covering overlay. Useful for controlling overlay look (tint, background-image, &c) with CSS.
  242. * (String) closeClass - Children of the modal element matching `closeClass` will fire the onHide event (to close the modal).
  243. * (Mixed) trigger - Matching elements will fire the onShow event (to display the modal). Trigger can be a selector String, a jQuery collection of elements, a DOM element, or a False boolean.
  244. * (String) ajax - URL to load content from via an AJAX request. False to disable ajax. If ajax begins with a "@", the URL is extracted from the attribute of the triggering element (e.g. use '@data-url' for; <a href="#" class="jqModal" data-url="modal.html">...)
  245. * (Mixed) target - Children of the modal element to load the ajax response into. If false, modal content will be overwritten by ajax response. Useful for retaining modal design.
  246. * Target may be a selector string, jQuery collection of elements, or a DOM element -- and MUST exist as a child of the modal element.
  247. * (String) ajaxText - Text shown while waiting for ajax return. Replaces HTML content of `target` element.
  248. * (Boolean) modal - If true, user interactivity will be locked to the modal window until closed.
  249. * (Boolean) toTop - If true, modal will be posistioned as a first child of the BODY element when opened, and its DOM posistion restored when closed. Useful for overcoming z-Index container issues.
  250. * (Function) onShow - User defined callback function fired when modal opened.
  251. * (Function) onHide - User defined callback function fired when modal closed.
  252. * (Function) onLoad - User defined callback function fired when ajax content loads.
  253. */
  254. params: {
  255. overlay: 50,
  256. overlayClass: 'jqmOverlay',
  257. closeClass: 'jqmClose',
  258. trigger: '.jqModal',
  259. ajax: false,
  260. target: false,
  261. ajaxText: '',
  262. modal: false,
  263. toTop: false,
  264. onShow: onShow,
  265. onHide: onHide,
  266. onLoad: false
  267. },
  268. // focusFunc is fired when a modal is shown, or when interaction occurs outside
  269. // a modal enabled dialog. Passed the modal element.
  270. focusFunc: function(e) { $(':input:visible:first',e).focus(); return false; }
  271. };
  272. })( jQuery );

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址