Skip to content

Commit 268632f

Browse files
committed
enabling complex html as background slider
This appends a whole DOM element to the parallax mirror (utilizing the same ".parallax-slider" class). so far it only works properly when setting the naturalWidth and naturalHeight options in the JS initialization. Somebody got a tip on how to improve that? I'm planning to implement a parallax carousel which should be possible with that change. also performance improvement suggestions from #71
1 parent 6f9b3b3 commit 268632f

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

parallax.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@
114114
}
115115

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

119126
this.$mirror.addClass('parallax-mirror').css({
120127
visibility: 'hidden',
@@ -140,7 +147,7 @@
140147

141148
this.$slider[0].src = this.imageSrc;
142149

143-
if (this.naturalHeight && this.naturalWidth || this.$slider[0].complete) {
150+
if (this.naturalHeight && this.naturalWidth || this.$slider[0].complete || slider.length > 0) {
144151
this.$slider.trigger('load');
145152
}
146153

@@ -263,22 +270,34 @@
263270

264271
var $doc = $(document), $win = $(window);
265272

273+
var loadDimensions = function() {
274+
Parallax.winHeight = $win.height();
275+
Parallax.winWidth = $win.width();
276+
Parallax.docHeight = $doc.height();
277+
Parallax.docWidth = $doc.width();
278+
};
279+
280+
var loadScrollPosition = function() {
281+
var winScrollTop = $win.scrollTop();
282+
var scrollTopMax = Parallax.docHeight - Parallax.winHeight;
283+
var scrollLeftMax = Parallax.docWidth - Parallax.winWidth;
284+
Parallax.scrollTop = Math.max(0, Math.min(scrollTopMax, winScrollTop));
285+
Parallax.scrollLeft = Math.max(0, Math.min(scrollLeftMax, $win.scrollLeft()));
286+
Parallax.overScroll = Math.max($win.scrollTop() - scrollTopMax, Math.min(winScrollTop, 0));
287+
};
288+
266289
$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();
271-
Parallax.isFresh = false;
272-
Parallax.requestRender();
273-
})
274-
.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));
280-
Parallax.requestRender();
281-
});
290+
loadDimensions();
291+
Parallax.isFresh = false;
292+
Parallax.requestRender();
293+
})
294+
.on('scroll.px.parallax load.px.parallax', function() {
295+
loadScrollPosition();
296+
Parallax.requestRender();
297+
});
298+
299+
loadDimensions();
300+
loadScrollPosition();
282301

283302
this.isReady = true;
284303
},

0 commit comments

Comments
 (0)