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.
var scrollingScreen = false;
$("body").mousewheel(function(event, delta) {
if ( !scrollingScreen ) {
scrollingScreen = true; // Throttles the call
var top = $("body").scrollTop() || // Chrome places overflow at body
$("html").scrollTop(); // Firefox places it at html
// Finds slide headers above/below the current scroll top
var candidates = $(".slide").filter(function() {
if ( delta < 0 )
return $(this).offset().top > top + 1;
else
return $(this).offset().top < top - 1;
});
// If one of more are found, updates top
if ( candidates.length > 0 ) {
if ( delta < 0 )
top = candidates.first().offset().top;
else if ( delta > 0 )
top = candidates.last().offset().top;
}
// Performs an animated scroll to the right place
$("html,body").animate({ scrollTop:top }, 800, "easeInOutQuint", function() {
scrollingScreen = false; // Releases the throttle
});
}
return false; // Prevents default scrolling
});
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 animate or in its callback function, to perform them before or after the scrolling respectively. (Obs.: the fiddle, running in an iframe, cannot access scrollTop in some browsers, for security reasons, so it stores the top variable 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With