1/* 2 Plugin: jQuery Parallax 3 Version 1.1.3 4 Author: Ian Lunn 5 Twitter: @IanLunn 6 Author URL: http://www.ianlunn.co.uk/ 7 Plugin URL: http://www.ianlunn.co.uk/plugins/jquery-parallax/ 8 9 (Contains lots of UpSolution's modifications) 10 11 Dual licensed under the MIT and GPL licenses: 12 http://www.opensource.org/licenses/mit-license.php 13 http://www.gnu.org/licenses/gpl.html 14 */ 15 16( function( $ ) { 17 var $window = $( window ), 18 windowHeight = $window.height(); 19 20 $.fn.parallax = function( xposParam ) { 21 this.each( function() { 22 var $container = $( this ), 23 $this = $container.children( '.l-section-img' ), 24 speedFactor, 25 offsetFactor = 0, 26 getHeight, 27 topOffset = 0, 28 containerHeight = 0, 29 containerWidth = 0, 30 // Disable parallax on certain screen/img ratios 31 disableParallax = false, 32 parallaxIsDisabled = false, 33 // Base image width and height (if can be achieved) 34 baseImgHeight = 0, 35 baseImgWidth = 0, 36 // Background-size cover? and current image size (counted) 37 isBgCover = ( $this.css( 'background-size' ) == 'cover' ), 38 originalBgPos = $this.css( 'background-position' ), 39 curImgHeight = 0, 40 reversed = $container.hasClass( 'parallaxdir_reversed' ), 41 baseSpeedFactor = reversed ? - 0.1 : 0.61, 42 xpos, 43 outerHeight = true; 44 if ( $this.length == 0 ) { 45 return; 46 } 47 48 // setup defaults if arguments aren't specified 49 if ( xposParam === undefined ) { 50 xpos = "50%"; 51 } else { 52 xpos = xposParam; 53 } 54 55 if ( $container.hasClass( 'parallax_xpos_right' ) ) { 56 xpos = "100%"; 57 } else if ( $container.hasClass( 'parallax_xpos_left' ) ) { 58 xpos = "0%"; 59 } 60 61 if ( outerHeight ) { 62 getHeight = function( jqo ) { 63 return jqo.outerHeight( true ); 64 }; 65 } else { 66 getHeight = function( jqo ) { 67 return jqo.height(); 68 }; 69 } 70 71 // Count background image size 72 function getBackgroundSize( callback ) { 73 var img = new Image(), 74 // here we will place image's width and height 75 width, height, 76 // here we get the size of the background and split it to array 77 backgroundSize = ( $this.css( 'background-size' ) || ' ' ).split( ' ' ), 78 backgroundWidthAttr = $this.attr( 'data-img-width' ), 79 backgroundHeightAttr = $this.attr( 'data-img-height' ); 80 81 if ( backgroundWidthAttr != '' ) { 82 width = parseInt( backgroundWidthAttr ); 83 } 84 if ( backgroundHeightAttr != '' ) { 85 height = parseInt( backgroundHeightAttr ); 86 } 87 88 if ( width !== undefined && height !== undefined ) { 89 // Image is not needed 90 return callback( { width: width, height: height } ); 91 } 92 93 // checking if width was set to pixel value 94 if ( /px/.test( backgroundSize[ 0 ] ) ) { 95 width = parseInt( backgroundSize[ 0 ] ); 96 } 97 // checking if width was set to percent value 98 if ( /%/.test( backgroundSize[ 0 ] ) ) { 99 width = $this.parent().width() * ( parseInt( backgroundSize[ 0 ] ) / 100 ); 100 } 101 // checking if height was set to pixel value 102 if ( /px/.test( backgroundSize[ 1 ] ) ) { 103 height = parseInt( backgroundSize[ 1 ] ); 104 } 105 // checking if height was set to percent value 106 if ( /%/.test( backgroundSize[ 1 ] ) ) { 107 height = $this.parent().height() * ( parseInt( backgroundSize[ 0 ] ) / 100 ); 108 } 109 110 if ( width !== undefined && height !== undefined ) { 111 // Image is not needed 112 return callback( { width: width, height: height } ); 113 } 114 115 // Image is needed 116 img.onload = function() { 117 // check if width was set earlier, if not then set it now 118 if ( typeof width == 'undefined' ) { 119 width = this.width; 120 } 121 // do the same with height 122 if ( typeof height == 'undefined' ) { 123 height = this.height; 124 } 125 // call the callback 126 callback( { width: width, height: height } ); 127 }; 128 // extract image source from css using one, simple regex 129 // src should be set AFTER onload handler 130 img.src = ( $this.css( 'background-image' ) || '' ).replace( /url\(['"]*(.*?)['"]*\)/g, '$1' ); 131 } 132
133 function update() { 134 // Force parallax off for iPad, no correct support for `background-attachment: fixed`; 135 // https://forums.developer.apple.com/forums/thread/99883 136 if( $us.$html.hasClass( 'ios-touch' ) ) { 137 return; 138 } 139 if ( disableParallax ) { 140 if ( ! parallaxIsDisabled ) { 141 $this.css( 'backgroundPosition', originalBgPos ); 142 $container.usMod( 'parallax', 'fixed' ); 143 parallaxIsDisabled = true; 144 } 145 return; 146 } else { 147 if ( parallaxIsDisabled ) { 148 $container.usMod( 'parallax', 'ver' ); 149 parallaxIsDisabled = false; 150 } 151 } 152 if ( isNaN( speedFactor ) ) { 153 return; 154 } 155 156 var pos = $window.scrollTop(); 157 // Check if totally above or totally below viewport 158 if ( ( topOffset + containerHeight < pos ) || ( pos < topOffset - windowHeight ) ) { 159 return; 160 } 161 $this.css( 'backgroundPosition', xpos + " " + ( offsetFactor + speedFactor * ( topOffset - pos ) ) + "px" ); 162 163 } 164 165 function resize() { 166 $ush.timeout( function() { 167 windowHeight = $window.height(); 168 containerHeight = getHeight( $this ); 169 containerWidth = $this.width(); 170 171 172 if ( $window.width() < $us.canvasOptions.disableEffectsWidth ) { 173 disableParallax = true; 174 } else { 175 disableParallax = false; 176 if ( isBgCover ) { 177 if ( baseImgWidth / baseImgHeight <= containerWidth / containerHeight ) { 178 // Resizing by width 179 curImgHeight = baseImgHeight * ( $this.width() / baseImgWidth ); 180 disableParallax = false; 181 } 182 else { 183 disableParallax = true; 184 } 185 } 186 } 187 188 // Improving speed factor to prevent showing image limits 189 if ( curImgHeight !== 0 ) { 190 if ( baseSpeedFactor >= 0 ) { 191 speedFactor = Math.min( baseSpeedFactor, curImgHeight / windowHeight ); 192 offsetFactor = .5 * ( windowHeight - curImgHeight - speedFactor * ( windowHeight - containerHeight ) ); 193 // offsetFactor = Math.min( 0, offsetFactor ); 194 if ( offsetFactor > 0 ) { 195 offsetFactor = -offsetFactor; 196 } 197 } else { 198 speedFactor = Math.min( baseSpeedFactor, ( windowHeight - containerHeight ) / ( windowHeight + containerHeight ) ); 199 offsetFactor = Math.max( 0, speedFactor * containerHeight ); 200 } 201 } else { 202 speedFactor = baseSpeedFactor; 203 offsetFactor = 0; 204 } 205 topOffset = $this.offset().top; 206 update(); 207 }, 10 ); 208 } 209 210 getBackgroundSize( function( sz ) { 211 curImgHeight = baseImgHeight = sz.height; 212 baseImgWidth = sz.width; 213 resize(); 214 } ); 215 216 $window.bind( { 'scroll.noPreventDefault': update, load: resize, resize: resize } ); 217 resize(); 218 } ); 219 }; 220 221 jQuery( '.parallax_ver' ).parallax( '50%' ); 222 223} )( jQuery );
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.