element-in-view is a js library that gives you information if an element is visible or not, it's compatible with elements with a css overflow, and a lot of options !).
Demo
npm link
Installyarn add element-in-view-rgermain//ornpm install element-in-view-rgermain
Cdn
<script src="https://unpkg.com/element-in-view-rgermain@1.4.0/dist/index.js">
If you use with cdn, you can find the library on window.elementInView
.
windowselementInViewallelement options // orwindowselementInViewleftelement optionswindowselementInViewtopelement options //... ect
Usage
// es6 import;// or es5const elementInView = ; const el = document; if elementInViewallel console; else console; if elementInViewrightel console; else console; /* scrollIntoView accept 2 arguments first: the element , required ! seconde: objects for options*/const parrentElement = document;elementInViewallelement parent: parrentElement offsetRight: 50 offsetLeft: 10 offsetY: -20;
API
elementInView.function(element, [options])
function
all
function: elementInView.all()
all corner of element need to be visible
right
function: elementInView.right()
only right corner of element need to be visible
left
function: elementInView.left()
only left corner of element need to be visible
top
function: elementInView.top()
only top corner of element need to be visible
bottom
function: elementInView.bottom()
only bottom corner of element need to be visible
options
Type: Object
partial
Type: Boolean
Default: false
the element must be partly visible
parent
Type: HtmlElement
Default: null
if you define a parent, the element must be in visible only
in relation to this one, that means that if this parent
element is not visible (with an overflow for example)
but that the element is well in the dimensions of this one,
it will return true to you
offsetX/offsetY
Type: Number
Default: 0
a offset for x/y axis
offsetRight / offsetLeft / offsetTop / offsetBottom
Type: Number
Default: 0
offset for every axis,
if offsetX is set, we ignore offsetRight and offetLeft
if offsetY is set, we ignore offsetTop and offsetBottom