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(xpos, baseSpeedFactor, outerHeight){ 21 var $this = $(this), 22 speedFactor, 23 offsetFactor = 0, 24 getHeight, 25 topOffset = 0, 26 containerHeight = 0, 27 containerWidth = 0, 28 // Disable parallax on certain screen/img ratios 29 disableParallax = 0, 30 // Base image width and height (if can be achieved) 31 baseImgHeight = 0, 32 baseImgWidth = 0, 33 // Backgroud-size cover? and current image size (counted) 34 isBgCover = ($this.css('background-size') == 'cover'), 35 curImgHeight = 0; 36 37 if ($this.length == 0) return; 38 39 // setup defaults if arguments aren't specified 40 if (arguments.length < 1 || xpos === null) xpos = "50%"; 41 if (arguments.length < 2 || baseSpeedFactor === null) baseSpeedFactor = 0.61; 42 if (arguments.length < 3 || outerHeight === null) outerHeight = true; 43 44 if (outerHeight){ 45 getHeight = function(jqo){ 46 return jqo.outerHeight(true); 47 }; 48 } else { 49 getHeight = function(jqo){ 50 return jqo.height(); 51 }; 52 } 53 54 // Count background image size 55 function getBackgroundSize(callback){ 56 var img = new Image(), 57 // here we will place image's width and height 58 width, height, 59 // here we get the size of the background and split it to array 60 backgroundSize = ($this.css('background-size') || ' ').split(' '); 61 62 // checking if width was set to pixel value 63 if (/px/.test(backgroundSize[0])) width = parseInt(backgroundSize[0]); 64 // checking if width was set to percent value 65 if (/%/.test(backgroundSize[0])) width = $this.parent().width() * (parseInt(backgroundSize[0]) / 100); 66 // checking if height was set to pixel value 67 if (/px/.test(backgroundSize[1])) height = parseInt(backgroundSize[1]); 68 // checking if height was set to percent value 69 if (/%/.test(backgroundSize[1])) height = $this.parent().height() * (parseInt(backgroundSize[0]) / 100); 70 71 if (typeof width != 'undefined' && typeof height != 'undefined'){ 72 // Image is not needed 73 return callback({ width: width, height: height }); 74 } 75 76 // Image is needed 77 img.onload = function () { 78 // check if width was set earlier, if not then set it now 79 if (typeof width == 'undefined') width = this.width; 80 // do the same with height 81 if (typeof height == 'undefined') height = this.height; 82 // call the callback 83 callback({ width: width, height: height }); 84 }; 85 // extract image source from css using one, simple regex 86 // src should be set AFTER onload handler 87 img.src = ($this.css('background-image') || '').replace(/url\(['"]*(.*?)['"]*\)/g, '$1'); 88 } 89 function update(){ 90 if (disableParallax) 91 return $this.css('backgroundPosition', ''); 92 if (isNaN(speedFactor)) 93 return; 94 95 var pos = $window.scrollTop(); 96 topOffset = $this.offset().top; 97 // Check if totally above or totally below viewport 98 if ((pos < topOffset - windowHeight) || (topOffset + containerHeight < pos)) return; 99 $this.css('backgroundPosition', xpos + " " + (offsetFactor + speedFactor * (topOffset - pos)) + "px"); 100 } 101 function resize(){ 102 setTimeout(function(){ 103 windowHeight = $window.height(); 104 containerHeight = getHeight($this); 105 containerWidth = $this.width(); 106 if (isBgCover){ 107 if (baseImgWidth / baseImgHeight <= containerWidth / containerHeight){ 108 // Resizing by width 109 curImgHeight = baseImgHeight * ($this.width() / baseImgWidth); 110 disableParallax = 0; 111 } 112 else { 113 disableParallax = 1; 114 } 115 } 116 // Improving speed factor to prevent showing image limits 117 if (curImgHeight !== 0){ 118 if (baseSpeedFactor >= 0) { 119 speedFactor = Math.min(baseSpeedFactor, curImgHeight / windowHeight); 120 offsetFactor = Math.min(0, .5 * (windowHeight - curImgHeight - speedFactor * (windowHeight - containerHeight))); 121 } else { 122 speedFactor = Math.max(baseSpeedFactor, -(curImgHeight / windowHeight)); 123 offsetFactor = Math.min(0, 0.5 * (-windowHeight + curImgHeight + speedFactor * (-windowHeight + containerHeight))); 124 } 125 126 127 }else{ 128 speedFactor = baseSpeedFactor; 129 offsetFactor = 0; 130 } 131 update(); 132 }, 10); 133 } 134
135 getBackgroundSize(function(sz){ 136 curImgHeight = baseImgHeight = sz.height; 137 baseImgWidth = sz.width; 138 resize(); 139 }); 140 141 $window.bind({scroll: update, load: resize, resize: resize}); 142 resize(); 143 }; 144})(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.