Skip to content

Commit d9cd67d

Browse files
committed
Merge pull request #88 from wstoettinger/master
enabling complex html as background slider
2 parents 6f9b3b3 + 2199ee0 commit d9cd67d

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

parallax.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
1616
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
1717
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
18-
|| window[vendors[x]+'CancelRequestAnimationFrame'];
18+
|| window[vendors[x]+'CancelRequestAnimationFrame'];
1919
}
2020

2121
if (!window.requestAnimationFrame)
@@ -92,7 +92,7 @@
9292
this.positionY + (isNaN(this.positionY)? '' : 'px');
9393

9494
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
95-
if (this.iosFix && !this.$element.is('img')) {
95+
if (this.imageSrc && this.iosFix && !this.$element.is('img')) {
9696
this.$element.css({
9797
backgroundImage: 'url(' + this.imageSrc + ')',
9898
backgroundSize: 'cover',
@@ -103,7 +103,7 @@
103103
}
104104

105105
if (navigator.userAgent.match(/(Android)/)) {
106-
if (this.androidFix && !this.$element.is('img')) {
106+
if (this.imageSrc && this.androidFix && !this.$element.is('img')) {
107107
this.$element.css({
108108
backgroundImage: 'url(' + this.imageSrc + ')',
109109
backgroundSize: 'cover',
@@ -114,7 +114,16 @@
114114
}
115115

116116
this.$mirror = $('<div />').prependTo('body');
117-
this.$slider = $('<img />').prependTo(this.$mirror);
117+
118+
var slider = this.$element.find('>.parallax-slider');
119+
var sliderExisted = false;
120+
121+
if (slider.length == 0)
122+
this.$slider = $('<img />').prependTo(this.$mirror);
123+
else {
124+
this.$slider = slider.prependTo(this.$mirror)
125+
sliderExisted = true;
126+
}
118127

119128
this.$mirror.addClass('parallax-mirror').css({
120129
visibility: 'hidden',
@@ -138,9 +147,10 @@
138147
Parallax.requestRender();
139148
});
140149

141-
this.$slider[0].src = this.imageSrc;
150+
if (!sliderExisted)
151+
this.$slider[0].src = this.imageSrc;
142152

143-
if (this.naturalHeight && this.naturalWidth || this.$slider[0].complete) {
153+
if (this.naturalHeight && this.naturalWidth || this.$slider[0].complete || slider.length > 0) {
144154
this.$slider.trigger('load');
145155
}
146156

@@ -263,23 +273,35 @@
263273

264274
var $doc = $(document), $win = $(window);
265275

276+
var loadDimensions = function() {
277+
Parallax.winHeight = $win.height();
278+
Parallax.winWidth = $win.width();
279+
Parallax.docHeight = $doc.height();
280+
Parallax.docWidth = $doc.width();
281+
};
282+
283+
var loadScrollPosition = function() {
284+
var winScrollTop = $win.scrollTop();
285+
var scrollTopMax = Parallax.docHeight - Parallax.winHeight;
286+
var scrollLeftMax = Parallax.docWidth - Parallax.winWidth;
287+
Parallax.scrollTop = Math.max(0, Math.min(scrollTopMax, winScrollTop));
288+
Parallax.scrollLeft = Math.max(0, Math.min(scrollLeftMax, $win.scrollLeft()));
289+
Parallax.overScroll = Math.max($win.scrollTop() - scrollTopMax, Math.min(winScrollTop, 0));
290+
};
291+
266292
$win.on('resize.px.parallax load.px.parallax', function() {
267-
Parallax.winHeight = $win.height();
268-
Parallax.winWidth = $win.width();
269-
Parallax.docHeight = $doc.height();
270-
Parallax.docWidth = $doc.width();
293+
loadDimensions();
271294
Parallax.isFresh = false;
272295
Parallax.requestRender();
273296
})
274297
.on('scroll.px.parallax load.px.parallax', function() {
275-
var scrollTopMax = Parallax.docHeight - Parallax.winHeight;
276-
var scrollLeftMax = Parallax.docWidth - Parallax.winWidth;
277-
Parallax.scrollTop = Math.max(0, Math.min(scrollTopMax, $win.scrollTop()));
278-
Parallax.scrollLeft = Math.max(0, Math.min(scrollLeftMax, $win.scrollLeft()));
279-
Parallax.overScroll = Math.max($win.scrollTop() - scrollTopMax, Math.min($win.scrollTop(), 0));
298+
loadScrollPosition();
280299
Parallax.requestRender();
281300
});
282301

302+
loadDimensions();
303+
loadScrollPosition();
304+
283305
this.isReady = true;
284306
},
285307

parallax.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)