smartscroll is a tiny (1815b minfied + gzipped) jQuery plugin that has these independent features:
It also supports:
Install lethargy, which is available as a Bower package, which you can install with bower install
.
Structure your HTML like so (default options included)
<body>
<div class="section-wrapper">
<div class="section" data-hash="section-hash-name"></div>
<div class="section" data-hash="section-hash-name"></div>
</div>
<script src="https://github.com/d4nyll/smartscroll/raw/master/path/to/lethargy.min.js">
<script src="https://rawgit.com/Olical/EventEmitter/master/EventEmitter.min.js"></script>
<script src="https://github.com/d4nyll/smartscroll/raw/master/path/to/smartscroll.min.js">
<script>
var options = {
mode: "vp", // "vp", "set"
autoHash: false,
sectionScroll: true,
initialScroll: true,
keepHistory: false,
sectionWrapperSelector: ".section-wrapper",
sectionClass: "section",
animationSpeed: 300,
headerHash: "header",
breakpoint: null,
eventEmitter: null,
dynamicHeight: false
};
$.smartscroll(options);
</script>
</body>
You may also want to link to
styles.css
, but all that does is to ensure thehtml
andbody
elements have no margins nor paddingYou may also leave out lethargy, but smartscroll may not work as well with scroll devices that uses inertial scrolling. Performance with lethargy can be slower, but it will be improved with further development.
mode
- (String) Valid options are:
vp
(Viewport) - The sections will be automatically sized to be the same as the viewportset
- Use the height and width set by CSS (use this for having different heights for different sections)autoHash
- (Boolean) Whether the auto-hashing feature is enabled. If you use this feature, it is important to use EventEmitter as well, otherwise smartscroll will listen to every scroll
event, which is very resource-draining and can make the animation more 'glitchy'sectionScroll
- (Boolean) Whether the section-scrolling feature is enabledinitialScroll
- (Boolean) Whether smartscroll should scroll to the position specified by the hash on initial loadkeepHistory
- (Boolean) Whether scrolling through different sections will be recorded in the browser's historysectionWrapperSelector
- (String) The CSS selector for the block element which wraps around your sectionssectionClass
- (String) The class name applied to each sectionanimationSpeed
- (Integer) Time taken for the scroll animation, in milisecondsheaderHash
- (String) The hash for the section above the sections, must be non-empty to reliably ensure the page do not jump when updating the hash value across browsers (as #
means _top
)breakpoint
- (Integer) The width of the browser below which scrolljacking will be disabledsectionSelector
- (String) The selector applied to each section, overrides sectionClass
and allow more flexibility in choosing a selector. (Added in 2.1.0)dynamicHeight
- (Boolean) If you are going to be dynamically adding and removing content so as to change the position and/or size of the section wrappers and/or sections, then set this to true. Set to false otherwise to increase performance.eventEmitter
- (Object) If you pass in an EventEmitter object, autoHashing will be much more efficient. You can also listen to the scroll events this way. Defaults to null
.ie8
- If you need to support Internet Explorer 8, change this to true
. Defaults to false
.bindSwipe
- (Boolean) Allow for listening of swipe events. Requires EventEmitter. Defaults to true
bindKeyboard
- (Boolean) Allow for keyboard events (up and down arrows) to control slides. Defaults to true
Smartscroll has a soft dependency on EventEmitter. If present, smartscroll can provide six events scrollStart
, scrollEnd
, swipeLeft
, swipeRight
, swipeDown
and swipeUp
. The scrollStart
and scrollEnd
listener will receive the slide number as its only argument; the first slide will have a number of 1
, the section before the sectionWrapper will have a number of 0
.
var ee = new EventEmitter();
var scrollStartListener = function (slideNumber) {
console.log("Scrolling to " + slideNumber);
}
var scrollEndListener = function () {
console.log("Scrolling End");
}
ee.addListener('scrollStart', scrollStartListener);
ee.addListener('scrollEnd', scrollEndListener);
$.smartscroll({
...
eventEmitter: ee
});
The other listeners receive no arguments.
var ee = new EventEmitter();
var swipeUpListener = function () {
console.log("Swiping Up");
}
var swipeDownListener = function () {
console.log("Swiping Down");
}
$.smartscroll({
...
eventEmitter: ee,
bindSwipe: true
});
ee.addListener('swipeUp', swipeUpListener);
ee.addListener('swipeDown', swipeDownListener);
You can manually scroll up or scroll down.
When you first initiate smartscroll with $.smartscroll()
, it returns an object. In that object is the scroll
function, which is called with a single parameter. If the parameter is truthy, it will scroll down, otherwise, it will scroll up.
var smartscroll = $.smartscroll();
smartscroll.scroll(1);
Currently, there are two features of smartscroll, and this is how it's implemented:
Smooth scroll by section
The mousewheel
and DOMMouseScroll
(for Firefox) events are bound.
When such event is fired in vp
mode, smartscroll will find it's most prominent section (one which occupies most of the screen), and smoothly scroll to the section above or below it.
When the event is fired in set
mode, smartscroll will find which section occupies the middle of the screen, and smoothly scroll to the section above or below it.
When scrolling outside of the sections, normal scrolling resumes.
Changing URL hash based on the current section
The scroll
event is bound.
When the event is fired in vp
mode, smartscroll will see which section occupies the top of the viewport, and if the hash is different, replace it with the new one.
When the event is fired in set
mode, smartscroll will see which section occupies the middle of the viewport, and if the hash is different, replace it with the new one.