How to scroll to div after click on About or Contact in my menu?

前端 未结 3 799
说谎
说谎 2021-02-06 13:43

How to scroll to div (e.g: #about, #contact) after click on About or Contact in my menu?

3条回答
  •  礼貌的吻别
    2021-02-06 14:23

    Your html:

    Name site

    about
    contact

    Your javascript:

    $('ul.nav').find('a').click(function(){
        var $href = $(this).attr('href');
        var $anchor = $('#'+$href).offset();
        window.scrollTo($anchor.left,$anchor.top);
        return false;
    });
    

    If you want to use animate, replace

    window.scrollTo($anchor.left,$anchor.top);
    

    by

    $('body').animate({ scrollTop: $anchor.top });
    

提交回复
热议问题