Git Product home page Git Product logo

simplesmoothscroll's Introduction

simplesmoothscroll

npm version Build Status

A module to simply scroll to an element on the page in a buttery smooth fashion

  • No dependencies
  • CommonJS, AMD, and Browser ready

What is it?

Simple smooth scroll takes in a dom element and scrolls to that element in an ease-in-out fashion. It can work on elements that are above the current window position or below the current window position

Installation

npm install simplesmoothscroll --save

Usage

var smoothScroll = require("simplesmoothscroll");

// Passing in no params scrolls you to the top of the page
smoothScroll();

// Passing an element as a param scrolls you to that element
var myHeader = document.querySelector("h2");
smoothScroll(myHeader);

// You can also include an offset so there is some room between
// the top of the viewport and the element
smoothScroll(myHeader, {
  offset: 10
});

/**
 *  If you want to execute a function when the scroll finishes, you can
 *  pass in an onScrollFinished callback. A "cancelled" param is passed in
 *  as an arg in case the scroll was cancelled due to user interaction
 */
smoothScroll(myHeader, {
  offset: 10,
  onScrollFinished: function(cancelled) {
    // Your function
  }
});

/**
 * If you want your own easing function instead of the default trig-based
 * ease-in-out function, you can pass in a getEasingFunction parameter that
 * should return your custom easing function. "getEasingFunction" is passed in
 * the targetPosition (distance the target is from the viewport)
 * and the returned function will be passed a "stepCount" argument that
 * represents the number of frames since the scroll started.
 * The easing function should return a value between 0 and 1 where 0
 * will scroll to the initial position and 1 will scroll to the target.
 * Below is an example of using a custom linear scroll
 */
smoothScroll(myHeader, {
  getEasingFunction: function(targetPosition) {
    return function(stepCount) {
      return stepCount * 0.01;
    };
  }
});

API

smoothScroll(element, options)

  • element [optional] - a dom element to scroll to. If not provided, smoothScroll will scroll to the top of the page
  • options [optional] - option parameters for the smooth scroll
    • offset - A margin between the target element and the top of the viewport
    • onScrollFinished - A function to execute when the scroll finishes. It is passed a "cancelled" argument in case the scroll did not finish due to user interaction with the page.
    • getEasingFunction - A function that should return your custom easing function if you do not wish to use the default easing function. This is passed in a targetPosition argument. See usage for an example

Contributing

My goal here is to make the scroll as performant and smooth as possible. If there are optimizations that can be made then feel free to open an issue or submit a pull request.

License

Released under the MIT License

simplesmoothscroll's People

Contributors

dependabot[bot] avatar jljorgenson18 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nottherealtwch

simplesmoothscroll's Issues

Add more descriptive variable names

When I initially wrote this, I used some pretty crappy variable names that weren't very descriptive. Time to simplify everything and have a more descriptive flow.

Add greater support for bottom of page scrolling

When scrolling to an element on the page, there is a currently a check to see if the bottom of the window has been hit. As of right now, hitting the bottom is not smooth because sss thinks it still needs to scroll to that target element. The distance between the bottom of the page, the target element, and the window height should all be factored in to find the appropriate distance.

Abstract easing function

As of right now, the math is somewhat unclear. The easing function using trig should be abstracted out.

Add amd and basic window support

Add the ability to load in smoothScroll in a way that doesn't require a bundler. Ultimately, all of the utility functions should be in an anonymous function as not to invade the global scope and then the main smoothScroll function should be exported.

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.