jquery.bgiframe.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*! Copyright (c) 2013 Brandon Aaron (http://brandon.aaron.sh)
  2. * Licensed under the MIT License (LICENSE.txt).
  3. *
  4. * Version 3.0.1
  5. *
  6. * Requires jQuery >= 1.2.6
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD. Register as an anonymous module.
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. // Node/CommonJS style for Browserify
  14. module.exports = factory;
  15. } else {
  16. // Browser globals
  17. factory(jQuery);
  18. }
  19. }(function ($) {
  20. $.fn.bgiframe = function(s) {
  21. s = $.extend({
  22. top : 'auto', // auto == borderTopWidth
  23. left : 'auto', // auto == borderLeftWidth
  24. width : 'auto', // auto == offsetWidth
  25. height : 'auto', // auto == offsetHeight
  26. opacity : true,
  27. src : 'javascript:false;',
  28. conditional : /MSIE 6\.0/.test(navigator.userAgent) // expresion or function. return false to prevent iframe insertion
  29. }, s);
  30. // wrap conditional in a function if it isn't already
  31. if (!$.isFunction(s.conditional)) {
  32. var condition = s.conditional;
  33. s.conditional = function() { return condition; };
  34. }
  35. var $iframe = $('<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
  36. 'style="display:block;position:absolute;z-index:-1;"/>');
  37. return this.each(function() {
  38. var $this = $(this);
  39. if ( s.conditional(this) === false ) { return; }
  40. var existing = $this.children('iframe.bgiframe');
  41. var $el = existing.length === 0 ? $iframe.clone() : existing;
  42. $el.css({
  43. 'top': s.top == 'auto' ?
  44. ((parseInt($this.css('borderTopWidth'),10)||0)*-1)+'px' : prop(s.top),
  45. 'left': s.left == 'auto' ?
  46. ((parseInt($this.css('borderLeftWidth'),10)||0)*-1)+'px' : prop(s.left),
  47. 'width': s.width == 'auto' ? (this.offsetWidth + 'px') : prop(s.width),
  48. 'height': s.height == 'auto' ? (this.offsetHeight + 'px') : prop(s.height),
  49. 'opacity': s.opacity === true ? 0 : undefined
  50. });
  51. if ( existing.length === 0 ) {
  52. $this.prepend($el);
  53. }
  54. });
  55. };
  56. // old alias
  57. $.fn.bgIframe = $.fn.bgiframe;
  58. function prop(n) {
  59. return n && n.constructor === Number ? n + 'px' : n;
  60. }
  61. }));