简体   繁体   中英

How to scroll to the particular div using jquery or javascript

I want to scroll to the particular div using jquery

I have written the code like:

 $("#button").on('click',function(){
     var p = $("#dynamictabstrp");
     var offset = p.offset();
     window.scrollBy(offset.left, offset.top);
 });

But it is not moving to the div position. How can i do that in jquery or javascript

Try this

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

.scrollTop()

Try

.scrollTop()

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


or

scrollIntoView()

document.getElementById('dynamictabstrp').scrollIntoView(true);

or

 document.getElementById('dynamictabstrp').scrollIntoView(true); 

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);               
    });
});

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

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);
        }
});

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