javascript - Active class on sticky nav -
my sticky nav:
<nav id="page-nav" class="page-nav"> <ul> <li><a href="#top" class="linky active">overview</a></li> <li><a class="linky" href="#sub-our-drainage-solutions">our drainage solutions</a></li> <li><a class="linky" href="#sub-cctv-drain-survey">cctv drain survey</a></li> <li><a class="linky" href="#sub-wet-waste-disposal">wet waste disposal</a></li> <li><a class="linky" href="#sub-blocked-drains">blocked drains</a></li> <li><a class="linky" href="#sub-cess-pit-emptying">cess pit emptying</a></li> </ul> </nav> <div id="sub-our-drainage-solutions"> </div> <div id="sub-cctv-drain-survey"> </div>
etc...
$(function() { //sticky scroll nav //page nav var s = $("#page-nav"), //sectorcontent = $("#sector-content"), pos = s.position(), linkclicked = false, w = $(window).width(); if(w > 800) { $(window).scroll(function() { var windowpos = $(window).scrolltop(); if (windowpos >= pos.top) { s.addclass("stick"); } else { s.removeclass("stick"); } }); $(window).scroll(function() { var y = $(this).scrolltop(); $('.linky').each(function(event) { id = $(this).attr('href'); if (y >= $(id).offset().top -30) { if (!linkclicked) { $('.linky').not(this).removeclass('active'); $(this).addclass('active'); } } }); }); $("#page-nav li a").click(function(e) { e.preventdefault(); $("#page-nav li a").removeclass('active'); $(this).addclass('active'); linkclicked = true; gotobyscroll($(this).attr("href").replace("#", "")); }); function gotobyscroll(id){ $('html,body').animate({ scrolltop: $("#"+id).offset().top -30}, 'slow', function() { linkclicked = false; }); } } });
the navigation sticks top of window on scroll, need change active class on anchor when passes related div id.
i'm getting following error on scroll:
uncaught typeerror: cannot read property 'top' of null
due part of code:
$(window).scroll(function() { var y = $(this).scrolltop(); $('.linky').each(function(event) { id = $(this).attr('href'); if (y >= $(id).offset().top -30) { if (!linkclicked) { $('.linky').not(this).removeclass('active'); $(this).addclass('active'); } } }); });
what can resolve issue?
not sure happening code, snippet works me change active class on scroll , smooth scrolling go with.
$(document).ready(function () { $(document).on("scroll", onscroll); //smoothscroll $('#nav-minor a[href^="#"]').on('click', function (e) { e.preventdefault(); $(document).off("scroll"); $('#nav-minor a').each(function () { $(this).removeclass('active'); }) $(this).addclass('active'); var target = this.hash, menu = target; var $target = $(target); $('html, body').stop().animate({ 'scrolltop': $target.offset().top+2 }, 500, 'swing', function () { window.location.hash = target; $(document).on("scroll", onscroll); }); }); }); function onscroll(event){ var scrollpo = $(document).scrolltop(); $('#nav-minor a').each(function () { var currlink = $(this); var refelement = $(currlink.attr("href")); if (refelement.position().top <= scrollpo && refelement.position().top + refelement.height() > scrollpo) { $('#nav-minor a').removeclass("active"); currlink.addclass("active"); } else{ currlink.removeclass("active"); } }); }
then need basic css styling of class="active"
Comments
Post a Comment