Demo

Smooth Scroll Plugin

Allows for easy implementation of smooth scrolling for same-page links.

NPM

Download

Using npm:

1
npm install jquery-smooth-scroll

The old-fashioned way:

Go to the following URL in your browser and copy/paste the code into your own file: https://raw.githubusercontent.com/kswedberg/jquery-smooth-scroll/master/jquery.smooth-scroll.js

Demo

You can try a bare-bones demo at kswedberg.github.io/jquery-smooth-scroll/demo/

Features

$.fn.smoothScroll

Options

The following options, shown with their default values, are available for both $.fn.smoothScroll and $.smoothScroll:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
  offset: 0,
 
  // one of 'top' or 'left'
  direction: 'top',
 
  // only use if you want to override default behavior
  scrollTarget: null,
 
  // string to use as selector for event delegation (Requires jQuery >=1.4.2)
  delegateSelector: null,
 
  // fn(opts) function to be called before scrolling occurs.
  // `this` is the element(s) being scrolled
  beforeScroll: function() {},
 
  // fn(opts) function to be called after scrolling occurs.
  // `this` is the triggering element
  afterScroll: function() {},
  easing: 'swing',
 
  // speed can be a number or 'auto'
  // if 'auto', the speed will be calculated based on the formula:
  // (current scroll position - target scroll position) / autoCoeffic
  speed: 400,
 
  // autoCoefficent: Only used when speed set to "auto".
  // The higher this number, the faster the scroll speed
  autoCoefficient: 2,
 
  // $.fn.smoothScroll only: whether to prevent the default click action
  preventDefault: true
 
}

The options object for $.fn.smoothScroll can take two additional properties: exclude and excludeWithin. The value for both of these is an array of selectors, DOM elements or jQuery objects. Default value for both is an empty array.

Setting options after initial call

If you need to change any of the options after you've already called .smoothScroll(), you can do so by passing the "options" string as the first argument and an options object as the second.

$.smoothScroll

Additional Option

The following option, in addition to those listed for $.fn.smoothScroll above, is available for $.smoothScroll:

1
2
3
4
5
{
  // jQuery set of elements you wish to scroll.
  //  if null (default), $('html, body').firstScrollable() is used.
  scrollElement: null
}

$.fn.scrollable

$.fn.firstScrollable

Notes

Contributing

Thank you! Please consider the following when working on this repo before you submit a pull request: