VisibilityWatcher 1.3.1

VisibilityWatcher is an event or poll based DOM element visibility detection class.



Details

Author
Francois Cartegnie
Current version
1.3.1
GitHub
fcartegnie/VisibilityWatcher
Downloads
7228
Category
Utilities
Tags
Report
GitHub Issues

Releases


Dependencies

  • _self_/_current_: core/1.2.3: '*'

How to use

new VisibilityWatcher($('target'),
{
    'enteredscreen': function(){
            alert('Element is on screen');
    }
});

Events will happen when the element is scrolled in or out of the viewport, and depends on scroll events. If you need to detect an element when there's no scroll event (no user action, animated by scripts) you can use the poll method.

new VisibilityWatcher($$('.watched'),
{
    'enteredscreen': function(el){
            alert('Element ' + el.id + 'is on screen');
    }
},
{ 'method': 'poll' }
);

You can also discard 'fast scrolling' events by only triggering the event when the element stays in the same state for more than the specified delay value. The delta_px allows extending the detection area. Can be used to trigger the event when the element is close to enter screen.

new VisibilityWatcher($$('.watched'),
{
    'enteredscreen': function(el){
            alert('Element ' + el.id + 'is on screen');
    }
},
{
    'delay': 2000,  /* Only trigger event if the element is on screen for more than 2 seconds */
    'delta_px': 300  /* Assume element is on screen when it enters a 300px range around viewport */
}
);

You can also detect element's position relatively to the viewport.

$('target').store('visibilitywatcher',
    new VisibilityWatcher($('target'),
    {
        'updatedvisibilitystatus': function(){
                var viswatcher = $('target').retrieve('visibilitywatcher');
                alert('we are ' + viswatcher.getVisibility()['x'] + ' element on x-axis and ' + viswatcher.getVisibility()['y'] + ' on y-axis');
        }
    })
);

Return value on of getVisibility() has the same meaning as a fired enteredscreen event. Return values before and after of getVisibility() have the same meaning as a fired leftscreen event.


Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus