Skip to content

Commit e2ddf13

Browse files
committed
update parallax.js to v1.1 and add position option to the documentation
1 parent a639a2e commit e2ddf13

File tree

4 files changed

+99
-18
lines changed

4 files changed

+99
-18
lines changed

img/arrows.png

34.6 KB
Loading

index.html

+18-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
<div id="ios-notice" class="alert alert-info hidden"><p><strong>Notice:</strong> Hello iOS user. Unfortunately, this parallax effect cannot be achieved on iOS devices, so all you will see on this demo page are static backgrounds. Please visit this page on a desktop browser to see its full effect.</p></div>
3636
<h1>Simple Parallax Scrolling <small>parallax.js</small></h1>
3737
<p>Parallax.js is a dirt simple parallax scrolling effect inspired by <a href="http://spotify.com">Spotify.com</a> and implemented as a jQuery plugin.</p>
38-
<a href="https://github.com/pixelcog/parallax.js/archive/v1.0.zip" class="btn btn-default download-link">Download Parallax.js v1.0</a>
38+
<a href="https://github.com/pixelcog/parallax.js/archive/v1.1.zip" class="btn btn-default download-link">Download Parallax.js v1.1</a>
3939
<a href="https://github.com/pixelcog/parallax.js/" class="btn btn-default">View on GitHub</a>
4040
</div>
4141
</section>
4242

43-
<div class="parallax-container" data-parallax="scroll" data-bleed="10" data-image-src="./img/stellar-spire-eagle-nebula-1400x900.jpg" data-natural-width="1400" data-natural-height="900"></div>
43+
<div class="parallax-container" data-parallax="scroll" data-position="top" data-bleed="10" data-image-src="./img/stellar-spire-eagle-nebula-1400x900.jpg" data-natural-width="1400" data-natural-height="900"></div>
4444

4545
<section>
4646
<div class="container">
@@ -118,6 +118,22 @@ <h2 id="options">Options</h2>
118118
<td>number</td>
119119
<td>auto</td>
120120
</tr>
121+
<tr>
122+
<td>position</td>
123+
<td>xPos yPos</td>
124+
<td>center center</td>
125+
<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>
126+
</tr>
127+
<tr>
128+
<td>positionX</td>
129+
<td>xPos</td>
130+
<td>center</td>
131+
</tr>
132+
<tr>
133+
<td>positionY</td>
134+
<td>yPos</td>
135+
<td>center</td>
136+
</tr>
121137
<tr>
122138
<td>speed</td>
123139
<td>float</td>

js/parallax.js

+79-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* parallax.js v1.0 (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

@@ -169,12 +234,12 @@
169234
// Parallax Static Methods
170235

171236
$.extend(Parallax, {
172-
scrollTop: $window.scrollTop(),
173-
scrollLeft: $window.scrollLeft(),
237+
scrollTop: 0,
238+
scrollLeft: 0,
174239
winHeight: 0,
175240
winWidth: 0,
176-
docHeight: 0,
177-
docWidth: 0,
241+
docHeight: 1 << 30,
242+
docWidth: 1 << 30,
178243
sliders: [],
179244
isReady: false,
180245
isFresh: false,
@@ -184,14 +249,14 @@
184249
if (this.isReady) return;
185250

186251
$window
187-
.on('scroll.px.parallax load.px.parallax', function(){
252+
.on('scroll.px.parallax load.px.parallax', function() {
188253
var scrollTopMax = Parallax.docHeight - Parallax.winHeight;
189254
var scrollLeftMax = Parallax.docWidth - Parallax.winWidth;
190255
Parallax.scrollTop = Math.max(0, Math.min(scrollTopMax, $window.scrollTop()));
191256
Parallax.scrollLeft = Math.max(0, Math.min(scrollLeftMax, $window.scrollLeft()));
192257
Parallax.requestRender();
193258
})
194-
.on('resize.px.parallax load.px.parallax', function(){
259+
.on('resize.px.parallax load.px.parallax', function() {
195260
Parallax.winHeight = $window.height();
196261
Parallax.winWidth = $window.width();
197262
Parallax.docHeight = $(document).height();

0 commit comments

Comments
 (0)