Skip to content

A cross-browser compatible, lightweight, easy-to-use library for smooth scrolling!

License

Notifications You must be signed in to change notification settings

LieutenantPeacock/SmoothScroll

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SmoothScroll.js

A cross-browser compatible, lightweight, easy-to-use library for smooth scrolling!

smoothScroll({options});

Simple Uses

Scroll to y-position 1000px in 750 milliseconds:

smoothScroll({yPos: 1000, duration: 750});

Scroll to the bottom of the page:

smoothScroll({yPos: 'end'});

Scroll to x-position 500px and y-position 500px:

smoothScroll({xPos: 500, yPos: 500});

Including the Script

The script can be included via CDN by adding the following into the head of the HTML page:

<script src="https://cdn.jsdelivr.net/gh/LieutenantPeacock/SmoothScroll@1.2.0/src/smoothscroll.min.js" integrity="sha384-UdJHYJK9eDBy7vML0TvJGlCpvrJhCuOPGTc7tHbA+jHEgCgjWpPbmMvmd/2bzdXU" crossorigin="anonymous"></script>

You can also download smoothscroll.min.js and include it in the head of the page using your own path:

<script src="path/to/smoothscroll.min.js"></script>

Options

OptionDescriptionDefault
yPos The y-position to scroll to. If specified as a number, it is an absolute number of pixels from the top. It can also be specified as a relative amount from the current position. It can also be the special string values "start" (the top of the scrolling area), "center" (the middle of the scrolling area), and "end" (the bottom of the scrolling area). A relative number of pixels is specified as a string prefixed with + (to scroll down a certain number of pixels) or - (to scroll up a certain number of pixels), e.g. '+100', '-50'. A relative percentage is specified by further adding the % symbol to the end of the string, which indicates scrolling by a percentage of the entire height, e.g. '+10%', '-15%'. No default.
xPos The x-position to scroll to. If specified as a number, it is an absolute number of pixels from the left. It can also be specified as a relative amount from the current position. It can also be the special string values "start" (the left of the scrolling area), "center" (the middle of the scrolling area), and "end" (the right of the scrolling area). A relative number of pixels is specified as a string prefixed with + (to scroll down a certain number of pixels) or - (to scroll up a certain number of pixels), e.g. '+100', '-50'. A relative percentage is specified by further adding the % symbol to the end of the string, which indicates scrolling by a percentage of the entire width, e.g. '+10%', '-15%'. No default.
duration The amount of time the scrolling takes (in milliseconds). 500
scrollingElement The HTML element to scroll instead of the window. No default.
toElement The HTML element to scroll to. This option overrides the xPos and yPos options. No default.
preventUserScroll Prevent the user from scrolling during the smooth scroll animation. true
easing The easing function. It can either be a string that is the name of one of the predefined easing functions (in smoothScroll.easing), or a function that accepts the percentage of time that has passed (in its decimal form, between 0 and 1) and returns the percentage of the scrolling distance to scroll to at the current time (as a number between 0 and 1). 'linear'
complete The callback function to invoke when the smooth scrolling is complete. No default.
firstAxis The first axis to scroll if changing both the x- and y-positions. There are two possible values: 'x' and 'y'. If not specified, both axes will scroll at the same time. No default.
scrollEvents An array of strings indicating the names of events that will stop the scrolling animation if the preventUserScroll option is set to false. ['scroll', 'mousedown', 'wheel', 'DOMMouseScroll', 'mousewheel', 'touchmove']
scrollKeys An array of integer key codes represent the keys that will stop the scrolling animation when pressed by the user if the preventUserScroll option is set to false. [37, 38, 39, 40, 32]
block The position of the element of the element to scroll to (specified by the toElement option) relative to the top of the container. The value can be "start" (the top of the element is aligned to the top of the visible area), "center" (the element is in the vertical center of the visible area), "end" (the bottom of the element is aligned with the bottom of the visible area), or a string indicating the percentage, e.g. '50%'. If not specified, it is the same as "start". No default.
inline The position of the element of the element to scroll to (specified by the toElement option) relative to the left of the container. The value can be "start" (the left of the element is aligned to the left of the visible area), "center" (the element is in the horizontal center of the visible area), "end" (the right of the element is aligned with the right of the visible area), or a string indicating the percentage, e.g. '50%'. If not specified, it is the same as "start". No default.
allowAnimationOverlap A boolean value indicating whether or not multiple simultaneous animations are allowed on the same container. false
paddingTop A certain number of pixels to add to the y-position to scroll to. If not specified, it is the same as 0. No default.
paddingLeft A certain number of pixels to add to the x-position to scroll to. If not specified, it is the same as 0. No default.

Return Value

smoothScroll returns an object with the destroy property which is a function that stops the scrolling when called.

var s = smoothScroll({yPos: 5000, duration: 3000});
// Stop the scrolling sometime later
s.destroy();

Instantiation

Using the new operator will create an instance with the passed in options as defaults. Properties specified in this options object will override the original defaults. Call the smoothScroll method on the returned value to perform smooth scrolling with these defaults.

var scroller = new smoothScroll({duration: 700, block: 'center'});
scroller.smoothScroll({yPos: 500}); // duration is 700 (rather than the original default of 500)

Getting and Setting Global Defaults

smoothScroll.defaults() returns an object containing the default values for each option.
Passing an object to smoothScroll.defaults will overwrite each option in the defaults with the value from that object if it is set.

smoothScroll.defaults({duration: 700}); // set default duration to 700ms

Other Methods and Properties

smoothScroll.stopAll() stops all current scroll animations and returns true/false depending on whether there were running animations that were stopped.

smoothScroll.scrolling() returns true/false depending on whether or not there are current scrolling animations.

smoothScroll.nativeSupported indicates whether or not the CSS scroll-behavior property is supported in the current browser.

smoothScroll.easing is an object containing the predefined easing functions.

Examples

Examples of common use cases can be found in the examples folder. Note that many newer JavaScript features are not used in order to demonstrate more cross-browser compatible code.