Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery scroll to a div tag

Tags:

jquery

I use the following in order scrolling to top. How could i edit it so the top is set by a div tag?

var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
pageRequestManager.add_endRequest(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
});
like image 298
OrElse Avatar asked Jun 09 '10 08:06

OrElse


People also ask

How do I scroll an element into a div?

// get the "Div" inside which you wish to scroll (i.e. the container element) const El = document. getElementById('xyz'); // Lets say you wish to scroll by 100px, El. scrollTo({top: 100, behavior: 'smooth'}); // If you wish to scroll until the end of the container El. scrollTo({top: El.

How to scroll to an element with jQuery?

scrollTop() method: It is used to set the vertical position of the scroll bar to the value 'val'. offSet() Method: It is used to get the coordinates of the first element in the set of all matched elements. Example 1: This example describes how to scroll a specific element using jQuery.

How do I smooth a div scroll?

click(function(event) { event. preventDefault(); $. scrollTo($('#myDiv'), 1000); });

Why is scrollTop not working?

If your CSS html element has the following overflow markup, scrollTop will not function. To allow scrollTop to scroll, modify your markup remove overflow markup from the html element and append to a body element.


1 Answers

You need to use .offset() to get the correct position value like:

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

.offset returns the current position of an element relative to the document.

References: .position(), .offset()

like image 127
jAndy Avatar answered Nov 09 '22 15:11

jAndy