3.7.6 • Published 4 months ago

@slidy/core v3.7.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

npm version npm bundle size npm downloads github issues npm license

@slidy/core

Simple, configurable, nested & reusable sliding action script.

Сompletely mimics the behavior of a native scroll with mouse drag, index navigation, acceleration, gravity, easings, custom animations & infinite loop mode.

Try the DEMO

Getting started

The package is available via NPM:

npm i -D @slidy/core

or from CDN:

<script src="https://unpkg.com/@slidy/core"></script>

Playground is available in svelte REPL.

Usage

Function slidy(node, options) is available via named import as MJS/CJS module or via global Window.Slidy object props as IIFE.
All options are optional, but mount node required:

MJS/CJS module import

<head>
   <script type="module">
        import { slidy } from 'https://unpkg.com/@slidy/core/dist/index.mjs'; // MJS module
        // OR
        import { slidy } from 'https://unpkg.com/@slidy/core/dist/index.cjs'; // CJS module

        slidy(node)
    </script>
</head>

<section>
    <ul id="node">
        <li>...</li>
        ...
    </ul>
</section>

IIFE as Window Object

window.Slidy object contain core script, @slidy/animation, @slidy/easing, @slidy/plugins & @slidy/media functions

window.Slidy = {
    animation, // animation functions
    core, // core script
    easing, // easing functions
    media // global media store
}
<head>
    <script src="https://unpkg.com/@slidy/core/dist/index.js"></script>
</head>

<script>
    let slidy = null,
        animation = null,
        easing = null,
        node = document.querySelector('#slidy'),

        window.onload = () => {
            animation = Slidy.animation.flip
            easing = Slidy.easing.cubic
            plugin = Slidy.plugin.play
            slidy = Slidy.core(node, { animation, easing, plugins: [ plugin() ] });
        }
</script>

<section>
    <ul id="slidy">
        <li>...</li>
        ...
    </ul>
</section>

As third party module in any frameworks

<!-- Svelte -->

<script>
    import { slidy } from '@slidy/core';
</script>

<section>
    <ul use:slidy>
        <li>...</li>
        ...
    </ul>
</section>

Options

@slidy/core does not style the DOM, but uses the browser render, except for 4 rules for target node: outline: none, overflow: hidden, user-select: none; -webkit-user-select:none;.

If you need reversed flow use css rules on target node, like: flex-flow: row-reverse / column-reverse, direction: rtl or html attribute dir="rtl".

If you need keyboard navigation just set tabindex=0 on target node.

⚠️ Don't positioning childs absolute, becouse @slidy use coordinates from childNodes. For deck flow use options.snap: 'deck'.

KeyDefaultTypeDescription
index0numberSliding index
position0numberSliding position. Setter working only in snap: undefined mode.
clamp0numberClamping sliding by index: clamp - index + clamp
indent1numberSliding indent: part of gap padding both start/end edges of slide gap * indent
sensity2.5numberSliding sensity: how many pixels to drag in the RAF ~16ms to start move, 0 when sliding
gravity1.2numberSliding gravity: 0(space) ~ 1(eath) ~ 2(underground)
duration450numberSliding duration in ms
animationundefinedfunctionAnimation function: AnimationFunc = (args: AnimationArgs) => Styles - predefined in @slidy/animation.
easingundefinedfunctionEasing function: t value from 1 to 0 - predefined in @slidy/easing. For proper operation minimal duration: 450 needed.
pluginsundefinedfunctionPlugins functions: t value from 1 to 0 - predefined in @slidy/plugins.
snapundefinedstringSnapping side: 'start', 'center', 'end', 'deck', undefined. Default clamp sliding by edges.
axisundefinedstringControl coordinate axis: 'x', 'y'.
loopfalsebooleanInfinite loop mode

Internal calculated - readonly

KeyTypeDescription
directionnumberChildren move direction: -1 <- 0 -> 1
reversenumberChildren reverse flow: -1 or 1
verticalnumberChildren vertical flow
scrollablenumberChildren full size with gaps > target node size
edgedbooleanScroll position on one of the edges

Usage

<head>
    <script type="module">
        import { slidy } from 'https://unpkg.com/@slidy/core/dist/index.mjs';
        import { linear } from 'https://unpkg.com/@slidy/easing/dist/index.mjs'
        import { fade } from 'https://unpkg.com/@slidy/animation/dist/index.mjs'

        const options = {
                index: 0,
                clamp: 0,
                indent: 1,
                sensity: 5,
                gravity: 1.2,
                duration: 375,
                animation: fade,
                easing: linear,
                snap: 'center',
                axis: 'x',
                loop: false,
            }

        slidy(node, options)
    </script>
</head>

<section>
    <ul id="node">
        <li>...</li>
        ...
    </ul>
</section>

Events

Slidy instance reinit on every change childNodes.length in on:mutate event.

NameDetailDescription
mount{options}Fires when node.children.length & node.children isConnected
resize{ROE,options}Fires on resize target node ROE: ResizeObserverEntry[]
mutate{ML,options}Fires on mutation childNodes in target node ML: MutationRecord[]
move{index,position}Fires on any sliding
index{index}Fires on each index change: index === changed.index
keys{e.key}Fires if target node focusing and any key pressed. Predefined keys: ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'] is defaultPrevented & navigate to(index + options.clamp). Focus not in core & can do programaticaly or with tabindex attribute on target node.
update{updated.options}Fires on each options update
destroy{node}Fires when destroy() is done or before target node unmounted from the DOM

Usage

<head>
    <script type="module">
        import { slidy } from 'https://unpkg.com/@slidy/core/dist/index.mjs';

        slidy(node)

        node.addEventListener('mount', (e) => console.log(e))

        node.onupdate = (e) => console.log(e.detail)

        function onMove(e) {
            const { index, position } = e.detail
            console.log(index, position)
        }
    </script>
</head>

<section>
    <ul id="node" onmove="onMove" tabindex="0">
        <li>...</li>
        ...
    </ul>
</section>

Methods

NameArgumentsDescription
to()(index, position)Scroll to index or position (only snap: undefined)
init()(node)Init slidy() instance
update()({option:value})Update any property in options
destroy()()Remove event listners, observers & defaulted props on slidy() instance

Usage

<head>
    <script type="module">
        import { slidy } from 'https://unpkg.com/@slidy/core/dist/index.mjs';

        slidy(node)

        slidy.update({ snap: 'center' })
        
        prev.onclick = () => slidy.to(index - 1)
        next.onclick = () => slidy.to(index + 1, 100)
    </script>
</head>

<section>
    <ul id="node">
        <li>...</li>
        ...
    </ul>
</section>

<nav>
    <button id="prev">←</button>
    <button id="next">→</button>
</nav>

<!-- if slidy() in global scope you can use global event handlers as atributes -->
<nav>
    <button onclick="slidy.to(index - 1)">←</button>
    <button onclick="slidy.to(index + 1)">→</button>
</nav>

MIT © Valexr

3.7.6

4 months ago

3.7.5

4 months ago

3.7.4

12 months ago

3.7.3

1 year ago

3.7.2

1 year ago

3.7.1

1 year ago

3.7.0

2 years ago

3.6.0

2 years ago

3.5.3

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.4.8

2 years ago

3.4.7

2 years ago

3.4.6

2 years ago

3.4.5

2 years ago

3.4.9

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.3.9

2 years ago

3.3.8

2 years ago

3.2.9

2 years ago

3.3.7

2 years ago

3.2.8

2 years ago

3.1.9

2 years ago

3.3.6

2 years ago

3.2.7

2 years ago

3.1.8

2 years ago

3.4.0

2 years ago

3.3.1

2 years ago

3.2.2

2 years ago

3.3.0

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.3.5

2 years ago

3.2.6

2 years ago

3.1.7

2 years ago

3.3.4

2 years ago

3.2.5

2 years ago

3.1.6

2 years ago

3.3.3

2 years ago

3.2.4

2 years ago

3.1.5

2 years ago

3.3.2

2 years ago

3.2.3

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.4

2 years ago

3.0.5

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago