Experience a demonstration of how the user's scroll state can be effectively scripted. By opening your browser console and observing the logs as you scroll up and down, you can witness the mechanics in action. Additionally, an event pause for animation between slides has been included to enhance the experience.
While my method relies on the presence of a scrollable element, an alternative event handler for the innovative jQuery Mouse Wheel Plugin is also discussed. This plugin eliminates the need for scrolling by listening to mousewheel events, though it was not successfully implemented for the demo, sticking instead to the original approach.
Feel free to explore the demo at: http://jsfiddle.net/YF4HY/
$(function () {
var scroll = 0, //tracking scroll count
scroll_max = 15, //maximum scrolls per slide
prev_scroll = 0, //previous scrollTop for direction
slide = 0, //current slide tracker
slide_max = 5, //total slides
animated = false; //animation flag
function scrollUp () {
if(scroll === 0 && slide < 1){
console.log('beginning of slides');
} else if (scroll > 0) {
scroll--;
} else if (scroll === 0 && slide > 0) {
simulateAnimation(slideBackward);
}
}
function scrollDown () {
if(scroll === scroll_max && slide === slide_max){
console.log('top of slides');
} else if (scroll < scroll_max) {
scroll++;
} else if (scroll === scroll_max && slide < slide_max) {
simulateAnimation(slideForward);
}
}
function slideForward () {
scroll = 0;
slide++;
animated = false;
}
function slideBackward () {
scroll = scroll_max;
slide--;
animated = false;
}
function determineScrollDirection (current_scroll) {
var prev = prev_scroll;
prev_scroll = current_scroll;
return (current_scroll > prev) ? 'down' : 'up';
}
function simulateAnimation (handler) {
animated = true;
setTimeout(handler, 2000);
}
$(window).on('scroll', function (e) {
if(!animated){
switch (determineScrollDirection($(this).scrollTop())) {
case 'down':
scrollDown();
break;
case 'up':
scrollUp();
break;
}
console.log({slide:slide, scroll:scroll});
} else {
console.log('Don\'t do anything because we are changing slides.');
}
});
$(document).on('mousewheel', function(event, delta, deltaX, deltaY) {
//console.log(delta, deltaX, deltaY);
});
});