Scroll to an element with jQuery
javascript, jquery, scroll
Solution
Assuming you have a button with the id `button`, try this example:
$("#button").click(function() {
$([document.documentElement, document.body]).animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 2000);
});
I got the code from the article Smoothly scroll to an element without a jQuery plugin. And I have tested it on the example below.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
$(document).ready(function (){
$("#click").click(function (){
$('html, body').animate({
scrollTop: $("#div1").offset().top
}, 2000);
});
});
</script>
<div id="div1" style="height: 1000px; width 100px">
Test
</div>
<br/>
<div id="div2" style="height: 1000px; width 100px">
Test 2
</div>
<button id="click">Click me</button>
</html>
Problem
I have this `input` element: ``` <input type="text" class="textfield" value="" id="subject" name="subject"> ``` Then I have some other elements, like other tag's & `<textarea>` tag's, etc... When the user clicks on the `<input id="#subject">`, the page should scroll to the page's last element, and it should do so with a nice animation (It should be a scroll to bottom and not to top). The last item of the page is a `submit` button with `#submit`: ``` <input type="submit" class="submit" id="submit" name="submit" value="Ok, Done."> ``` The animation should not be too fast and should be fluid. I am running the latest jQuery version. I prefer to not install any plugin but to use the default jQuery features to achieve this.
Related problems
- scrollIntoView Scrolls just too far
- How do I get the offset().top value of an element without using jQuery?
- How to scroll to specific item using jQuery?
- Callback of .animate() gets called twice jquery
- jQuery flicker when using animate-scrollTo
- scrollTop animation without jquery
- How come $('html').animate() only works in IE and $('body').animate() is needed for Chrome/Safari?