Giter Club home page Giter Club logo

smoothscroll's Introduction

SmoothScroll.js

Watch the demo! https://codepen.io/liron42/full/bYXeKj/

A small lightwaight (2.66 KB) library to implement browser smooth scroll without jquary, and with easy settings

You can clone this repo and use the SmoothScroll.dist.min.js file, or you can use the rawgit cdn which serve the file directly from this repository:

<script src="https://cdn.rawgit.com/liron-navon/SmoothScroll/master/SmoothScroll.dist.min.js"></script>

It is very easy to setup by just calling:

let smoothScroll = new SmoothScroll(); //setup smooth scroll
scroller.listenToAllInternalLinks(); // override all href links with internal id (#) to use smooth scroll

And in the html just use

<a href="#myInternalLinkId">go to some internal link smoothly</a>

Or just use it from your js code with any framework like so:

smoothScroll.scrollToElement(document.getElementById('targetId')) // vanila
smoothScroll.scrollToElement(this.$refs.myRefName) // vue
smoothScroll.scrollToElement(this.myViewChild.nativeElement) // angular

SmoothScroll will work out of the box without any configuration, you may pass no object to the constructor, but you can also configure it. In order to configure the smooth scroll you can pass an object to the constructor (this are the default settings also seen in the demo):

let smoothScroll = new SmoothScroll({
        el:'html', // an html element to use as a scroll medium (for things like scrolling inside a div)
        scrollTime: 1000, // applied only when using time calculation - the time for each scroll in milliseconds (from current position to the target's position)
        targetHeightPercentageFromTop: 50, /* percentage from top for the element to be: 100 will make it scroll until the  element is at the bottom of the screen, and 50 will make it scroll until the element is at the center of the screen */
        calculation: 'time', //calculation is by either time, or a pace with fixed scroll length per frame
        stepSize: 1 // applied only when using fixed calculation - the number of pixels to traverse per frame
    });

calculation can be either 'fixed' or 'time'.

when using calculation:'fixed', the scrollTime is not accounted, and when using calculation:'time' the stepSize is ignored (it's being calculated to fit the time frame)

Lifecycle hooks

you can hook to when a scroll started and ended, obviously SmoothScroll wont produce onStartScroll when you call it programmatically, but will produce onScrollStart event:

smoothScroll.scrollToElement(document.getElementById('point'), function(){
        console.log("button scroll ended")
})

A more usefull scenario is using the hooks when calling listenToAllInternalLinks:

smoothScroll.listenToAllInternalLinks(onScrollEnd,onScrollStart);

function onScrollStart(targetElement , clickEvent) {
        console.log(`Going to element: ${targetElement.id}`);
}
function onScrollEnd(targetElement){
        console.log(`Got to element: ${targetElement.id}`);
}

smoothscroll's People

Contributors

liron-navon avatar

Watchers

James Cloos 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.