Via bower:
bower install jquery.scrollToVia npm:
npm install jquery.scrolltoVia packagist:
php composer.phar require --prefer-dist flesler/jquery.scrollto "*"CDN provided by cdnjs
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.14/jquery.scrollTo.min.js"></script>If you want the latest stable version, get the latest release from the releases page.
Version 2.0 has been recently released. It is mostly backwards compatible, if you have any issue first check this link. If your problem is not solved then go ahead and report the issue.
jQuery.scrollTo's signature is designed to resemble $().animate().
$(element).scrollTo(target[,duration][,settings]);This must be a scrollable element, to scroll the whole window use $(window).
This defines the position to where element must be scrolled. The plugin supports all these formats:
- A number with a fixed position:
250 - A string with a fixed position with px:
"250px" - A string with a percentage (of container's size):
"50%" - A string with a relative step:
"+=50px" - An object with
leftandtopcontainining any of the aforementioned:{left:250, top:"50px"} - The string
"max"to scroll to the end. - A string selector that will be relative to the element to scroll:
".section:eq(2)" - A DOM element, probably a child of the element to scroll:
document.getElementById("top") - A jQuery object with a DOM element:
$("#top")
The duration parameter is a shortcut to the setting with the same name.
These are the list of supported settings:
- axis: The axes to animate:
xy(default),x,y,yx - interrupt: If
truewill cancel the animation if the user scrolls. Default isfalse - limit: If
truethe plugin will not scroll beyond the container's size. Default istrue - margin: If
true, subtracts the margin and border of thetargetelement. Default isfalse - offset: Added to the final position, can be a number or an object with
leftandtop - over: Adds a % of the
targetdimensions:{left:0.5, top:0.5} - queue: If
truewill scroll oneaxisand then the other. Default isfalse - onAfter(target, settings): A callback triggered when the animation ends (jQuery's
complete()) - onAfterFirst(target, settings): A callback triggered after the first axis scrolls when queueing
You can add any setting supported by $().animate() as well:
- duration: Duration of the animation, default is
0which makes it instantaneous - easing: Name of an easing equation, you must register the easing function:
swing - fail(): A callback triggered when the animation is stopped (f.e via
interrupt) - step(): A callback triggered for every animated property on every frame
- progress(): A callback triggered on every frame
- And more, check jQuery's documentation
jQuery.scrollTo ends up creating ordinary animations which can be stopped by calling $().stop() or $().finish() on the same element you called $().scrollTo(), including the window.
Remember you can pass a fail() callback to be called when the animation is stopped.
Check the demo to see every option in action.
There are two plugins, also created by me that depend on jQuery.scrollTo and aim to simplify certain use cases.
This plugin makes it very easy to implement anchor navigation.
This plugin simplifies the creation of scrolling slideshows.