Skip to content

Commit

Permalink
add proceedLoop method to fix and improve the initialization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Signorato committed Mar 12, 2018
1 parent 2cc3dba commit 338df41
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/simpleParallax.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
var pluginName = 'simpleParallax',
edge = 20,
lastPosition = -1,
isInit = false;
isInitialized = false;

function SimpleParallax ( element, options ) {

Expand Down Expand Up @@ -104,10 +104,9 @@

this.occurence.push(plugin);

if (!isInit) {
plugin.proceedElement();
isInit = true;
}
plugin.proceedElement(plugin);

if (isInitialized) plugin.proceedLoop();

},

Expand Down Expand Up @@ -233,13 +232,27 @@

},

proceedElement: function() {
proceedElement: function(elem) {

elem.getElementOffset();

if ( !elem.isVisible() ) return;

var needAnimate = elem.calculate();

if (!needAnimate) return;

elem.animate();

},

proceedLoop: function() {

var plugin = this;

if (lastPosition === window.pageYOffset) {

window.requestAnimationFrame(plugin.proceedElement.bind(plugin));
window.requestAnimationFrame(plugin.proceedLoop.bind(plugin));

return;

Expand All @@ -250,24 +263,16 @@
$.each( this.occurence, function(index) {

if (index === 0) SimpleParallax.getViewportOffset();

this.getElementOffset();

if ( !this.isVisible() ) return;

var needAnimate = this.calculate();

if (!needAnimate) return;

this.animate();
plugin.proceedElement(this.occurence[index]);


});

window.requestAnimationFrame(plugin.proceedElement.bind(plugin));
window.requestAnimationFrame(plugin.proceedLoop.bind(plugin));

}


},

destroy: function() {
Expand All @@ -287,8 +292,10 @@
});

$.fn.simpleParallax = function ( options ) {
this.each(function() {
var length = this.length;
this.each(function(index) {
if ( !$.data( this, pluginName ) ) {
if ((length - 1) == index) isInitialized = true;
$.data( this, pluginName, new SimpleParallax( this, options ) );
}
});
Expand Down

0 comments on commit 338df41

Please sign in to comment.