JQuery animates the scroll bar at the top of a div

I have a hidden div, .model-detail-panel, which opens when I click .span.

$('[class*="span"]').on('click', function () { $(this).parent().next('.model-detail-panel').slideToggle() }); 

I want to include .animate () in this code to scroll the screen so that the top of the opened div is at the top of the window.

How can i achieve this?

Thanks in advance.

+4
source share
2 answers

You can use Position , for example:

 $("body, html").animate({ scrollTop: $(".model-detail-panel").position().top }); 
+13
source

Slightly changing the code suggested by @ michele-bertoli to

 $('html, body').animate({scrollTop: $(".model-detail-panel").offset().top }, 500); 

Worked for me ..

Thanks to those who posted ..

+3
source

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


All Articles