1/* 2 * Backstretch 3 * http://srobbin.com/jquery-plugins/backstretch/ 4 * 5 * Copyright (c) 2013 Scott Robbin 6 * Licensed under the MIT license. 7 */ 8 9;(function ($, window, undefined) { 10 'use strict'; 11 12 /* PLUGIN DEFINITION 13 * ========================= */ 14 15 $.fn.backstretch = function (images, options) { 16 // We need at least one image or method name 17 if (images === undefined || images.length === 0) { 18 $.error("No images were supplied for Backstretch"); 19 } 20 21 /* 22 * Scroll the page one pixel to get the right window height on iOS 23 * Pretty harmless for everyone else 24 */ 25 if ($(window).scrollTop() === 0 ) { 26 window.scrollTo(0, 0); 27 } 28 29 return this.each(function () { 30 var $this = $(this) 31 , obj = $this.data('backstretch'); 32 33 // Do we already have an instance attached to this element? 34 if (obj) { 35 36 // Is this a method they're trying to execute? 37 if (typeof images == 'string' && typeof obj[images] == 'function') { 38 // Call the method 39 obj[images](options); 40 41 // No need to do anything further 42 return; 43 } 44 45 // Merge the old options with the new 46 options = $.extend(obj.options, options); 47 48 // Remove the old instance 49 obj.destroy(true); 50 } 51 52 obj = new Backstretch(this, images, options); 53 $this.data('backstretch', obj); 54 }); 55 }; 56 57 // If no element is supplied, we'll attach to body 58 $.backstretch = function (images, options) { 59 // Return the instance 60 return $('body') 61 .backstretch(images, options) 62 .data('backstretch'); 63 }; 64 65 // Custom selector 66 $.expr[':'].backstretch = function(elem) { 67 return $(elem).data('backstretch') !== undefined; 68 }; 69 70 /* DEFAULTS 71 * ========================= */ 72 73 $.fn.backstretch.defaults = { 74 centeredX: true // Should we center the image on the X axis? 75 , centeredY: true // Should we center the image on the Y axis? 76 , duration: 5000 // Amount of time in between slides (if slideshow) 77 , fade: 0 // Speed of fade transition between slides 78 }; 79 80 /* STYLES 81 * 82 * Baked-in styles that we'll apply to our elements. 83 * In an effort to keep the plugin simple, these are not exposed as options. 84 * That said, anyone can override these in their own stylesheet. 85 * ========================= */ 86 var styles = { 87 wrap: { 88 left: 0 89 , top: 0 90 , overflow: 'hidden' 91 , margin: 0 92 , padding: 0 93 , height: '100%' 94 , width: '100%' 95 , zIndex: -999999 96 } 97 , img: { 98 position: 'absolute' 99 , display: 'none' 100 , margin: 0 101 , padding: 0 102 , border: 'none' 103 , width: 'auto' 104 , height: 'auto' 105 , maxHeight: 'none' 106 , maxWidth: 'none' 107 , zIndex: -999999 108 } 109 }; 110 111 /* CLASS DEFINITION 112 * ========================= */ 113 var Backstretch = function (container, images, options) { 114 this.options = $.extend({}, $.fn.backstretch.defaults, options || {}); 115 116 /* In its simplest form, we allow Backstretch to be called on an image path. 117 * e.g. $.backstretch('/path/to/image.jpg') 118 * So, we need to turn this back into an array. 119 */ 120 this.images = $.isArray(images) ? images : [images]; 121 122 // Preload images 123 $.each(this.images, function () { 124 $('<img />')[0].src = this; 125 }); 126 127 // Convenience reference to know if the container is body. 128 this.isBody = container === document.body; 129 130 /* We're keeping track of a few different elements 131 * 132 * Container: the element that Backstretch was called on. 133 * Wrap: a DIV that we place the image into, so we can hide the overflow. 134 * Root: Convenience reference to help calculate the correct height. 135 */ 136 this.$container = $(container); 137 this.$root = this.isBody ? supportsFixedPosition ? $(window) : $(document) : this.$container; 138 139 // Don't create a new wrap if one already exists (from a previous instance of Backstretch) 140 var $existing = this.$container.children(".backstretch").first(); 141 this.$wrap = $existing.length ? $existing : $('<div class="backstretch"></div>').css(styles.wrap).appendTo(this.$container); 142 143 // Non-body elements need some style adjustments 144 if (!this.isBody) { 145 // If the container is statically positioned, we need to make it relative, 146 // and if no zIndex is defined, we should set it to zero. 147 var position = this.$container.css('position') 148 , zIndex = this.$container.css('zIndex'); 149 150 this.$container.css({ 151 position: position === 'static' ? 'relative' : position 152 , zIndex: zIndex === 'auto' ? 0 : zIndex 153 , background: 'none' 154 }); 155 156 // Needs a higher z-index 157 this.$wrap.css({zIndex: -999998}); 158 } 159 160 // Fixed or absolute positioning? 161 this.$wrap.css({ 162 position: this.isBody && supportsFixedPosition ? 'fixed' : 'absolute' 163 }); 164 165 // Set the first image 166 this.index = 0; 167 this.show(this.index); 168 169 // Listen for resize 170 $(window).on('resize.backstretch', $.proxy(this.resize, this)) 171 .on('orientationchange.backstretch', $.proxy(function () { 172 // Need to do this in order to get the right window height 173 if (this.isBody && window.pageYOffset === 0) { 174 window.scrollTo(0, 1); 175 this.resize(); 176 } 177 }, this)); 178 }; 179 180 /* PUBLIC METHODS 181 * ========================= */ 182 Backstretch.prototype = { 183 resize: function () { 184 try { 185 var bgCSS = {left: 0, top: 0} 186 , rootWidth = this.isBody ? this.$root.width() : this.$root.innerWidth() 187 , bgWidth = rootWidth 188 , rootHeight = this.isBody ? ( window.innerHeight ? window.innerHeight : this.$root.height() ) : this.$root.innerHe
188ight() 189 , bgHeight = bgWidth / this.$img.data('ratio') 190 , bgOffset; 191 192 // Make adjustments based on image ratio 193 if (bgHeight >= rootHeight) { 194 bgOffset = (bgHeight - rootHeight) / 2; 195 if(this.options.centeredY) { 196 bgCSS.top = '-' + bgOffset + 'px'; 197 } 198 } else { 199 bgHeight = rootHeight; 200 bgWidth = bgHeight * this.$img.data('ratio'); 201 bgOffset = (bgWidth - rootWidth) / 2; 202 if(this.options.centeredX) { 203 bgCSS.left = '-' + bgOffset + 'px'; 204 } 205 } 206 207 this.$wrap.css({width: rootWidth, height: rootHeight}) 208 .find('img:not(.deleteable)').css({width: bgWidth, height: bgHeight}).css(bgCSS); 209 } catch(err) { 210 // IE7 seems to trigger resize before the image is loaded. 211 // This try/catch block is a hack to let it fail gracefully. 212 } 213 214 return this; 215 } 216 217 // Show the slide at a certain position 218 , show: function (newIndex) { 219 220 // Validate index 221 if (Math.abs(newIndex) > this.images.length - 1) { 222 return; 223 } 224 225 // Vars 226 var self = this 227 , oldImage = self.$wrap.find('img').addClass('deleteable') 228 , evtOptions = { relatedTarget: self.$container[0] }; 229 230 // Trigger the "before" event 231 self.$container.trigger($.Event('backstretch.before', evtOptions), [self, newIndex]); 232 233 // Set the new index 234 this.index = newIndex; 235 236 // Pause the slideshow 237 clearInterval(self.interval); 238 239 // New image 240 self.$img = $('<img />') 241 .css(styles.img) 242 .bind('load', function (e) { 243 var imgWidth = this.width || $(e.target).width() 244 , imgHeight = this.height || $(e.target).height(); 245 246 // Save the ratio 247 $(this).data('ratio', imgWidth / imgHeight); 248 249 // Show the image, then delete the old one 250 // "speed" option has been deprecated, but we want backwards compatibilty 251 $(this).fadeIn(self.options.speed || self.options.fade, function () { 252 oldImage.remove(); 253 254 // Resume the slideshow 255 if (!self.paused) { 256 self.cycle(); 257 } 258 259 // Trigger the "after" and "show" events 260 // "show" is being deprecated 261 $(['after', 'show']).each(function () { 262 self.$container.trigger($.Event('backstretch.' + this, evtOptions), [self, newIndex]); 263 }); 264 }); 265 266 // Resize 267 self.resize(); 268 }) 269 .appendTo(self.$wrap); 270 271 // Hack for IE img onload event 272 self.$img.attr('src', self.images[newIndex]); 273 return self; 274 } 275 276 , next: function () { 277 // Next slide 278 return this.show(this.index < this.images.length - 1 ? this.index + 1 : 0); 279 } 280 281 , prev: function () { 282 // Previous slide 283 return this.show(this.index === 0 ? this.images.length - 1 : this.index - 1); 284 } 285 286 , pause: function () { 287 // Pause the slideshow 288 this.paused = true; 289 return this; 290 } 291 292 , resume: function () { 293 // Resume the slideshow 294 this.paused = false; 295 this.next(); 296 return this; 297 } 298 299 , cycle: function () { 300 // Start/resume the slideshow 301 if(this.images.length > 1) { 302 // Clear the interval, just in case 303 clearInterval(this.interval); 304 305 this.interval = setInterval($.proxy(function () { 306 // Check for paused slideshow 307 if (!this.paused) { 308 this.next(); 309 } 310 }, this), this.options.duration); 311 } 312 return this; 313 } 314
vendor: 2,463 bytes, lines 315-381
315 , destroy: function (preserveBackground) { 316 // Stop the resize events 317 $(window).off('resize.backstretch orientationchange.backstretch'); 318 319 // Clear the interval 320 clearInterval(this.interval); 321 322 // Remove Backstretch 323 if(!preserveBackground) { 324 this.$wrap.remove(); 325 } 326 this.$container.removeData('backstretch'); 327 } 328 }; 329 330 /* SUPPORTS FIXED POSITION? 331 * 332 * Based on code from jQuery Mobile 1.1.0 333 * http://jquerymobile.com/ 334 * 335 * In a nutshell, we need to figure out if fixed positioning is supported. 336 * Unfortunately, this is very difficult to do on iOS, and usually involves 337 * injecting content, scrolling the page, etc.. It's ugly. 338 * jQuery Mobile uses this workaround. It's not ideal, but works. 339 * 340 * Modified to detect IE6 341 * ========================= */ 342 343 var supportsFixedPosition = (function () { 344 var ua = navigator.userAgent 345 , platform = navigator.platform 346 // Rendering engine is Webkit, and capture major version 347 , wkmatch = ua.match( /AppleWebKit\/([0-9]+)/ ) 348 , wkversion = !!wkmatch && wkmatch[ 1 ] 349 , ffmatch = ua.match( /Fennec\/([0-9]+)/ ) 350 , ffversion = !!ffmatch && ffmatch[ 1 ] 351 , operammobilematch = ua.match( /Opera Mobi\/([0-9]+)/ ) 352 , omversion = !!operammobilematch && operammobilematch[ 1 ] 353 , iematch = ua.match( /MSIE ([0-9]+)/ ) 354 , ieversion = !!iematch && iematch[ 1 ]; 355 356 return !( 357 // iOS 4.3 and older : Platform is iPhone/Pad/Touch and Webkit version is less than 534 (ios5) 358 ((platform.indexOf( "iPhone" ) > -1 || platform.indexOf( "iPad" ) > -1 || platform.indexOf( "iPod" ) > -1 ) && wkversion && wkversion < 534) || 359 360 // Opera Mini 361 (window.operamini && ({}).toString.call( window.operamini ) === "[object OperaMini]") || 362 (operammobilematch && omversion < 7458) || 363 364 //Android lte 2.1: Platform is Android and Webkit version is less than 533 (Android 2.2) 365 (ua.indexOf( "Android" ) > -1 && wkversion && wkversion < 533) || 366 367 // Firefox Mobile before 6.0 - 368 (ffversion && ffversion < 6) || 369 370 // WebOS less than 3 371 ("palmGetResource" in window && wkversion && wkversion < 534) || 372 373 // MeeGo 374 (ua.indexOf( "MeeGo" ) > -1 && ua.indexOf( "NokiaBrowser/8.5.0" ) > -1) || 375 376 // IE6 377 (ieversion && ieversion <= 6) 378 ); 379 }()); 380 381}(jQuery, window));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.