I'm new to jQuery & Javascript and I'm trying to remake the Apple 5s sliding page as you can see here: http://www.apple.com/iphone-5s/ . I'm quite far at the moment, but it doesn't stop calling the function when you rapidly scroll. What I mean by that, if you scroll down really fast with my code you basically get to the lowest slide. But at Apple its site the slide animation has to be finished before you can call the animation again. I'm having problems with realizing this. Here is my code.
HTML:
<div id="slider">
<div id="slide1"></div>
<div id="slide2"></div>
<div id="slide3"></div>
</div>
jQuery:
// jQuery for Sliding
$(document).ready(function() {
var scroll = 1;
var currentScroll = 1;
var totalSlides = $("#slider").children().length;
$("#slider").children().hide();
$("#slide1").show();
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
// Scroll event up
currentScroll = scroll;
scroll = scroll - 1;
if (scroll < 1) {
scroll = 1;
}
else {
$("#slide" + currentScroll).hide("slide", {
direction : 'down'
}, 700);
$("#slide" + scroll).show("slide", {
direction : 'up'
}, 700);
}
} else {
// Scroll event down
currentScroll = scroll;
scroll++;
if (scroll < totalSlides + 1) {
$("#slide" + currentScroll).hide("slide", {
direction : 'up'
}, 700);
$("#slide" + scroll).show("slide", {
direction : 'down'
}, 700);
}
}
});
});
Thanks for your help!
On the Apple's page, it's not hiding/showing the slides it's just anchoring it to a fix point (which is the slides
offset().top
position) depending on mousewheel direction. Binding'mousewheel'
alone does not work on other browsers like firefox, it should be'mousewheel DOMMouseScroll'
. Furthermore 'mousewheel-down' on firefox returns a positive value on chrome it's negative value.This should be the mousewheel event structure, cross-browser-wise:
We need to determine which slide is currently being anchored, we can add a class 'active' as the indicator. If the user's mousewheel direction is down, move to the
next()
slide and reassign the class 'active' on this slide. If the user user's mousewheel direction is up, move to theprev()
slide of the current 'active' slide and reassign the 'active' class.See this jsfiddle.