1.0.8 • Published 3 years ago

intersection-observer-utility v1.0.8

Weekly downloads
9
License
MIT
Repository
github
Last release
3 years ago

IOU (intersection observer utility)

This plugin observes elements and sets data attributes accordingly to the state. Also, it emits a custom event every time the element comes into the viewport or leaves the viewport.

Getting Started

A simple Demo can be found on codepen: https://codepen.io/cojaco/pen/rNOmPoK

Installing

install via npm

npm i intersection-observer-utility --save

And in your js

import Iou from 'intersection-observer-utility'

document.addEventListener('DOMContentLoaded', () => {
    const myIouInstance = new Iou();

    // initialize
    myIouInstance.init();
});

install it the old way

Just grab the file https://github.com/nico-jacobs/intersection-observer-utility/blob/master/dist/iou.js

and load it before body end tag e.g.

<script src="yourDomain.com/js/iou.js"></script>

than you can initilize it like:

<script>
    document.addEventListener('DOMContentLoaded', function () {

        // create instance of the plugin
        var myIouInstance = new Iou();

        // initialize
        myIouInstance.init();

    });
</script>

Getting Started

If your Plugin is initialized, you need to add a selector to all the HTML elements that you want to observe. The Default selector for that is the data attribute data-iou-trigger.

The plugin then will attach the data attributes accordingly to the position to the viewport.

Data AttributeDefaultOptionsDescription
data-iou-triggeremptynoneMandetory selector for the elements that should be observed
data-iou-targetnot setany selector you wantOptional, this data attribute holds an Selector of elements that should be triggerd
data-iou-relationset by the pluginprev, next, currentMarks currently triggered elements, and also marks sibling elements as previous or next
data-iou-visibleset by the plugintrue, falsShows if element is in viewport or not
data-iou-directionset by the pluginvisible-top,invisible-top, visible-bottom, invisible-bottomvisible-top = element came from topinvisble-top = element left the viewport on the topvisible-bottom = element came from bottominvisble-bottom = element left the viewport on the bottom side

Now you can style the elements, based on their data-attribute values. A Demo will follow soon.

Also, the intersection of elements will emit a custom event on the document, that you can listen to.

Example:

bindListener: () => {


            document.addEventListener('iouOut', ({detail}) => {
                console.log('iou animated out', detail.entryObj.target.targetEls, detail.entryObj);
            });

            document.addEventListener('iouIn', ({detail}) => {
               console.log('iou animated in', detail.entryObj.target.targetEls, detail.entryObj);
            });

    },

Built With

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

Nico Jacobs

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

This Plugin was inspired by AOS Animate On Scroll Library