background-blur.min.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. !function(t) {
  2. "use strict";
  3. function e(e) {
  4. return this.each(function() {
  5. var i = t(this)
  6. , n = i.data("plugin.backgroundBlur")
  7. , o = t.extend({}, r.DEFAULTS, i.data(), "object" == typeof e && e);
  8. n || i.data("plugin.backgroundBlur", n = new r(this,o)),
  9. "fadeIn" === e ? n.fadeIn() : "fadeOut" === e ? n.fadeOut() : "string" == typeof e && n.generateBlurredImage(e)
  10. })
  11. }
  12. var i = function() {
  13. for (var t, e = 3, i = document.createElement("div"), n = i.getElementsByTagName("i"); i.innerHTML = "<!--[if gt IE " + ++e + "]><i></i><![endif]-->",
  14. n[0]; )
  15. ;
  16. return e > 4 ? e : t
  17. }()
  18. , n = function() {
  19. return "_" + Math.random().toString(36).substr(2, 9)
  20. }
  21. , o = {
  22. svgns: "http://www.w3.org/2000/svg",
  23. xlink: "http://www.w3.org/1999/xlink",
  24. createElement: function(t, e) {
  25. var i = document.createElementNS(o.svgns, t);
  26. return e && o.setAttr(i, e),
  27. i
  28. },
  29. setAttr: function(t, e) {
  30. for (var i in e)
  31. "href" === i ? t.setAttributeNS(o.xlink, i, e[i]) : t.setAttribute(i, e[i]);
  32. return t
  33. }
  34. }
  35. , r = function(e, i) {
  36. this.internalID = n(),
  37. this.$element = t(e),
  38. this.$width = this.$element.width(),
  39. this.$height = this.$element.height(),
  40. this.element = e,
  41. this.options = t.extend({}, r.DEFAULTS, i),
  42. this.$overlayEl = this.createOverlay(),
  43. this.$blurredImage = {},
  44. this.useVelocity = this.detectVelocity(),
  45. this.attachListeners(),
  46. this.generateBlurredImage(this.options.imageURL)
  47. };
  48. r.VERSION = "0.0.1",
  49. r.DEFAULTS = {
  50. imageURL: "",
  51. blurAmount: 10,
  52. imageClass: "",
  53. overlayClass: "",
  54. duration: !1,
  55. opacity: 1
  56. },
  57. r.prototype.detectVelocity = function() {
  58. return !!window.jQuery.Velocity
  59. }
  60. ,
  61. r.prototype.attachListeners = function() {
  62. this.$element.on("ui.blur.loaded", t.proxy(this.fadeIn, this)),
  63. this.$element.on("ui.blur.unload", t.proxy(this.fadeOut, this))
  64. }
  65. ,
  66. r.prototype.fadeIn = function() {
  67. this.options.duration && this.options.duration > 0 && (this.useVelocity ? this.$blurredImage.velocity({
  68. opacity: this.options.opacity
  69. }, {
  70. duration: this.options.duration
  71. }) : this.$blurredImage.animate({
  72. opacity: this.options.opacity
  73. }, {
  74. duration: this.options.duration
  75. }))
  76. }
  77. ,
  78. r.prototype.fadeOut = function() {
  79. this.options.duration && this.options.duration > 0 ? this.useVelocity ? this.$blurredImage.velocity({
  80. opacity: 0
  81. }, {
  82. duration: this.options.duration
  83. }) : this.$blurredImage.animate({
  84. opacity: 0
  85. }, {
  86. duration: this.options.duration
  87. }) : this.$blurredImage.css({
  88. opacity: 0
  89. })
  90. }
  91. ,
  92. r.prototype.generateBlurredImage = function(t) {
  93. var e = this.$blurredImage;
  94. this.internalID = n(),
  95. e.length > 0 && (this.options.duration && this.options.duration > 0 ? this.useVelocity ? e.first().velocity({
  96. opacity: 0
  97. }, {
  98. duration: this.options.duration,
  99. complete: function() {
  100. e.remove()
  101. }
  102. }) : e.first().animate({
  103. opacity: 0
  104. }, {
  105. duration: this.options.duration,
  106. complete: function() {
  107. e.remove()
  108. }
  109. }) : e.remove()),
  110. this.$blurredImage = i ? this.createIMG(t, this.$width, this.$height) : this.createSVG(t, this.$width, this.$height)
  111. }
  112. ,
  113. r.prototype.createOverlay = function() {
  114. return this.options.overlayClass && "" !== this.options.overlayClass ? t("<div></div>").prependTo(this.$element).addClass(this.options.overlayClass) : !1
  115. }
  116. ,
  117. r.prototype.createSVG = function(e, i, n) {
  118. var r = this
  119. , s = o.createElement("svg", {
  120. xmlns: o.svgns,
  121. version: "1.1",
  122. width: i,
  123. height: n,
  124. id: "blurred" + this.internalID,
  125. "class": this.options.imageClass,
  126. viewBox: "0 0 " + i + " " + n,
  127. preserveAspectRatio: "none"
  128. })
  129. , a = "blur" + this.internalID
  130. , u = o.createElement("filter", {
  131. id: a
  132. })
  133. , l = o.createElement("feGaussianBlur", {
  134. "in": "SourceGraphic",
  135. stdDeviation: this.options.blurAmount
  136. })
  137. , h = o.createElement("image", {
  138. x: 0,
  139. y: 0,
  140. width: i,
  141. height: n,
  142. externalResourcesRequired: "true",
  143. href: e,
  144. style: "filter:url(#" + a + ")",
  145. preserveAspectRatio: "none"
  146. });
  147. h.addEventListener("load", function() {
  148. r.$element.trigger("ui.blur.loaded")
  149. }, !0),
  150. h.addEventListener("SVGLoad", function() {
  151. r.$element.trigger("ui.blur.loaded")
  152. }, !0),
  153. u.appendChild(l),
  154. s.appendChild(u),
  155. s.appendChild(h);
  156. var d = t(s);
  157. return r.options.duration && r.options.duration > 0 && (d.css({
  158. opacity: 0
  159. }),
  160. window.setTimeout(function() {
  161. "0" === d.css("opacity") && d.css({
  162. opacity: 1
  163. })
  164. }, this.options.duration + 100)),
  165. this.element.insertBefore(s, this.element.firstChild),
  166. d
  167. }
  168. ,
  169. r.prototype.createIMG = function(t, e, i) {
  170. var n = this
  171. , o = this.prependImage(t)
  172. , r = 2 * this.options.blurAmount > 100 ? 100 : 2 * this.options.blurAmount;
  173. return o.css({
  174. filter: "progid:DXImageTransform.Microsoft.Blur(pixelradius=" + r + ") ",
  175. top: 2.5 * -this.options.blurAmount,
  176. left: 2.5 * -this.options.blurAmount,
  177. width: e + 2.5 * this.options.blurAmount,
  178. height: i + 2.5 * this.options.blurAmount
  179. }).attr("id", "blurred" + this.internalID),
  180. o.load(function() {
  181. n.$element.trigger("ui.blur.loaded")
  182. }),
  183. this.options.duration && this.options.duration > 0 && window.setTimeout(function() {
  184. "0" === o.css("opacity") && o.css({
  185. opacity: 1
  186. })
  187. }, this.options.duration + 100),
  188. o
  189. }
  190. ,
  191. r.prototype.prependImage = function(e) {
  192. var i, n = t('<img src="' + e + '" />');
  193. return i = this.$overlayEl ? n.insertBefore(this.$overlayEl).attr("id", this.internalID).addClass(this.options.imageClass) : n.prependTo(this.$element).attr("id", this.internalID).addClass(this.options.imageClass)
  194. }
  195. ;
  196. var s = t.fn.backgroundBlur;
  197. t.fn.backgroundBlur = e,
  198. t.fn.backgroundBlur.Constructor = r,
  199. t.fn.backgroundBlur.noConflict = function() {
  200. return t.fn.backgroundBlur = s,
  201. this
  202. }
  203. }(jQuery);