bPopup

bpopup from http://dinbror.dk/bpopup

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/2723/7539/bPopup.js

  1. /*===================================================================================================================
  2. * @name: bPopup
  3. * @type: jQuery
  4. * @author: (c) Bjoern Klinggaard - @bklinggaard
  5. * @demo: http://dinbror.dk/bpopup
  6. * @version: 0.9.4
  7. * @requires jQuery 1.4.3
  8. *==================================================================================================================*/
  9. // Do With It What You Want But Please Visit My Sponsor license: http://dinbror.dk/blog/bpopup/#license */
  10. ;(function($) {
  11. 'use strict';
  12. $.fn.bPopup = function(options, callback) {
  13. if ($.isFunction(options)) {
  14. callback = options;
  15. options = null;
  16. }
  17.  
  18. // OPTIONS
  19. var o = $.extend({}, $.fn.bPopup.defaults, options);
  20. // HIDE SCROLLBAR?
  21. if (!o.scrollBar)
  22. $('html').css('overflow', 'hidden');
  23. // VARIABLES
  24. var $popup = this
  25. , d = $(document)
  26. , w = window
  27. , $w = $(w)
  28. , wH = windowHeight()
  29. , wW = windowWidth()
  30. , prefix = '__b-popup'
  31. , isIOS6X = (/OS 6(_\d)+/i).test(navigator.userAgent) // Used for a temporary fix for ios6 timer bug when using zoom/scroll
  32. , buffer = 200
  33. , popups = 0
  34. , id
  35. , inside
  36. , fixedVPos
  37. , fixedHPos
  38. , fixedPosStyle
  39. , vPos
  40. , hPos
  41. , height
  42. , width
  43. , debounce
  44. ;
  45.  
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. // PUBLIC FUNCTION - call it: $(element).bPopup().close();
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. $popup.close = function() {
  50. o = this.data('bPopup');
  51. id = prefix +$w.data('bPopup') + '__';
  52. close();
  53. };
  54.  
  55. return $popup.each(function() {
  56. if ($(this).data('bPopup')) return; //POPUP already exists?
  57. init();
  58. });
  59.  
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. // HELPER FUNCTIONS - PRIVATE
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. function init() {
  64. triggerCall(o.onOpen);
  65. popups = ($w.data('bPopup') || 0) + 1, id = prefix + popups + '__',fixedVPos = o.position[1] !== 'auto', fixedHPos = o.position[0] !== 'auto', fixedPosStyle = o.positionStyle === 'fixed', height = $popup.outerHeight(true), width = $popup.outerWidth(true);
  66. o.loadUrl ? createContent() : open();
  67. };
  68. function createContent() {
  69. o.contentContainer = $(o.contentContainer || $popup);
  70. switch (o.content) {
  71. case ('iframe'):
  72. var iframe = $('<iframe class="b-iframe" ' + o.iframeAttr +'></iframe>');
  73. iframe.appendTo(o.contentContainer);
  74. height = $popup.outerHeight(true);
  75. width = $popup.outerWidth(true);
  76. open();
  77. iframe.attr('src', o.loadUrl); // setting iframe src after open due IE9 bug
  78. triggerCall(o.loadCallback);
  79. break;
  80. case ('image'):
  81. open();
  82. $('<img />')
  83. .load(function() {
  84. triggerCall(o.loadCallback);
  85. recenter($(this));
  86. }).attr('src', o.loadUrl).hide().appendTo(o.contentContainer);
  87. break;
  88. default:
  89. open();
  90. $('<div class="b-ajax-wrapper"></div>')
  91. .load(o.loadUrl, o.loadData, function(){
  92. triggerCall(o.loadCallback);
  93. recenter($(this));
  94. }).hide().appendTo(o.contentContainer);
  95. break;
  96. }
  97. };
  98.  
  99. function open(){
  100. // MODAL OVERLAY
  101. if (o.modal) {
  102. $('<div class="b-modal '+id+'"></div>')
  103. .css({backgroundColor: o.modalColor, position: 'fixed', top: 0, right:0, bottom:0, left: 0, opacity: 0, zIndex: o.zIndex + popups})
  104. .appendTo(o.appendTo)
  105. .fadeTo(o.speed, o.opacity);
  106. }
  107. // POPUP
  108. calPosition();
  109. $popup
  110. .data('bPopup', o).data('id',id)
  111. .css({
  112. 'left': o.transition == 'slideIn' || o.transition == 'slideBack' ? (o.transition == 'slideBack' ? d.scrollLeft() + wW : (hPos + width) *-1) : getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle))
  113. , 'position': o.positionStyle || 'absolute'
  114. , 'top': o.transition == 'slideDown' || o.transition == 'slideUp' ? (o.transition == 'slideUp' ? d.scrollTop() + wH : vPos + height * -1) : getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle))
  115. , 'z-index': o.zIndex + popups + 1
  116. }).each(function() {
  117. if(o.appending) {
  118. $(this).appendTo(o.appendTo);
  119. }
  120. });
  121. doTransition(true);
  122. };
  123. function close() {
  124. if (o.modal) {
  125. $('.b-modal.'+$popup.data('id'))
  126. .fadeTo(o.speed, 0, function() {
  127. $(this).remove();
  128. });
  129. }
  130. // Clean up
  131. unbindEvents();
  132. // Close trasition
  133. doTransition();
  134. return false; // Prevent default
  135. };
  136. //Eksperimental
  137. function recenter(content){
  138. var _width = content.width(), _height = content.height(), css = {};
  139. o.contentContainer.css({height:_height,width:_width});
  140. if (_height >= $popup.height()){
  141. css.height = $popup.height();
  142. }
  143. if(_width >= $popup.width()){
  144. css.width = $popup.width();
  145. }
  146. height = $popup.outerHeight(true)
  147. , width = $popup.outerWidth(true);
  148. calPosition();
  149. o.contentContainer.css({height:'auto',width:'auto'});
  150. css.left = getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)),
  151. css.top = getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle));
  152. $popup
  153. .animate(
  154. css
  155. , 250
  156. , function() {
  157. content.show();
  158. inside = insideWindow();
  159. }
  160. );
  161. };
  162. function bindEvents() {
  163. $w.data('bPopup', popups);
  164. $popup.delegate('.bClose, .' + o.closeClass, 'click.'+id, close); // legacy, still supporting the close class bClose
  165. if (o.modalClose) {
  166. $('.b-modal.'+id).css('cursor', 'pointer').bind('click', close);
  167. }
  168. // Temporary disabling scroll/resize events on devices with IOS6+
  169. // due to a bug where events are dropped after pinch to zoom
  170. if (!isIOS6X && (o.follow[0] || o.follow[1])) {
  171. $w.bind('scroll.'+id, function() {
  172. if(inside){
  173. $popup
  174. .dequeue()
  175. .animate({ 'left': o.follow[0] ? getLeftPos(!fixedPosStyle) : 'auto', 'top': o.follow[1] ? getTopPos(!fixedPosStyle) : 'auto' }, o.followSpeed, o.followEasing);
  176. }
  177. }).bind('resize.'+id, function() {
  178. wH = windowHeight();
  179. wW = windowWidth();
  180. inside = insideWindow();
  181. if(inside){
  182. clearTimeout(debounce);
  183. debounce = setTimeout(function(){
  184. calPosition();
  185. $popup
  186. .dequeue()
  187. .each(function() {
  188. if(fixedPosStyle) {
  189. $(this).css({ 'left': hPos, 'top': vPos });
  190. }
  191. else {
  192. $(this).animate({ 'left': o.follow[0] ? getLeftPos(true) : 'auto', 'top': o.follow[1] ? getTopPos(true) : 'auto' }, o.followSpeed, o.followEasing);
  193. }
  194. });
  195. }, 50);
  196. }
  197. });
  198. }
  199. if (o.escClose) {
  200. d.bind('keydown.'+id, function(e) {
  201. if (e.which == 27) { //escape
  202. close();
  203. }
  204. });
  205. }
  206. };
  207. function unbindEvents() {
  208. if (!o.scrollBar) {
  209. $('html').css('overflow', 'auto');
  210. }
  211. $('.b-modal.'+id).unbind('click');
  212. d.unbind('keydown.'+id);
  213. $w.unbind('.'+id).data('bPopup', ($w.data('bPopup')-1 > 0) ? $w.data('bPopup')-1 : null);
  214. $popup.undelegate('.bClose, .' + o.closeClass, 'click.'+id, close).data('bPopup', null);
  215. };
  216. function doTransition(open) {
  217. switch (open ? o.transition : o.transitionClose || o.transition) {
  218. case "slideIn":
  219. animate({
  220. left: open ? getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() - (width || $popup.outerWidth(true)) - buffer
  221. });
  222. break;
  223. case "slideBack":
  224. animate({
  225. left: open ? getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() + wW + buffer
  226. });
  227. break;
  228. case "slideDown":
  229. animate({
  230. top: open ? getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() - (height || $popup.outerHeight(true)) - buffer
  231. });
  232. break;
  233. case "slideUp":
  234. animate({
  235. top: open ? getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() + wH + buffer
  236. });
  237. break;
  238. default:
  239. //Hardtyping 1 and 0 to ensure opacity 1 and not 0.9999998
  240. $popup.stop().fadeTo(o.speed, open ? 1 : 0, function(){onCompleteCallback(open);});
  241. }
  242. function animate(css){
  243. $popup
  244. .css({display: 'block',opacity: 1})
  245. .animate(css, o.speed, o.easing, function(){ onCompleteCallback(open); });
  246. };
  247. };
  248. function onCompleteCallback(open){
  249. if(open){
  250. bindEvents();
  251. triggerCall(callback);
  252. if(o.autoClose){
  253. setTimeout(close, o.autoClose);
  254. }
  255. } else {
  256. $popup.hide();
  257. triggerCall(o.onClose);
  258. if (o.loadUrl) {
  259. o.contentContainer.empty();
  260. $popup.css({height: 'auto', width: 'auto'});
  261. }
  262. }
  263. };
  264. function getLeftPos(includeScroll){
  265. return includeScroll ? hPos + d.scrollLeft() : hPos;
  266. };
  267. function getTopPos(includeScroll){
  268. return includeScroll ? vPos + d.scrollTop() : vPos;
  269. };
  270. function triggerCall(func) {
  271. $.isFunction(func) && func.call($popup);
  272. };
  273. function calPosition(){
  274. vPos = fixedVPos ? o.position[1] : Math.max(0, ((wH- $popup.outerHeight(true)) / 2) - o.amsl)
  275. , hPos = fixedHPos ? o.position[0] : (wW - $popup.outerWidth(true)) / 2
  276. , inside = insideWindow();
  277. };
  278. function insideWindow(){
  279. return wH > $popup.outerHeight(true) && wW > $popup.outerWidth(true);
  280. };
  281. function windowHeight(){
  282. return w.innerHeight || $w.height();
  283. };
  284. function windowWidth(){
  285. return w.innerWidth || $w.width();
  286. };
  287. };
  288.  
  289. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. // DEFAULT VALUES
  291. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  292. $.fn.bPopup.defaults = {
  293. amsl: 50
  294. , appending: true
  295. , appendTo: 'body'
  296. , autoClose: false
  297. , closeClass: 'b-close'
  298. , content: 'ajax' // ajax, iframe or image
  299. , contentContainer: false
  300. , easing: 'swing'
  301. , escClose: true
  302. , follow: [true, true] // x, y
  303. , followEasing: 'swing'
  304. , followSpeed: 500
  305. , iframeAttr: 'scrolling="no" frameborder="0"'
  306. , loadCallback: false
  307. , loadData: false
  308. , loadUrl: false
  309. , modal: true
  310. , modalClose: true
  311. , modalColor: '#000'
  312. , onClose: false
  313. , onOpen: false
  314. , opacity: 0.7
  315. , position: ['auto', 'auto'] // x, y,
  316. , positionStyle: 'absolute'// absolute or fixed
  317. , scrollBar: true
  318. , speed: 250 // open & close speed
  319. , transition: 'fadeIn' //transitions: fadeIn, slideDown, slideIn
  320. , transitionClose: false
  321. , zIndex: 9997 // popup gets z-index 9999, modal overlay 9998
  322. };
  323. })(jQuery);

QingJ © 2025

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