Coder Social home page Coder Social logo

native-smooth-scroll's Introduction

Native smooth scroll

Initializing

import {SmoothScroll} from './src/native.smooth.scroll.js';

const smoothScroll = new SmoothScroll({
    container: document.querySelector(".container"),
});

In a browser, you can use the UMD files located in the dist directory:

<script src="dist/native.smooth.scroll.umd.js"></script>
const smoothScroll = new SmoothScroll({
    container: document.querySelector(".container"),
});

Parameters

Here are the basic parameters that you can specify:

Parameter Type Default Description
container HTML node document.body container that will be fixed and translated to according scroll values
inertia float 0.1 Easing value
threshold float 0.5 Threshold to stop the easing animation, in pixels
useRaf bool false Whether to use the built-in requestAnimationFrame callback.
const smoothScroll = new SmoothScroll({
    // container that will be translated
    container: document.getElementById("content"),
    // round the threshold to 1 pixel
    threshold: 1, 
    // use built-in raf loop
    useRaf: true
});

Methods

Name Description
resize Should be called in a window resize event to update the values
update Should be called to update internal values when the document height changed
scrollTo Immediately scroll to a defined position
render Update our current scroll and velocity values, translate the container and emit our onScroll event. Should be called at each tick of a requestAnimationFrame callback if useRaf is set to false
const smoothScroll = new SmoothScroll({
    // container that will be translated
    container: document.getElementById("content"),
    // round the threshold to 1 pixel
    threshold: 1, 
    // use built-in raf loop
    useRaf: true
});

window.addEventListener("resize", () => {
    smoothScroll.resize();
});

onScroll callback

const smoothScroll = new SmoothScroll({
     // container that will be translated
     container: document.getElementById("content"),
     // round the threshold to 1 pixel
     threshold: 1, 
     // use built-in raf loop
     useRaf: true
});

smoothScroll.onScroll((scroll) => {
    // an object containing the current scroll value, the target scroll value and the velocity value
    console.log(scroll);
});

native-smooth-scroll's People

Contributors

martinlaxenaire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.