I'm having a couple of sections on the page each with assigned sectionID. I've added jQuery function for scrolling to anchor point but I can't get all anchor points to show at the same place after scroll.
I have sections named health, medicine, finance, papers. Clicking on the "health" scrolls to the top of the health section but clicking on the "finance" link scrolls to finance section but with offset. It doesn't stop at the very top of the section.
The piece of script I have is as follows.
<script type="text/javascript">
/* show active navigation element */
jQuery(document).ready(function () {
jQuery(document).on("scroll", onScroll);
});
//get scroll position and handle class active
function onScroll(event){
var scrollPos = jQuery(document).scrollTop()+450;
jQuery('#nav li a').each(function () {
var currLink = jQuery(this);
var refElement = jQuery(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
jQuery('#nav li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
/* activate sticky navigation */
jQuery(document).ready(function() {
var stickyNavTop = jQuery('#nav').offset().top;
var stickyNav = function(){
var scrollTop = jQuery(window).scrollTop();
if (scrollTop > stickyNavTop) {
jQuery('#nav').addClass('sticky');
} else {
jQuery('#nav').removeClass('sticky');
}
};
stickyNav();
jQuery(window).scroll(function() {
stickyNav();
});
});
</script>
I found a similar question here, but I couldn't make it work with my code.
How do I control scroll offset for each anchor point with the jQuery I'm using?