Git Product home page Git Product logo

emergence.js's Introduction

Emergence.js

Emergence.js is a lightweight (<3KB), high-performance and dependency-free JS utility (currently beta) for manipulating elements when they appear in the viewport. It leverages HTML5 data-* attributes for ease, and works in IE8+.

View Demo

Get Started

The fastest way to get started is to reference to emergence.js just before your closing </body> tag, and then simply call emergence.init.

<script src="path/to/emergence.min.js"></script>
<script>
	emergence.init();
</script>

You can grab the latest code from the following locations:

How To Use

Add data-emergence="hidden" to any element you wish to watch:

<div class="element" data-emergence="hidden"></div>

When the element becomes visible within the viewport, the attribute will change to data-emergence="visible". Now you can leverage CSS, for example, to animate the element:

.element[data-emergence=hidden] {
    /* Initial state */
}
.element[data-emergence=visible] {
    /* Do something */
}

Custom Options

Emergence.js has a number of options you can customize. Below are the defaults:

<script>
	emergence.init({
		container: null,
		throttle: 250,
		reset: true,
		handheld: true,
		elemCushion: 0.15,
		offsetTop: 0,
		offsetRight: 0,
		offsetBottom: 0,
		offsetLeft: 0,
		callback: function(element, state) {
		    if (state === 'visible') {
		      console.log('Element has emerged.');
		    } else {
		      console.log('Element has reset.');
		    }
		}
	});
</script>

Options Explained

container

It's possible to provide a custom container from within the DOM. For example, document.querySelector('.wrapper');. By default, null will pass <html> as the container.

throttle

Throttle is a method that prevents performance issues associated with scroll and resize events. The throttle will create a small timeout and steadily check element visibility every set amount of milliseconds during the event. The default is 250.

reset

Determines whether the data-attribute state will reset after it's been revealed. Set to false to prevent the reset.

handheld

Don't want this utility to run on handheld devices such as iPhones, iPads, and Androids? Set this option to false.

elemCushion

The element cushion will determine how much of the element needs to be within the viewport to count as "visible". The default value of 0.15 incidates 15% of the element needs to be in the viewport.

offsetTop, offsetRight, offsetBottom, offsetLeft

Provide an offset on any edge of the viewport. This is useful if you have a fixed component such as a header, for which you can offset the same value as the height of the header.

callback

Useful for providing callbacks on when an element has become visible, and when it resets.

emergence.js's People

Contributors

xtianmiller avatar watermelonpizza avatar

Watchers

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