javascript - Nav anchor doesn't apply active class correctly while scroll jQuery -
I have a website, in & lt; Header & gt; The sticky nav is under the
element, as long as I scroll to a section, it activates a class using data-attributes
, here's the bug: when I class If I scroll to active
, half the section also does not scroll according to the section section.
What do I need active
to turn on the class until I get the anchor of each section, I leave my code below a JSfield, you can see that What is the problem, I hope you guys can help me.
HTML:
& lt; Header class = "testheader" & gt; & Lt; Nav id = "cloud_nav" class = "absolute" & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "#" data-scroll = "overview" & gt; Section 1 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "#" data-scroll = "readiness" & gt; Section 2 & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "#" data-scroll = "support" & gt; Section 3 & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Neo & gt; & Lt; / Header & gt; & Lt; Section data-anchor = "overview" style = "background: red; font-size: 25px;" & Gt; & Lt; / Section & gt; & Lt; Section data-anchor = "readiness" style = "background: green; font-size: 25px;" & Gt; & Lt; / Section & gt; & Lt; Section data-anchor = "support" style = "background: #ccc; font-size: 25px;" & Gt; & Lt; / Section & gt; & Lt; / Div & gt;
Javascript:
& lt; Script type = "text / javascript" & gt; ('Data' scroll), scrollpoint = $ ('section [data-anchor]' ('click', function) (var scroll anchor = $ (this). $ ('Body, html'). Animate ({scrollTop : ScrollPoint}, 500); return returned;}) var navOffset = $ ('# cloud_nav') offset () top .. $ (window) .scroll (function () {var scrollPos = $ (window) .scrollTop () ; If (scrollPos> = navOffset) {$ ('# cloud_nav'). RemoveClass ('absolute'); $ ('$ (' section '(' active '); $ (' # cloud_nav a ') Eq (i) .addClass ('active');}})} and {$ ('# cloud_nav'). AddClass ('absolute'); $ ('# cloud_nav a.active'). RemoveClass ('active' ); $ ('# Cloud_nav a: first'); $ '(' # cloud_nav a: first ') .addClass (' active ');}}); & lt; / script & gt;
text ">
Was it done after you?
First, I changed the account to the size of the title to calculate the scrollPoint
:
scrollPoint = $ ('section [Data-anchor = '' + scrollAnchor + '"]'). Offset () top-$ ('# cloud_nav'). Outer heights (); After that, decrease 50 pixels, we add the height of that nav, where it is detecting the scroll position: if ($ (this) .bizon (). Top
Now scroll the anchor to the right place and the active classes are being switched correctly.
Comments
Post a Comment