Friday 14 August 2015

Check if an element is visible in viewport



Hi Friends,
Many times we get into a situation where we want to know whether an element exists within a viewport of browser or not.
Here below. I am writing a function to check whether an elements exists in a viewport or not.
It will return true or false and by using this, one can use it in different situations.
.

First we will store the DOM element into a variable
var element = document.getElementById("element_id");
Case 1: If we want to know element is completely inside the viewport.
function checkIfElementIsWithinViewport(element) {    if((element != undefined) && (element != null)) {        var element_offets = el.getBoundingClientRect();
To read more about check if an element is visible in viewport visit FindNerd.
You can also ask questions related to Javascript in our javascript forum section.

No comments:

Post a Comment