Giter VIP home page Giter VIP logo

scrolltrigger's Introduction

ScrollTrigger

Triggers classes on html elements based on the scroll position

How to use?

It's quite simple, just add the ScrollTrigger.min.js file to your HTML page. Then call ScrollTrigger.init() when the page has loaded. Like so:

<script src="ScrollTrigger.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(){
  ScrollTrigger.init();
});
</script>

The init call takes 2 parameters, the bindTo and scrollIn parameters. These are optional.

The bindTo parameter is the HTML element where the triggers should be fetched from, this usually is document.body but could specify to a certain element.

The scrollIn parameter is the element to get the scroll position from, by default this is window, but it could be a scrolling div etc.

Now add the data-scroll attribute to the HTML element you want to animate:

<div data-scroll></div>

When you scroll the page, and the element is visible in the viewport, it will add the default class visible. If it's out of the viewport the invisible class is added. You can specify options in the data-scroll attribute, it can take up to 4.

<div data-scroll="150 animateIn animateOut true"></div>

The first option 150 is the offset to add to the position, in this case, it will start 150 pixels after it has been in the viewport. This is nice for tweaking animations to trigger at the right time.

The second and third options are the classes to add when visible/invisible.

The fourth and last option is a boolean, if true it'll add the element's height to the scroll offset. Now it will only add the animateIn class when the element is completely in the viewport.

What about CSS?

You can find examples on how to use the css classes to animate in the gh-pages branch. Checkout index.html and style.css for some basic animations.

Custom animations

If you want to add custom animations based on the scroll position, it would be a waste to start another loop / jack the onscroll function. That's why you can attach callbacks to the ScrollTrigger's loop. This is really simple:

document.addEventListener('DOMContentLoaded', function(){
  ScrollTrigger.init();
  ScrollTrigger.attach(function(height, scrollPos){
    // i can do anything now with the height of the viewport
    // or the scrollPosition in the scrollElement. 'this' refers to
    // the ScrollTrigger object.
  });
});

Contributing

Fork, check out ScrollTrigger.js and enjoy!

scrolltrigger's People

Contributors

terwanerik avatar

Watchers

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