Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #2 from pixshatterer/master

refactor to enable better performance
  • Loading branch information...
commit cde7495fa04d227080668db85f0b821a1ac2ae64 2 parents 484ba0b + 76bfc4f
Dave Cranwell authored

Showing 1 changed file with 53 additions and 32 deletions. Show diff stats Hide diff stats

  1. +53 32 jquery.scrollParallax.js
85 jquery.scrollParallax.js
@@ -83,8 +83,14 @@
83 83
84 84 return this.each(function() {
85 85 //defined accessible $this var in standard way for use within functions
86   - var $this = $(this);
87   -
  86 + var $this = $(this),
  87 + //timestamp,last position for the interval & FPS ~= 30
  88 + ts,FPS = 34,lastPos = {x:0,y:0},intervalActive,
  89 + //find current position so parallax can be relative to it
  90 + currentPosArray=$this.css("backgroundPosition").split(" "),
  91 + currentXPos=parseInt(currentPosArray[0].replace(/[^0-9\-]/g, "")),
  92 + currentYPos=parseInt(currentPosArray[1].replace(/[^0-9\-]/g, ""));
  93 +
88 94 //extend options in standard way
89 95 if (options) {
90 96 $.extend(settings, options);
@@ -99,37 +105,52 @@
99 105 debug("out of view");
100 106 }
101 107 });
102   -
103   - //find current position so parallax can be relative to it
104   - var currentPosArray=$this.css("backgroundPosition").split(" ");
105   - var currentXPos=parseInt(currentPosArray[0].replace(/[^0-9\-]/g, ""));
106   - var currentYPos=parseInt(currentPosArray[1].replace(/[^0-9\-]/g, ""));
107   -
108   - //recalculate position on scroll
109   - $(window).bind('scroll', function(){
110   - if($this.hasClass("inview")){
111   - var offset = $this.offset();
  108 +
  109 + function updateElemPos(){
  110 + var newXPos,newYPos,
  111 + offset = $this.offset();
112 112
113   - //calculate new position
114   - if(settings.axis.match(/x/)){
115   - var Xpos = offset.left - $(window).scrollLeft();
116   - var newXPos = (-(Xpos) * settings.speed) + currentXPos;
117   - }else{
118   - var newXPos = currentXPos;
119   - }
120   - if(settings.axis.match(/y/)){
121   - var Ypos = offset.top - $(window).scrollTop();
122   - var newYPos = (-(Ypos) * settings.speed) + currentYPos;
123   - }else{
124   - var newYPos = currentYPos;
125   - }
126   -
127   - debug("new X position: "+ newXPos);
128   - debug("new Y position: "+ newYPos);
129   -
130   - $this.css({'backgroundPosition': parseInt(newXPos) + "px " + parseInt(newYPos) +"px"});
131   - }
132   - });
  113 + //calculate new position
  114 + newXPos = (settings.axis.match(/x/))?
  115 + parseInt((-(offset.left - $(window).scrollLeft()) * settings.speed) + currentXPos) + "px":
  116 + currentPosArray[0];
  117 +
  118 + newYPos = (settings.axis.match(/y/))?
  119 + parseInt((-(offset.top - $(window).scrollTop()) * settings.speed) + currentYPos) + "px":
  120 + currentPosArray[1];
  121 +
  122 + if(typeof intervalActive == 'undefined'){
  123 + lastPos.x = newXPos;
  124 + lastPos.y = newYPos;
  125 + }
  126 +
  127 + intervalActive = true;
  128 +
  129 + debug("new X position: "+ newXPos);
  130 + debug("new Y position: "+ newYPos);
  131 +
  132 + if((newXPos !== lastPos.x) || (newYPos !== lastPos.y)){
  133 + $this.css({'backgroundPosition': newXPos + " " + newYPos});
  134 + lastPos.x = newXPos;
  135 + lastPos.y = newYPos;
  136 + }else{
  137 + clearInterval(ts);
  138 + intervalActive = false;
  139 + $(window).bind('scroll.parallax',onScroll);
  140 + }
  141 + }
  142 +
  143 + function onScroll(){
  144 + if($this.hasClass("inview")){
  145 + if(!intervalActive){
  146 + ts = setInterval(updateElemPos,FPS);
  147 + $(window).unbind('scroll.parallax');
  148 + }
  149 + }
  150 + }
  151 +
  152 + //recalculate position on scroll
  153 + $(window).bind('scroll.parallax',onScroll);
133 154 });
134 155 };
135 156

0 comments on commit cde7495

Please sign in to comment.
Something went wrong with that request. Please try again.