简体   繁体   中英

Smooth Scrolling JavaScript work on all A tags

I have the following JavaScript Code for smoothing scrolling:

$(document).on('click', 'a', function(event){
    event.preventDefault();

    $('html, body').animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top
    }, 500);
});

Now I can't use other a links on my website. When I do this: LINK nothing happened.

My website: http://www.be-virtual.org/schnittchen

Currently it targets all the <a> tag. Change your selector correctly to target only those start with # :

$(document).on('click', 'a[href^="#"]', function(event){
    event.preventDefault();

    $('html, body').animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top
    }, 500);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM