Allows for easy implementation of smooth scrolling for same-page links.
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
You can try a bare-bones demo at kswedberg.github.io/jquery-smooth-scroll/demo/
$('a').smoothScroll();
$('#container a').smoothScroll();
$('#container a').smoothScroll({excludeWithin: ['.container2']});
$('a').smoothScroll({exclude: ['.rough','#chunky']});
$('.backtotop').smoothScroll({offset: -100});
$('a').smoothScroll({afterScroll: function() { alert('we made it!'); }});
The following options, shown with their default values, are available for both $.fn.smoothScroll
and $.smoothScroll
:
12345678910111213141516171819202122232425262728293031323334 {
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.
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()
document.documentElement
/
document.body
)Doesn't automatically fire, so you need to bind it to some other user interaction. For example:
1234567 $(
'button.scrollsomething'
).on(
'click'
,
function
() {
$.smoothScroll({
scrollElement: $(
'div.scrollme'
),
scrollTarget:
'#findme'
});
return
false
;
});
The $.smoothScroll
method can take one or two arguments.
scrollTarget
option.The following option, in addition to those listed for $.fn.smoothScroll
above, is available
for $.smoothScroll
:
12345 {
// jQuery set of elements you wish to scroll.
// if null (default), $('html, body').firstScrollable() is used.
scrollElement:
null
}
.find()
or .next()
..find()
or .next()
.$('html, body').firstScrollable().animate({scrollTop: someNumber},
someSpeed)
$.fn.smoothScroll
method looks
for an element with an id attribute that matches the <a>
element's hash.
It does not look at the element's name attribute. If you want a clicked link
to scroll to a "named anchor" (e.g. <a name="foo">
), you'll need to use the
$.smoothScroll
method instead.$.fn.smoothScroll
and $.smoothScroll
methods use the
$.fn.firstScrollable
DOM traversal method (also defined by this plugin)
to determine which element is scrollable. If no elements are scrollable,
these methods return a jQuery object containing an empty array, just like
all of jQuery's other DOM traversal methods. Any further chained methods,
therefore, will be called against no elements (which, in most cases,
means that nothing will happen).Thank you! Please consider the following when working on this repo before you submit a pull request:
src/jquery.smooth-scroll.js
.jshint
grunt file options and the .jscsrc
file. To be sure your additions comply, run grunt lint
from the command line.--author
flag to ensure that proper authorship (yours) is maintained.