3.2.8 • Published 4 years ago

loco-scroll v3.2.8

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

Installation

npm install loco-scroll

Usage

import LocomotiveScroll from "loco-scroll";

const scroll = new LocomotiveScroll();
<h1 data-scroll>Hello</h1>
<p data-scroll>Text</p>

With smooth scrolling

import LocomotiveScroll from "loco-scroll";

const scroll = new LocomotiveScroll({
    el: document.querySelector("#js-scroll"),
    smooth: true
});
<h1 data-scroll data-scroll-speed="1">Hello</h1>
<p data-scroll data-scroll-speed="2">Text</p>

With methods

import LocomotiveScroll from "loco-scroll";

const scroll = new LocomotiveScroll();
const target = document.querySelector("#js-target");

scroll.scrollTo(target);

With events

import LocomotiveScroll from "loco-scroll";

const scroll = new LocomotiveScroll();

scroll.on("call", func => {
    // Using modularJS
    this.call(...func);
    // Using jQuery events
    $(document).trigger(func);
    // Or do it your own way 😎
});
<!-- Using modularJS -->
<div data-scroll data-scroll-call="function, module">Trigger</div>
<!-- Using jQuery events -->
<div data-scroll data-scroll-call="EVENT_NAME">Trigger</div>
<!-- Or do it your own way 😎 -->
<div data-scroll data-scroll-call="{y,o,l,o}">Trigger</div>

Instance options

OptionTypeDefaultDescription
elobjectdocumentScroll container element.
namestring'scroll'Data attribute prefix (data-scroll-xxxx).
offsetarray0In-view trigger offset.
repeatbooleanfalseRepeat in-view detection.
smoothbooleanfalseSmooth scrolling.
smoothMobilebooleanfalseSmooth scrolling on iOS and Android devices.
directionstringverticalScroll direction.
inertianumber1Lerp intensity.
getDirectionbooleanfalseAdd direction to scroll event.
getSpeedbooleanfalseAdd speed to scroll event.
classstringis-inviewElement in-view class.
initClassstringhas-scroll-initInitialize class.
scrollingClassstringhas-scroll-scrollingIs scrolling class.
draggingClassstringhas-scroll-draggingIs dragging class.
smoothClassstringhas-scroll-smoothHas smooth scrolling class.
scrollbarClassstringc-scrollbarScrollbar element class.
firefoxMultipliernumber50Boost scrolling speed of Firefox on Windows.
touchMultipliernumber2Mutiply touch action to scroll faster than finger movement.

Element attributes

AttributeValuesDescription
data-scrollDetect if in-view.
data-scroll-sectionDefines a scrollable section. Splitting your page into sections may improve performance.
data-scroll-classstringElement in-view class.
data-scroll-offsetstringElement in-view trigger offset (ex.: "10", "100,50%", "25%, 15%").
data-scroll-repeattrue, falseElement in-view detection repeat.
data-scroll-callstringElement in-view trigger call event.
data-scroll-speednumberElement parallax speed. A negative value will reverse the direction.
data-scroll-targetstringTarget element's in-view position.
data-scroll-positiontop, bottomWindow position of in-view trigger.
data-scroll-directionvertical, horizontalElement's parallax direction.
data-scroll-delaynumberElement's parallax lerp delay.
data-scroll-stickySticky element. Starts and stops at data-scroll-target position.

Instance methods

MethodDescriptionArguments
init()Reinitializes the scroll.
on(eventName, function)Listen instance events ⬇.
update()Updates all element positions.
destroy()Destroys the scroll events.
start()Restarts the scroll events.
stop()Stops the scroll events.
scrollTo(target, offset)Scroll to an element.target: dom object, selector string, top or bottom offset: number

Instance events

EventArgumentsDescription
scrollobjReturns scroll instance (position, limit, speed, direction).
callfuncTrigger if in-view. Returns your string or array if contains ,.

Dependencies

NameDescription
Virtual ScrollCustom scroll event with inertia/momentum.
modularScrollElements in viewport detection. Forked from it, not a dependency.

Browser support

Works on most modern browsers. Chrome, Firefox, Safari, Edge...

To get IE 11 support, you need polyfills. You can use your own or include these before this script.

<script
    nomodule
    src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.6.0/polyfill.min.js"
    crossorigin="anonymous"
></script>
<script
    nomodule
    src="https://polyfill.io/v3/polyfill.min.js?features=Object.assign%2CElement.prototype.append%2CNodeList.prototype.forEach%2CCustomEvent%2Csmoothscroll"
    crossorigin="anonymous"
></script>