3.1.5 • Published 4 years ago

@deli/locomotive-scroll v3.1.5

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

Installation

npm install locomotive-scroll

Usage

import LocomotiveScroll from 'locomotive-scroll';

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

With smooth scrolling

import LocomotiveScroll from 'locomotive-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 'locomotive-scroll';

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

scroll.scrollTo(target);

With events

import LocomotiveScroll from 'locomotive-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.

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.