Skip to content

Commit 6664ce6

Browse files
committed
implement position option and update documentation accordingly
1 parent b3ce71c commit 6664ce6

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

Diff for: README.md

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ Note that when specifying these options as html data-attributes, you should conv
6969
<td>number</td>
7070
<td>auto</td>
7171
</tr>
72+
<tr>
73+
<td>position</td>
74+
<td>xPos yPos</td>
75+
<td>center center</td>
76+
<td rowspan="3">This is analogous to the background-position css property. Specify coordinates as top, bottom, right, left, center, or pixel values (i.e. -10px 0px). The parallax image will be positioned as close to these values as possible while still covering the target element.</td>
77+
</tr>
78+
<tr>
79+
<td>positionX</td>
80+
<td>xPos</td>
81+
<td>center</td>
82+
</tr>
83+
<tr>
84+
<td>positionY</td>
85+
<td>yPos</td>
86+
<td>center</td>
87+
</tr>
7288
<tr>
7389
<td>speed</td>
7490
<td>float</td>

Diff for: parallax.js

+73-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* parallax.js v1.0.1 (http://pixelcog.github.io/parallax.js/)
2+
* parallax.js v1.1 (http://pixelcog.github.io/parallax.js/)
33
* Copyright (c) 2014 PixelCog, Inc.
44
* Licensed under MIT (https://github.com/pixelcog/parallax.js/blob/master/LICENSE)
55
*/
@@ -55,12 +55,53 @@
5555
this.imageSrc = this.$element.attr('src');
5656
}
5757

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+
5899
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
59100
if (this.iosFix && !this.$element.is('img')) {
60101
this.$element.css({
61102
backgroundImage: 'url(' + encodeURIComponent(this.imageSrc) + ')',
62103
backgroundSize: 'cover',
63-
backgroundPosition: 'center'
104+
backgroundPosition: this.position
64105
});
65106
}
66107
return this;
@@ -103,10 +144,11 @@
103144
// Parallax Instance Methods
104145

105146
$.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',
110152

111153
refresh: function() {
112154
this.boxWidth = this.$element.width();
@@ -115,19 +157,42 @@
115157
this.boxOffsetLeft = this.$element.offset().left;
116158
this.boxOffsetBottom = this.boxOffsetTop + this.boxHeight;
117159

160+
var margin = 0;
118161
var winHeight = Parallax.winHeight;
119162
var imageHeightMin = winHeight - (winHeight - this.boxHeight) * this.speed | 0;
120163

121164
if (imageHeightMin * this.aspectRatio >= this.boxWidth) {
122165
this.imageWidth = imageHeightMin * this.aspectRatio | 0;
123166
this.imageHeight = imageHeightMin;
124-
this.offsetLeft = (this.boxWidth - this.imageWidth) / 2 | 0;
125167
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+
}
126180
} else {
127181
this.imageWidth = this.boxWidth;
128182
this.imageHeight = this.boxWidth / this.aspectRatio | 0;
129183
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+
}
131196
}
132197
},
133198

Diff for: parallax.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)