GithubHelp home page GithubHelp logo

hoodwink73 / o-element-visibility Goto Github PK

View Code? Open in Web Editor NEW

This project forked from financial-times/o-element-visibility

0.0 2.0 0.0 189 KB

Element visibility tracking

HTML 9.04% JavaScript 88.14% CSS 2.82%

o-element-visibility's Introduction

o-element-visibility Build Status

Utility for tracking element visibility within the viewport.

Methods

o-element-visibility#init(selector)

Tracks the selected elements and reports the percentage of the element currently on the screen, changes to the percentage in view will fire events within the oVisibility namespace (see Events below).

If the selector argument is omitted [data-o-element-visibility-track] is used.

Note: Enabling element tracking will also attach all o-viewport events.

o-element-visibility#destroy()

Stop tracking all currently tracked elements.

Note: This will not stop listening to all the oViewport events.

o-element-visibility#track(element)

Pass an element to be tracked, returns a TrackedElement (see below).

o-element-visibility#updatePositions(force)

The position of an element is cached when we begin tracking, this will recache those values for every element currently being tracked.

Force will force an event to fire even if no change is detected.

Note: During scrolling this method is not run but is run on resize or orientation change

o-element-visibility#update(force)

Updates the current visibility status of all elements and fires events if change are detected.

Force will force an event to fire even if no change is detected.

Properties

o-element-visibility#tracked

An array of the currently tracked elements

TrackedElement

An instance of TrackedElement is returned by the track method. The instance contains information about the position of the element in the page and methods to calculate it's visibility:

inview: Boolean // the elements current visibility within the viewport
percentage: Number // the percentage of the element in view
rect: Object // the result of the last getBoundingClientRect run on this element
width: Number // the width of the element
height: Number // the height of the element
top: Number // the absolute top of the element
left: Number // the absolute left of the element
bottom: Number // the absolute bottom of the element
right: Number // the absolute right of the element
area: Number // the area of the element

updatePosition(force)

returns this

Same as above but for this TrackedElement only. Force will force an event to fire even if no change is detected.

update(force)

returns this

Same as above but for this TrackedElement only. Force will force an event to fire even if no change is detected.

inViewport()

returns this

Test if the element is currently in the viewport

percentInViewport()

returns this

Calculate the percentage of the element in the viewport

Events

Each of these custom events are fired on document.body. For each custom event event.detail.originalEvent contains a reference to the original browser event.

Additional properties in event.detail are detailed below:

oVisibility.visible

element: HTMLElement // or Node in < ie10
inviewport: Boolean // if the element is in the viewport
type: ['visibility', 'percentage', 'update'] // the reason the event fired, either because of visibility or percentage change, update means the function was run with force
percentInView: Number //  the percentage of the element that is visible

Note: inviewport can be true while percentage is 0, this means the element has a width or height of 0 but is within the viewport

o-element-visibility's People

Contributors

expe avatar kaelig avatar robinmarr-ft avatar

Watchers

 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.