I've long been using the excellent JS plugin called Waypoints.js to change CSS classes based on a user's viewport position. Now I'm trying to dynamically create these waypoints.
I've come up with two unsuccessful approaches.
Given this HTML:
<div class="emp-question-set fade-up" id="emp-question-set-1"></div>
<div class="emp-question-set fade-up" id="emp-question-set-2"></div>
<div class="emp-question-set fade-up" id="emp-question-set-3"></div>
Approach 1: Generate the waypoints using a for loop and getElementByID
// Number of question areas
var questionCount = $('.emp-question-set').length;
// Setup variables
var waypointName, selector, selectorjQuery;
// Start at 2, leave the first item to load normally
for (var i = 1; i <= questionCount; i++) {
waypointName = 'questionsRollIn' + i;
selector = 'emp-question-set-' + i;
selectorjQuery = '#emp-question-set-' + i;
waypointName = new Waypoint({
element: document.getElementById(selector),
handler: function(direction) {
if (direction === 'down') {
$(selectorjQuery).addClass('fade-up-active');
}
if (direction === 'up') {
$(selectorjQuery).removeClass('fade-up-active');
}
},
offset: '70%'
});
}
Approach 2: Generate the waypoints using getElementsByClassName
questionsRollIn = new Waypoint({
element: document.getElementsByClassName('emp-question-set'),
handler: function(direction) {
if (direction === 'down') {
$('emp-question-set').addClass('fade-up-active');
}
if (direction === 'up') {
$('emp-question-set').removeClass('fade-up-active');
}
},
offset: '70%'
});
Hopefully one of you can help, thanks in advance.