|
1 | 1 | /*! |
2 | | - * parallax.js v1.0.1 (http://pixelcog.github.io/parallax.js/) |
| 2 | + * parallax.js v1.1 (http://pixelcog.github.io/parallax.js/) |
3 | 3 | * Copyright (c) 2014 PixelCog, Inc. |
4 | 4 | * Licensed under MIT (https://github.com/pixelcog/parallax.js/blob/master/LICENSE) |
5 | 5 | */ |
|
55 | 55 | this.imageSrc = this.$element.attr('src'); |
56 | 56 | } |
57 | 57 |
|
| 58 | + var positions = (this.position + '').toLowerCase().match(/\S+/g) || []; |
| 59 | + |
| 60 | + if (positions.length < 1) { |
| 61 | + positions.push('center'); |
| 62 | + } |
| 63 | + if (positions.length == 1) { |
| 64 | + positions.push(positions[0]); |
| 65 | + } |
| 66 | + |
| 67 | + if (positions[0] == 'top' || positions[0] == 'bottom' || |
| 68 | + positions[1] == 'left' || positions[1] == 'right') { |
| 69 | + self.positionX = positions[1]; |
| 70 | + self.positionY = positions[0]; |
| 71 | + } else { |
| 72 | + self.positionX = positions[0]; |
| 73 | + self.positionY = positions[1]; |
| 74 | + } |
| 75 | + |
| 76 | + if (this.positionX != undefined) positions[0] = this.positionX.toLowerCase(); |
| 77 | + if (this.positionY != undefined) positions[1] = this.positionY.toLowerCase(); |
| 78 | + |
| 79 | + if (this.positionX != 'left' && this.positionX != 'right') { |
| 80 | + if (isNaN(parseInt(this.positionX))) { |
| 81 | + this.positionX = 'center'; |
| 82 | + } else { |
| 83 | + this.positionX = parseInt(this.positionX); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (this.positionY != 'top' && this.positionY != 'bottom') { |
| 88 | + if (isNaN(parseInt(this.positionY))) { |
| 89 | + this.positionY = 'center'; |
| 90 | + } else { |
| 91 | + this.positionY = parseInt(this.positionY); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + this.position = |
| 96 | + this.positionX + (isNaN(this.positionX)? '' : 'px') + ' ' + |
| 97 | + this.positionY + (isNaN(this.positionY)? '' : 'px'); |
| 98 | + |
58 | 99 | if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) { |
59 | 100 | if (this.iosFix && !this.$element.is('img')) { |
60 | 101 | this.$element.css({ |
61 | 102 | backgroundImage: 'url(' + encodeURIComponent(this.imageSrc) + ')', |
62 | 103 | backgroundSize: 'cover', |
63 | | - backgroundPosition: 'center' |
| 104 | + backgroundPosition: this.position |
64 | 105 | }); |
65 | 106 | } |
66 | 107 | return this; |
|
103 | 144 | // Parallax Instance Methods |
104 | 145 |
|
105 | 146 | $.extend(Parallax.prototype, { |
106 | | - speed: 0.2, |
107 | | - bleed: 0, |
108 | | - zIndex: -100, |
109 | | - iosFix: true, |
| 147 | + speed: 0.2, |
| 148 | + bleed: 0, |
| 149 | + zIndex: -100, |
| 150 | + iosFix: true, |
| 151 | + position: 'center', |
110 | 152 |
|
111 | 153 | refresh: function() { |
112 | 154 | this.boxWidth = this.$element.width(); |
|
115 | 157 | this.boxOffsetLeft = this.$element.offset().left; |
116 | 158 | this.boxOffsetBottom = this.boxOffsetTop + this.boxHeight; |
117 | 159 |
|
| 160 | + var margin = 0; |
118 | 161 | var winHeight = Parallax.winHeight; |
119 | 162 | var imageHeightMin = winHeight - (winHeight - this.boxHeight) * this.speed | 0; |
120 | 163 |
|
121 | 164 | if (imageHeightMin * this.aspectRatio >= this.boxWidth) { |
122 | 165 | this.imageWidth = imageHeightMin * this.aspectRatio | 0; |
123 | 166 | this.imageHeight = imageHeightMin; |
124 | | - this.offsetLeft = (this.boxWidth - this.imageWidth) / 2 | 0; |
125 | 167 | this.offsetBaseTop = 0; |
| 168 | + |
| 169 | + margin = this.imageWidth - this.boxWidth; |
| 170 | + |
| 171 | + if (this.positionX == 'left') { |
| 172 | + this.offsetLeft = 0; |
| 173 | + } else if (this.positionX == 'right') { |
| 174 | + this.offsetLeft = - margin; |
| 175 | + } else if (!isNaN(this.positionX)) { |
| 176 | + this.offsetLeft = Math.max(this.positionX, - margin); |
| 177 | + } else { |
| 178 | + this.offsetLeft = - margin / 2 | 0; |
| 179 | + } |
126 | 180 | } else { |
127 | 181 | this.imageWidth = this.boxWidth; |
128 | 182 | this.imageHeight = this.boxWidth / this.aspectRatio | 0; |
129 | 183 | this.offsetLeft = 0; |
130 | | - this.offsetBaseTop = (imageHeightMin - this.imageHeight) / 2 | 0; |
| 184 | + |
| 185 | + margin = this.imageHeight - imageHeightMin; |
| 186 | + |
| 187 | + if (this.positionY == 'top') { |
| 188 | + this.offsetBaseTop = 0; |
| 189 | + } else if (this.positionY == 'bottom') { |
| 190 | + this.offsetBaseTop = - margin; |
| 191 | + } else if (!isNaN(this.positionY)) { |
| 192 | + this.offsetBaseTop = Math.max(this.positionY, - margin); |
| 193 | + } else { |
| 194 | + this.offsetBaseTop = - margin / 2 | 0; |
| 195 | + } |
131 | 196 | } |
132 | 197 | }, |
133 | 198 |
|
|
0 commit comments