How to scroll to an element in jQuery?

后端 未结 9 1715
无人及你
无人及你 2020-11-30 19:23

I have done the following code in JavaScript to put focus on the particular element (branch1 is a element),

document.location.href=\"#branch1\";
         


        
9条回答
  •  佛祖请我去吃肉
    2020-11-30 19:59

    You can extend jQuery functionalities like this:

    jQuery.fn.extend({
    scrollToMe: function () {
        var x = jQuery(this).offset().top - 100;
        jQuery('html,body').animate({scrollTop: x}, 500);
    }});
    

    and then:

    $('...').scrollToMe();
    

    easy ;-)

提交回复
热议问题