Skip to content

vessys/scrollReveal.js

 
 

Repository files navigation

#scrollReveal.js A simple way to create and maintain how elements fade in, triggered when they enter the viewport. An open-source experiment from @JulianLloyd.

scrollReveal version


####→ View Demo ←


Installation

Disclaimer: scrollReveal.js is in early development, and breaking changes will likely occur in future updates. Before updating, please refer to the CHANGELOG for details.

Download

scrollReveal.js

GitHub

git clone https://github.com/julianlloyd/scrollReveal.js.git

Bower

bower install scrollReveal.js


Once you have a copy of scrollReveal.js, place it in your JavaScript folder, and include the following JavaScript just before the closing </body> tag.

  <script src='{your_JavaScript_path}/scrollReveal.js'></script>
  <script>

      window.scrollReveal = new scrollReveal();

  </script>

Basic Usage

By adding a data-scroll-reveal attribute to an element, it will automatically be revealed (using default values) as soon as the element is within the viewport:

<!-- Reveal using defaults. -->
<div data-scroll-reveal> Hello world! </div>

But wait! It’s more fun if you define your own reveal animation parameters, which you can do using using natural, declarative language:

<!-- Reveal using custom parameters. -->
<div data-scroll-reveal="enter left and move 50px over 1.33s"> Foo </div>
<div data-scroll-reveal="enter from the bottom after 1s"> Bar </div>
<div data-scroll-reveal="wait 2.5s and then ease-in-out 100px"> Baz </div>

Getting Started

What you enter into the data-scroll-reveal attribute is parsed for specific words:

  • keywords that expect to be followed by a value.

  • fillers as natural language sugar. (optional)

Keywords and Values

These specific keyword / value pairs allow you to describe basic reveal animation behavior.


keyword: enter — Controls the vector origin of your reveal animation.
value: top | right | bottom | left

Example:

<!-- Reveal your element with a downward motion. -->
<div data-scroll-reveal='enter top'> Foo </div>

keyword: move — The distance your revealing element travels.
value: [ integer ]px.

Example:

<div data-scroll-reveal='move 24px'> Bar </div>

keyword: over — The duration of your reveal animation.
value: [ decimal ]s

Example:

<div data-scroll-reveal='over 1.66s'> Baz </div>

keyword: after/wait — The duration before your reveal begins.
value: [ decimal ]s

Example:

<!-- Both are accepted. -->
<div data-scroll-reveal='after 0.33s'> Mel </div>
<div data-scroll-reveal='wait 0.33s'> Mel </div>

####Combining Keyword/Value Pairs You can easily combine the above pairs to create more dynamic reveal animations.

Example:

<div data-scroll-reveal="enter top move 50px"> Foo </div>
<div data-scroll-reveal="enter top move 50px, after 0.3s"> Bar </div>
<div data-scroll-reveal="enter top move 50px, after 0.6s"> Baz </div>
<div data-scroll-reveal="enter top move 50px, after 0.9s"> Mel </div>

Fillers (optional)

You can use conjoining filler words for more readable language.

  • from
  • the
  • and
  • then
  • but
  • with
  • ,

Example:

<!-- These 4 lines are equivalent. -->
<div data-scroll-reveal="wait 0.3s, then enter left and move 40px over 2s"> Foo </div>
<div data-scroll-reveal="enter from the left after 0.3s, move 40px, over 2s"> Bar </div>
<div data-scroll-reveal="enter left move 40px over 2s after 0.3s"> Baz </div>
<div data-scroll-reveal="enter left, move 40px, over 2s, wait 0.3s"> Mel </div>

Advanced Usage

####Custom defaults You can pass an object to the constructor with your desired default configuration.

  <!-- Everything else. -->

  <script src='{your_JavaScript_path}/scrollReveal.js'></script>
  <script>

    // The starting defaults.
    var config = {
            after: '0s',
            enter: 'bottom',
            move: '24px',
            over: '0.66s',
            easing: 'ease-in-out',
            viewportFactor: 0.33,
            reset: false,
            init: true
          };

    window.scrollReveal = new scrollReveal( config );

  </script>
</body>

By default init is set to true, meaning scrollReveal.init() fires on instantiation. (This registers DOM elements and prepares them to reveal)

You may want to set init to false if you’re working with templates or other generated content, letting you control when scrollReveal.init() is first called.

You can also call this method any time afterwards to re-check all elements in the DOM for data-scroll-reveal attributes.

Generated HTML

You may have dynamically generated HTML in your use case, (AJAX, templates, other JavaScript libraries, etc.,) so as mentioned above, the scrollReveal object has the init() helper method that checks for new elements in the DOM.

Example:

  <!-- Everything else. -->

  <script src='{your_JavaScript_path}/scrollReveal.js'></script>
  <script>

      var config = {
              enter: 'bottom',
              move: '40px',
              over: '0.16s',
              reset: true,
              init: false
            };

      window.scrollReveal = new scrollReveal( config );

      // Dummy AJAX return object:
      var data = { newElementHtml: '<div data-scroll-reveal> Foo </div>' }

      var container = document.getElementById('#container');
          container.innerHTML( data.newElementHTML );

     // Now check for new elements in the DOM…
     scrollReveal.init();

  </script>
</body>

Viewport Factor

If set to 0, the element is considered in the viewport as soon as it enters.
If set to 1, the element is considered in the viewport when it is fully visible.

Example:

    var config = {
      viewportFactor: 0.33
    };

    // Your reveal animation triggers after 33% of
    // your element is visible within the viewport.

Replaying animations

Keyword: reset — replay reveal animations every time elements enter the viewport.

<!-- Note: The reset keyword works by itself. -->
<div data-scroll-reveal="reset"> Foo </div>

Controlling Easing

The move keyword can be replaced with any one of the following:

  • ease
  • ease-in
  • ease-out
  • ease-in-out

Example:

<div data-scroll-reveal="after 2s, ease-in 32px and reset over .66s"> Foo </div>

Browser Support

scrollReveal.js does not require jQuery, but does rely on CSS3 transitions to power its reveal animations; it has been developed exclusively for modern browser use only.

Contributions

Community feedback and involvement is highly encouraged.



Special Thanks

scrollReveal.js was inspired by the awesome cbpScroller.js by Mary Lou. Copyright © 2014 Codrops.

Also, a special thanks to Jeff Escalante (Jenius) for setting up the build process, and his ongoing help with testing and the JavaScript API.

License

The MIT License (MIT)

Copyright © 2014 Julian Lloyd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Declarative on-scroll reveal animations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published