Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try changing plugin like this (added lines are marked):</p> <pre><code>jQuery.fn.anchorAnimate = function (settings) { settings = jQuery.extend({ speed: 500 }, settings); var scrollFn = function () { // added $('[href^=#]').removeClass('active'); $(window).unbind('scroll', scrollFn); } return this.each(function () { var caller = this $(caller).click(function (event) { event.preventDefault() var locationHref = window.location.href var elementClick = $(caller).attr("href") var destination = $(elementClick).offset().top; $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, settings.speed, 'easeOutCubic', function () { window.location.hash = elementClick $('[href^=#]').removeClass('active'); // added $('[href^=' + elementClick + ']').addClass('active'); // added $(window).scroll(scrollFn); // added }); return false; }) }) } $(document).ready(function () { $('[href^=#]').anchorAnimate(); }); </code></pre> <p>EDIT: Explanation:</p> <p>Animate method takes callback as its final parameter. This callback is called <em>after</em> animation finishes. See <a href="http://api.jquery.com/animate/" rel="nofollow noreferrer">http://api.jquery.com/animate/</a>.</p> <p>So on anchor click an animation will start. When it finishes, it will change window.location.hash (original plugin code)</p> <p>Then it will remove "active" class from all links pointing to this document (for the cases where user clicks on the links without scrolling in between).</p> <p>Then it will add "active" class to the anchor that user just clicked</p> <p>Finally it will bind an event handler to window scroll. By biding it <em>after</em> animation, we ensure, that it doesn't fire during animation.</p> <p>Now when user scrolls using mouse or keys, our event handler is fired. We remove the active class from all the links and unbind the handler so it doesn't get called again before user clicks another link.</p> <p>When another link is clicked, whole process is repeated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload