How to scroll to the particular div using jquery or javascript

后端 未结 5 991
耶瑟儿~
耶瑟儿~ 2021-02-07 16:25

I want to scroll to the particular div using jquery

I have written the code like:

 $(\"#button\").on(\'click\',function(){
     var p = $(\"#dynamictabst         


        
相关标签:
5条回答
  • 2021-02-07 17:15

    You can set offset as per requirement

    jQuery(document).ready(function(){
    function secOffset(){
                            jQuery('html, body').animate({
                            scrollTop: jQuery(window.location.hash).offset().top - 60
                    }, 0);
            }
    });
    
    0 讨论(0)
  • 2021-02-07 17:21

    Here is the code :-

    $(document).ready(function (){
      $("#button").on('click',function(){                
             $('html, body').animate({
                  scrollTop: $("#dynamictabstrp").offset().top
            }, 1000);               
        });
    });
    

    or

    $(document).ready(function (){
      $("#button").click(function(){                
             $('html, body').animate({
                  scrollTop: $("#dynamictabstrp").offset().top
            }, 1000);               
        });
    });
    
    0 讨论(0)
  • 2021-02-07 17:23

    Try

    .scrollTop()

    $(window).scrollTop($('#dynamictabstrp').offset().top);
    


    or

    scrollIntoView()

    $('#dynamictabstrp')[0].scrollIntoView(true);
    

    or

    document.getElementById('dynamictabstrp').scrollIntoView(true);
    
    0 讨论(0)
  • 2021-02-07 17:29

    Try this simple script. Change #targetDiv with your particular div ID or Class.

    $('html,body').animate({
        scrollTop: $('#targetDiv').offset().top
    }, 1000);
    

    The source code and live demo can be found from here - Smooth scroll to div using jQuery

    0 讨论(0)
  • 2021-02-07 17:31

    Try this

    $("#button").on('click',function() {
        $('html, body').animate({
            'scrollTop' : $("#dynamictabstrp").position().top
        });
    });
    

    .scrollTop()

    0 讨论(0)
提交回复
热议问题