Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Simple javascript class allowing to detect position of element in the viewport. Repaint and reflow are limited to the max in order to limit the performance impact with other librairies

Notifications You must be signed in to change notification settings

20minutes/checkvisibility

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Checkvisibility

Basically CheckVisibility allows to detect vertical positioning of an element in the viewport. It takes only one param: the DOM elem, you want to track.

Such as:

var checkVisibility = new CheckVisibility(elem);

Methods available in the object

  • checkVisibility.inView(y): y is a value between 0 and 1. the method return true if at least "y x 100" (refers to y value) of the element is visible in the viewport.
  • checkVisibility.fromBottom(): return the distance of element from the bottom of the viewport.
  • checkVisibility.fromTop(): return the distance of element from the top of the viewport.
  • checkVisibility.viewportTop(): return window.scrollTop()
  • checkVisibility.viewportBottom(): return window.scrollTop() - this.height()
  • checkVisibility.percentageScrolled(): return the percentage of the page that has been scrolled
  • checkVisibility.updatePosition(): useful to call this in order to refresh calculation after a change that impact our element positioning): window.addEventListener('resize', function(){checkVisibility.updatePosition();});
  • checkVisibility.measure(): to calcul delta and bounds

This lib can be used to creates modles such as: sticky elem, parallax effect or anything that rely on detecting positions. Check the demo for a sticky elem

Nothing is bind in the function, so it's totally free to use with scroll, resize…

var checkElem = new CheckVisibility(document.querySelector('.my-elem'));

window.addEventListener('scroll', function(){
    console.log(checkElem.inView());
});

About

Simple javascript class allowing to detect position of element in the viewport. Repaint and reflow are limited to the max in order to limit the performance impact with other librairies

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%