No problem, this is what’s happening:
$('#wrapper').data('dir', 1); //Set the direction to move the background in
$('#wrapper').data('orig_height', $('#wrapper').height()); //Remember the height of this element
Here I’m just storing a variable dir inside the element for easy referencing. You could just the same use a variable like “var dir = 1” if you’d like.
1 is an easy number to Toggle, because you can multiply it by -1 to get the opposite.
1 * -1 = -1
-1 * -1 = 1
The height of the element changes when you slide it up or down, so I’m storing the original height (before it slides) “orig_height” into the elements data “memory”, so I can look it up later.
Pretty easy, just using $().data(var) to store variables.
Next:
var dir = $('#wrapper').data('dir'); //The direction to push the background
var height = $('#wrapper').data('orig_height');
$('#wrapper').data('dir', (dir*-1)); //"toggle" the direction
All I’m doing here is taking the values we just stored out from the element into local variables “dir” and “height”.
I toggle “dir” by multiplying it by -1…so it’s now the opposite of whatever it just was. Then I store this back into the element for future use.
$('#wrapper').slideToggle('slow', 'linear');
$('body').animate({'background-position-y': (1+dir)*height/2+'px'}, 'slow', 'linear'); //Animate the background
Here I have two animations going at the SAME time at the SAME speed with the SAME easing. In order to make it work, we need to move the backdground down X pixels, where X is the height of the #wrapper.
This part looks confusing:
(1+dir)*height/2+‘px’
It’s just some fancy math tricks. (1+dir) will ALWAYS equal either 0 or 2, depending on if dir is -1 or 1.
If height = 100 then this means that
(0 or 2) * height = 0 or 200
200 is double the height of #wrapper, so we divide it by 2. We add ‘px’ to the end so the string becomes “100px”