PageSourceSearch

https://www.ambientsoundbath.com/wp-content/themes/harmonic/js/jquery.backstretch.js?ver=1.0.0

js ambientsoundbath.com collected 2026-10-02 07:54:45 UTC 13,814 bytes, 401 lines download raw bytes

1/*! Backstretch - v2.0.4 - 2013-06-19
2 * http://srobbin.com/jquery-plugins/backstretch/
3 * Copyright (c) 2013 Scott Robbin; Licensed MIT */
4
5;
6(function($, window, undefined) {
7    'use strict';
8
9    /* PLUGIN DEFINITION
10     * ========================= */
11
12    $.fn.backstretch = function(images, options) {
13        // We need at least one image or method name
14        if (images === undefined || images.length === 0) {
15            $.error("No images were supplied for Backstretch");
16        }
17
18        /*
19         * Scroll the page one pixel to get the right window height on iOS
20         * Pretty harmless for everyone else
21         */
22        if ($(window).scrollTop() === 0) {
23            window.scrollTo(0, 0);
24        }
25
26        return this.each(function() {
27            var $this = $(this),
28                obj = $this.data('backstretch');
29
30            // Do we already have an instance attached to this element?
31            if (obj) {
32
33                // Is this a method they're trying to execute?
34                if (typeof images == 'string' && typeof obj[images] == 'function') {
35                    // Call the method
36                    obj[images](options);
37
38                    // No need to do anything further
39                    return;
40                }
41
42                // Merge the old options with the new
43                options = $.extend(obj.options, options);
44
45                // Remove the old instance
46                obj.destroy(true);
47            }
48
49            obj = new Backstretch(this, images, options);
50            $this.data('backstretch', obj);
51        });
52    };
53
54    // If no element is supplied, we'll attach to body
55    $.backstretch = function(images, options) {
56        // Return the instance
57        return $('body')
58            .backstretch(images, options)
59            .data('backstretch');
60    };
61
62    // Custom selector
63    $.expr[':'].backstretch = function(elem) {
64        return $(elem).data('backstretch') !== undefined;
65    };
66
67    /* DEFAULTS
68     * ========================= */
69
70    $.fn.backstretch.defaults = {
71        centeredX: true // Should we center the image on the X axis?
72        ,
73        centeredY: true // Should we center the image on the Y axis?
74        ,
75        duration: 5000 // Amount of time in between slides (if slideshow)
76        ,
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({
158                zIndex: -999998
159            });
160        }
161
162        // Fixed or absolute positioning?
163        this.$wrap.css({
164            position: this.isBody && supportsFixedPosition ? 'fixed' : 'absolute'
165        });
166
167        // Set the first image
168        this.index = 0;
169        this.show(this.index);
170
171        // Listen for resize
172        $(window).on('resize.backstretch', $.proxy(this.resize, this))
173            .on('orientationchange.backstretch', $.proxy(function() {
174                // Need to do this in order to get the right window height
175                if (this.isBody && window.pageYOffset === 0) {
176                    window.scrollTo(0, 1);
177                    this.resize();
178                }
179            }, this));
180    };
181
182    /* PUBLIC METHODS
183     * ========================= */
184    Backstretch.prototype = {
185        resize: function() {
186            try {
187                var bgCSS = {
188                    left: 0,
189                    top: 0
190                }, rootWidth = this.isBody ? this.$root.width() : this.$root.innerWidth(),
191                    bgWidth = rootWidth,
192                    rootHeight = this.isBody ? (window.innerHeight ? window.innerHeight : this.$root.height()) : this.$root.innerHe
192ight(),
193                    bgHeight = bgWidth / this.$img.data('ratio'),
194                    bgOffset;
195
196                // Make adjustments based on image ratio
197                if (bgHeight >= rootHeight) {
198                    bgOffset = (bgHeight - rootHeight) / 2;
199                    if (this.options.centeredY) {
200                        bgCSS.top = '-' + bgOffset + 'px';
201                    }
202                } else {
203                    bgHeight = rootHeight;
204                    bgWidth = bgHeight * this.$img.data('ratio');
205                    bgOffset = (bgWidth - rootWidth) / 2;
206                    if (this.options.centeredX) {
207                        bgCSS.left = '-' + bgOffset + 'px';
208                    }
209                }
210
211                this.$wrap.css({
212                    width: rootWidth,
213                    height: rootHeight
214                })
215                    .find('img:not(.deleteable)').css({
216                        width: bgWidth,
217                        height: bgHeight
218                    }).css(bgCSS);
219            } catch (err) {
220                // IE7 seems to trigger resize before the image is loaded.
221                // This try/catch block is a hack to let it fail gracefully.
222            }
223
224            return this;
225        }
226
227        // Show the slide at a certain position
228        ,
229        show: function(newIndex) {
230
231            // Validate index
232            if (Math.abs(newIndex) > this.images.length - 1) {
233                return;
234            }
235
236            // Vars
237            var self = this,
238                oldImage = self.$wrap.find('img').addClass('deleteable'),
239                evtOptions = {
240                    relatedTarget: self.$container[0]
241                };
242
243            // Trigger the "before" event
244            self.$container.trigger($.Event('backstretch.before', evtOptions), [self, newIndex]);
245
246            // Set the new index
247            this.index = newIndex;
248
249            // Pause the slideshow
250            clearInterval(self.interval);
251
252            // New image
253            self.$img = $('<img />')
254                .css(styles.img)
255                .bind('load', function(e) {
256                    var imgWidth = this.width || $(e.target).width(),
257                        imgHeight = this.height || $(e.target).height();
258
259                    // Save the ratio
260                    $(this).data('ratio', imgWidth / imgHeight);
261
262                    // Show the image, then delete the old one
263                    // "speed" option has been deprecated, but we want backwards compatibilty
264                    $(this).fadeIn(self.options.speed || self.options.fade, function() {
265                        oldImage.remove();
266
267                        // Resume the slideshow
268                        if (!self.paused) {
269                            self.cycle();
270                        }
271
272                        // Trigger the "after" and "show" events
273                        // "show" is being deprecated
274                        $(['after', 'show']).each(function() {
275                            self.$container.trigger($.Event('backstretch.' + this, evtOptions), [self, newIndex]);
276                        });
277                    });
278
279                    // Resize
280                    self.resize();
281                })
282                .appendTo(self.$wrap);
283
284            // Hack for IE img onload event
285            self.$img.attr('src', self.images[newIndex]);
286            return self;
287        }
288
289        ,
290        next: function() {
291            // Next slide
292            return this.show(this.index < this.images.length - 1 ? this.index + 1 : 0);
293        }
294
295        ,
296        prev: function() {
297            // Previous slide
298            return this.show(this.index === 0 ? this.images.length - 1 : this.index - 1);
299        }
300
301        ,
302        pause: function() {
303            // Pause the slideshow
304            this.paused = true;
305            return this;
306        }
307
308        ,
309        resume: function() {
310            // Resume the slideshow
311            this.paused = false;
312            this.next();
313            return this;
314        }
315
316        ,
317        cycle: function() {
318            // Start/resume the slideshow
319            if (this.images.length > 1) {
320                // Clear the interval, just in case
321                clearInterval(this.interval);
322
323                this.interval = setInterval($.proxy(function() {
324                    // Check for paused slideshow
325                    if (!this.paused) {
326                        this.next();
327                    }
328                }, this), this.options.duration);
329            }
330            return this;
331        }
332
333        ,
vendor: 2,624 bytes, lines 334-401
334        destroy: function(preserveBackground) {
335            // Stop the resize events
336            $(window).off('resize.backstretch orientationchange.backstretch');
337
338            // Clear the interval
339            clearInterval(this.interval);
340
341            // Remove Backstretch
342            if (!preserveBackground) {
343                this.$wrap.remove();
344            }
345            this.$container.removeData('backstretch');
346        }
347    };
348
349    /* SUPPORTS FIXED POSITION?
350     *
351     * Based on code from jQuery Mobile 1.1.0
352     * http://jquerymobile.com/
353     *
354     * In a nutshell, we need to figure out if fixed positioning is supported.
355     * Unfortunately, this is very difficult to do on iOS, and usually involves
356     * injecting content, scrolling the page, etc.. It's ugly.
357     * jQuery Mobile uses this workaround. It's not ideal, but works.
358     *
359     * Modified to detect IE6
360     * ========================= */
361
362    var supportsFixedPosition = (function() {
363        var ua = navigator.userAgent,
364            platform = navigator.platform
365            // Rendering engine is Webkit, and capture major version
366            ,
367            wkmatch = ua.match(/AppleWebKit\/([0-9]+)/),
368            wkversion = !! wkmatch && wkmatch[1],
369            ffmatch = ua.match(/Fennec\/([0-9]+)/),
370            ffversion = !! ffmatch && ffmatch[1],
371            operammobilematch = ua.match(/Opera Mobi\/([0-9]+)/),
372            omversion = !! operammobilematch && operammobilematch[1],
373            iematch = ua.match(/MSIE ([0-9]+)/),
374            ieversion = !! iematch && iematch[1];
375
376        return !(
377            // iOS 4.3 and older : Platform is iPhone/Pad/Touch and Webkit version is less than 534 (ios5)
378            ((platform.indexOf("iPhone") > -1 || platform.indexOf("iPad") > -1 || platform.indexOf("iPod") > -1) && wkversion && wkversion < 534) ||
379
380            // Opera Mini
381            (window.operamini && ({}).toString.call(window.operamini) === "[object OperaMini]") ||
382            (operammobilematch && omversion < 7458) ||
383
384            //Android lte 2.1: Platform is Android and Webkit version is less than 533 (Android 2.2)
385            (ua.indexOf("Android") > -1 && wkversion && wkversion < 533) ||
386
387            // Firefox Mobile before 6.0 -
388            (ffversion && ffversion < 6) ||
389
390            // WebOS less than 3
391            ("palmGetResource" in window && wkversion && wkversion < 534) ||
392
393            // MeeGo
394            (ua.indexOf("MeeGo") > -1 && ua.indexOf("NokiaBrowser/8.5.0") > -1) ||
395
396            // IE6
397            (ieversion && ieversion <= 6)
398        );
399    }());
400
401}(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.