How to scroll to a specific div using jquery or javascript

I want to scroll to a specific div using jquery

I wrote code like:

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

But it does not move to div position. How can I do this in jquery or javascript

+6
source share
5 answers

try it

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

.scrollTop ()

+20
source

Try

. scrollTop ()

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


or

scrollIntoView ()

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

or

 document.getElementById('dynamictabstrp').scrollIntoView(true); 
+8
source

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); }); }); 
+1
source

Try this simple script. Change #targetDiv to your id or div class.

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

Source code and live demo can be found here - Smooth scrolling to div using jQuery

0
source

You can set the offset as required

 jQuery(document).ready(function(){ function secOffset(){ jQuery('html, body').animate({ scrollTop: jQuery(window.location.hash).offset().top - 60 }, 0); } }); 
0
source

Source: https://habr.com/ru/post/957729/


All Articles