• Stars
    star
    3,673
  • Rank 11,488 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight, cross-browser and highly customizable animated scrolling with jQuery

jQuery.scrollTo

Lightweight, cross-browser and highly customizable animated scrolling with jQuery

GitHub version

Installation

The plugin requires jQuery 1.8 or higher.

Via bower:

bower install jquery.scrollTo

Via npm:

npm install jquery.scrollto

Via packagist:

php composer.phar require --prefer-dist flesler/jquery.scrollto "*"

Using a public CDN

CDN provided by jsdelivr

<script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery.scrollTo.min.js"></script>

CDN provided by cdnjs

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/2.1.3/jquery.scrollTo.min.js"></script>

Downloading Manually

If you want the latest stable version, get the latest release from the releases page.

2.0

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.

Usage

jQuery.scrollTo's signature is designed to resemble $().animate().

$(element).scrollTo(target[,duration][,settings]);

element

This must be a scrollable element, to scroll the whole window use $(window).

target

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 left and top containining 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")

settings

The duration parameter is a shortcut to the setting with the same name. These are the supported settings:

  • axis: The axes to animate: xy (default), x, y, yx
  • interrupt: If true will cancel the animation if the user scrolls. Default is false
  • limit: If true the plugin will not scroll beyond the container's size. Default is true
  • margin: If true, subtracts the margin and border of the target element. Default is false
  • offset: Added to the final position, can be a number or an object with left and top
  • over: Adds a % of the target dimensions: {left:0.5, top:0.5}
  • queue: If true will scroll one axis and then the other. Default is false
  • 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 0 which 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

window shorthand

You can use $.scrollTo(...) as a shorthand for $(window).scrollTo(...).

Changing the default settings

As with most plugins, the default settings are exposed so they can be changed.

$.extend($.scrollTo.defaults, {
  axis: 'y',
  duration: 800
});

Stopping the animation

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.

onAfter and requestAnimationFrame

jQuery.scrollTo has a onAfter callback for work that runs after the animation finishes. It will be called before the scroll event fires. To combat this you can use requestAnimationFrame to do work on the next tick. It is available in many browsers, but you may want to polyfill for the few it does not support.

$.scrollTo(100, {
  onAfter: function() {
    requestAnimationFrame(function() {
        $(".result").addClass("selected");
    });
  }
});

Demo

Check the demo to see every option in action.

Complementary plugins

There are two plugins, also created by me that depend on jQuery.scrollTo and aim to simplify certain use cases.

jQuery.localScroll

This plugin makes it very easy to implement anchor navigation. If you don't want to include another plugin, you can try using something like this minimalistic gist.

jQuery.serialScroll

This plugin simplifies the creation of scrolling slideshows.

Troubleshooting

  • Cannot read property 'propHooks' of undefined: You are most likely using the slim version jQuery, which doesn't include the effects module. The plugin cannot function without it and you'll need the full version.

  • The element doesn't scroll: A good approach to debug is to set overflow: auto temporarily to the scrollable element, to make sure it would be manually scrollable. If you don't see any scrollbar, the problem is likely in the HTML/CSS.

License

(The MIT License)

Copyright (c) 2007 Ariel Flesler [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

jquery.localScroll

Animated anchor navigation made easy with jQuery
JavaScript
615
star
2

hashmap

HashMap JavaScript class for Node.js and the browser. The keys can be anything and won't be stringified
JavaScript
385
star
3

jquery.serialScroll

Animated scrolling of series with jQuery
JavaScript
181
star
4

config-node

Flexible lightweight configuration loader for Node
JavaScript
75
star
5

node-spider

Generic web crawler powered by NodeJS
JavaScript
68
star
6

parallel

CLI tool to execute shell commands in parallel, based on GNU parallel command
JavaScript
34
star
7

jquery.rule

JavaScript
17
star
8

jquery.preload

Multi-functional image preloader plugin for jQuery
JavaScript
14
star
9

uver-cli

CLI for uver, bump the version on package.json and other files
JavaScript
13
star
10

XMLWriter

XML generator for Javascript, based on .NET's XMLTextWriter.
JavaScript
13
star
11

connect-pause

Connect/Express middleware to simulate latency for debugging
JavaScript
12
star
12

node-http-codes

Map of HTTP status messages to their code, based on Node's built-in mapping
JavaScript
10
star
13

lucky

CLI tool to measure the state of your luck, at the moment
JavaScript
10
star
14

uver

Bump the version on package.json and other files
JavaScript
7
star
15

stress-node

Stress test library for Node JS
JavaScript
6
star
16

dotfiles

My dotfiles
Shell
5
star
17

word-clock

A responsive HTML5 Word-based clock
JavaScript
5
star
18

stress-node-server

Node Server for running stress tests
JavaScript
4
star
19

taptap

A touch game for dogs and cats
HTML
4
star
20

electron-wi-fine

A connectivity monitoring app, built on Electron
JavaScript
4
star
21

node-http-status

HTTP Status class for NodeJS and Express
JavaScript
3
star
22

vscode-url-encode

Adds commands to URL-encode and URL-decode text
JavaScript
3
star
23

jwt-crack

Multi-threaded brute force JWT cracker in pure Node.js
JavaScript
3
star
24

satoshi-data

Curated dataset of Satoshi Nakamoto responding to users
TypeScript
3
star
25

todo-list

A single HTML page tool to track time spent on tasks
JavaScript
2
star
26

genetic-maze

Build a Maze, then let a Genetic Algorithm solve it
JavaScript
2
star
27

idonethis-form

Very simple mobile-friendly HTML form to submit dones to IDoneThis
JavaScript
2
star
28

infer-gender

Infer the gender of a name based on a large list of names, case insensitive and removes diacritics
JavaScript
2
star
29

remote

Browser to browser keyboard
HTML
1
star
30

xdotool-trigger

A simple wrapper for xdotool to easily trigger some artificial inputs
Shell
1
star
31

node-watch

Transparent hot reload of files in place for NodeJS
JavaScript
1
star
32

tracer

Trace redirects from a URL
JavaScript
1
star
33

jquery.detect

(Bookmarklet) Detect jQuery version and plugins on a page
JavaScript
1
star