Coder Social home page Coder Social logo

jedwards1211 / smoothscroll Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alicelieutier/smoothscroll

3.0 3.0 1.0 102 KB

A teeny tiny smooth scroll script with ease-in-out effect and no jQuery.

License: MIT License

HTML 96.77% JavaScript 3.23%

smoothscroll's Introduction

smoothScroll

A teeny tiny, standard compliant, smooth scroll script with ease-in-out effect and no dependancy.

smoothScroll will tie all your internal links to a handler that will produce a smooth scroll to their target instead of an instant jump. It also returns an API that you can use to call a smooth scroll yourself.

This works in Firefox, Chrome, IE10, Opera and Safari. Unsupported browsers would just use the normal internal link behaviour.

How to use

Just include smoothscroll inside your page, like this:

<script type="text/javascript" src="path/to/smoothscroll.min.js"></script>

If you want all links to be automatically tied to smooth scroll, call the following: window.smoothScroll.attachToAllLinks()

If you want to call a smooth scroll from your code, you can now use the API by calling:

window.smoothScroll(target, duration, callback, context)

where:

  • target is a HTMLElement Object from your document that you want to scroll to, or a numeric position on the page
  • duration is the total duration of the scroll (optional, defaults to 500ms)
  • callback is a function to be executed when the scrolling is over (optional)
  • context is the scrolling context (optional, defaults to window, can be any HTMLElement Object)

Alternatively, you can install smoothscroll as a dependency using npm:

npm install --save smoothscroll

Example usage as a module, binding to a custom element:

var smoothScroll = require('smoothscroll');

var exampleBtn = document.querySelector('.example-button');
var exampleDestination = document.querySelector('.example-destination');

// This function can easily be an onClick handler in React components
var handleClick = function(event) {
  event.preventDefault();

  smoothScroll(exampleDestination);
};

exampleBtn.addEventListener('click', handleClick);

smoothscroll.js

Here are some indications if you want to tweak the code to fit your needs:

There is an ease-in-out type timing function. You can change it quite easily in the code. Here is where I found the one I use:

You can also change the default duration of a scroll, which is 500ms by default.

My code is heavily commented so you shoudn't lose yourself too much.

Example

The example.html file is basically the script applied to a w3c page. I just changed the style so the table of content is fixed to the left.

Check out the result. Wouldn't it be great if all w3c specs where that easy to navigate in?

Similar scripts

While I was looking for a name for this script, I found these sites. If this script is not what you need, you might have more luck there:

License

This library is released under the MIT license.

smoothscroll's People

Contributors

alicelieutier avatar t1mmen avatar afmklk avatar ahoym avatar 7cc avatar jedwards1211 avatar kennethormandy avatar shlajin avatar wridgers avatar

Stargazers

 avatar anubhav mishra avatar David Bismut avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

superhuman

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.