Similar questions have been asked, but I cannot find any help in those topics.
Take a look at this fiddle. There's a menu on the right, which allows you to scroll to other sections.
What should happen is that when you click "3" for instance, "3" in the menu gets the class "visible" (shown by a larger font size). For some reason or another, however, the first list item always gets this class. I think this happens because in the if-statement below, the first div is evaluated as true for having an offset of zero, which is not correct.
$("body > div").each(function () {
var $this = $(this),
nextSection = $this.next("div"),
offsetT = $this.offset().top;
if (offsetT == 0) {
$("nav a").removeClass("visible");
$("a#" + $this.attr("id") + "-button").addClass("visible");
console.log("a#" + $this.attr("id") + "-button");
}
});
Another explanation might be that something goes wrong because the function is called as a callback. I did so intentionally because when it is called as a callback, there should eb no animation left, and a container should be snapped to the top of the window: so there can only be one element that has offset == 0
. But as you can see, this does not work.
The class must be set inside the SnapIt function, because the function is used else where as well.