Tuesday, 30 January 2018

javascript - jQuery scroll to element



I have this input element:






Then I have some other elements, like other text inputs, textareas, etc.




When the user clicks on that input with #subject, the page should scroll to the last element of the page 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:






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.


Answer



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.









Test




Test 2






No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...