1/* 2 * Project: Scrolly2 - Background Image Parallax 3 * Originally based on Scrolly by Victor C. / Octave & Octave web agency 4 * Rewritten and heavily adjusted by Benjamin Intal / Gambit 5 */ 6(function ( $, window, document, undefined ) { 7 8 var pluginName = 'scrolly2'; 9 10 function Plugin( element, options ) { 11 this.$element = $(element); 12 this.init(); 13 } 14 15 Plugin.prototype.init = function () { 16 17 // Declare vars 18 var self = this; 19 this.startPosition = 0; 20 this.offsetTop = this.$element.offset().top; 21 this.height = this.$element.outerHeight(true); 22 this.velocity = this.$element.attr('data-velocity'); 23 this.direction = this.$element.attr('data-direction'); 24 25 // Bind so that we don't refresh everytime 26 $(window).bind('scroll', function() { 27 self.scrolly2(); 28 }); 29 }; 30 31 Plugin.prototype.scrolly2 = function() { 32 33 // Check if the element is inside our viewport, if it's not, don't do anything 34 var viewTop = $(window).scrollTop() - 20; // with leeway 35 var viewBottom = $(window).scrollTop() + $(window).height() + 20; // with leeway 36 var elemTop = this.$element.offset().top; 37 var elemBottom = this.$element.offset().top + this.$element.height(); 38 39 if ( elemTop >= viewBottom || elemBottom <= viewTop ) { 40 return; 41 } 42 43 // If the element is below the fold, then we need to 44 // make sure that when we first see the element, 45 // our background image should be in the starting position 46 if ( this.$element.offset().top > $(window).height() ) { 47 if ( this.direction !== 'none' ) { 48 this.startPosition = (this.$element.offset().top - $(window).height()) * Math.abs(this.velocity); 49 } 50 } 51 52 // Calculate position 53 var position = this.startPosition + $(window).scrollTop() * this.velocity; 54 55 // Adjust position 56 var xPos = "50%"; 57 var yPos = "50%"; 58 if ( this.direction === 'left' ) { 59 xPos = position + 'px'; 60 } else if ( this.direction === 'right' ) { 61 xPos = 'calc(100% + ' + -position + 'px)'; 62 } else if ( this.direction === 'down' ) { 63 // yPos = 'calc(100% + ' + (-position) + 'px)'; 64 // Use this one for background-attachment: fixed 65 var offset = - ( $(window).height() - 66 this.$element.offset().top - 67 this.$element.height() - 68 parseInt( this.$element.css('paddingTop') ) - 69 parseInt( this.$element.css('paddingBottom') ) ); 70 yPos = 'calc(100% + ' + ( offset - $(window).scrollTop() - position ) + 'px)'; 71 } else { // up 72 // yPos = position + 'px'; 73 // Use this one for background-attachment: fixed 74 yPos = ( this.$element.offset().top - $(window).scrollTop() + position ) + 'px'; 75 } 76 this.$element.css( { backgroundPosition: xPos + ' ' + yPos } ); 77 }; 78 79 $.fn[pluginName] = function ( options ) { 80 return this.each(function () { 81 if (!$.data(this, 'plugin_' + pluginName)) { 82 $.data(this, 'plugin_' + pluginName, new Plugin( this, options )); 83 } 84 }); 85 }; 86 87})(jQuery, window, document);
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.