Search code examples
javascriptjqueryhashsmooth-scrolling

smooth scrolling how to use excerpting?


I have this code and it works well.

$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
    && location.hostname == this.hostname) {
    var target = $(this.hash);
    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
    if (target.length) {
        $('html, body').animate({
            scrollTop: target.offset().top
        }, 1000);
        return false;
    }
}

});

But also I have some elements and I don't want apply smooth scrolling for them! How can I do it ? Example - http://codepen.io/zoom/pen/ggYaXZ I don't want aplly it to <li><a href="#apple">Scroll to Section Apple</a></li>


Solution

  • You can use some other data- attribute:

    $('a[href*="#"]:not([href="#"]):not([data-no-smooth-scroll])').click(function() {
    

    And inside your html add data-no-smooth-scroll="true" to the relevant link:

    <a href="#apple" data-no-smooth-scroll="true">Scroll to Section Apple</a>
    

    Here is a fork to your codepen:
    http://codepen.io/anon/pen/dNbYKe