For the site I am working on I need to add a class to an element as it scrolls into view. There are multiple elements on the page which this needs to work for.
I have setup a fiddle displaying how it works: http://jsfiddle.net/e8qX9/35/
Below is the part which is causing me an issue:
if ($('#one').doesExist()) { // checks if element exists
$(window).scroll(function () { // on scroll
addInView('#one', 'slideleft', 0.2); // run function with params
});
addInView('#one', 'slideleft', 0.2); // run function again on page load
}
if ($('#two').doesExist()) {
$(window).scroll(function () {
addInView('#two', 'slideright', 0.5);
});
addInView('#two', 'slideright', 0.5);
}
if ($('#three').doesExist()) {
$(window).scroll(function () {
addInView('#three', 'slideup', 0.2);
});
addInView('#three', 'slideup', 0.2);
}
if ($('#four').doesExist()) {
$(window).scroll(function () {
addInView('#four', 'slidedown', 0.5);
});
addInView('#four', 'slidedown', 0.5);
}
if ($('#five').doesExist()) {
$(window).scroll(function () {
addInView('#five', 'slideleft', 0.2);
});
addInView('#five', 'slideleft', 0.2);
}
I was wondering if there was a better way of running this function multiple times, both when the page loads and when it scrolls. I thought about creating a multidimensional array and looping through it, but can't quite get my head around it.
Thanks