I am trying to make this scrolleffect work on a website. When a users scroll down it automagically has to go to the next slide. When the users scrolls up it has to go one slide back. I have tried a lot of things using the onScroll function in Jquery, but none of them seem to work.
I use the following script to check whether the users scrolls up or down.
var lastScrollTop = 0;
var check = 1;
var scrollDirection = 'down';
var scrollBottom = $(window).scrollTop() + $(window).height();
$(window).scroll(function(event) {
slideHeight = $('.slide').height();
var st = $(this).scrollTop();
if (st > lastScrollTop){
scrollDirection = 'down';
if (check == 1){
$('body').scrollTo( {top:'0px', left:'100%'}, 800 );
check = 0;
}
} else {
scrollDirection = 'up';
}
lastScrollTop = st;
console.log('ScrollDirection: '+ scrollDirection);
});
I am not coming any further than this. The (test)website where developing takes place is: http://bit.ly/RBcffY
If anyone has experience with this kind of function it would be really helpful.
The jquery.mousewheel plugin can be used to capture the mouse wheel even when no scrolling happened (contrary to
$.scroll). This way you can both detect that the mouse wheel was moved and prevent the scrolling from happening. Then it’s just a matter to performing the animated scrolling yourself.Working example at jsFiddle. This is pretty consistent with the behavior of the reference you showed, and you could add extra effects as needed – by inserting code either before the
animateor in its callback function, to perform them before or after the scrolling respectively. (Obs.: the fiddle, running in aniframe, cannot accessscrollTopin some browsers, for security reasons, so it stores thetopvariable globally; however, the code as shown in the answer should work fine in a standalone page)Note: both in the reference site and in my example, middle clicking and moving the mouse scrolls at will. You might want to prevent that or not, your choice. Nonetheless, the updated example scrolls to a specific point instead of just addind some delta, like you suggested in your question –
$('.slide').height(). The reason is to account for the possibility that different slides have different heights. Also, knowing exactly which slide you’re showing allows you to properly set the location’s hash.See also this related question for more info.