Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery check if element is visible in viewport [duplicate]

Function to check if the div class "media" is within the browsers visual viewport regardless of the window scroll position.

<HTML> <HEAD>   <TITLE>My first HTML document</TITLE> </HEAD> <BODY>   <div class="main">    <div class="media"></div>   </div>  </BODY> </HTML> 

Trying to use this plugin https://github.com/customd/jquery-visible with this function but I don't know how to make it work.

$('#element').visible( true ); 
like image 821
Vim Bonsu Avatar asked Dec 26 '13 21:12

Vim Bonsu


People also ask

How to check if element is visible in viewport jQuery?

Check if element is visible in viewport using jquery:If the bottom position of the viewport is greater than the element's top position AND the top position of the viewport is less than the element's bottom position, the element is in the viewport (at least partially).

How to check if element is in viewport?

Summary. Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.

Is visible in jQuery?

The :visible selector in jQuery is used to select every element which is currently visible. It works upon the visible elements. The elements that are consuming space in the document are considered visible elements. The height and width of visible elements are larger than 0.

How do you check if an element is visible after scrolling?

To know whether the element is fully visible in viewport, you will need to check whether top >= 0, and bottom is less than the screen height. In a similar way you can also check for partial visibility, top is less than screen height and bottom >= 0. The Javascript code could be written as : window.

How to check if element is visible in viewport without plugin?

Check if element is visible in viewport without plugin: First determine the top and bottom positions of the element. Then determine the position of the viewport's bottom (relative to the top of your page) by adding the scroll position to the viewport height.

What is a viewport check?

A dead simple jQuery script (not a plugin) that determines if an element is in the viewport. Can be used to detect when an element is scrolling into view and tell the user which part of the page they are viewing. 1.

How do you know if a div is in a viewport?

If the <div> element is in the viewport, its top and left are always greater than or equal zero. In addition, its distance from the right is less than or equal to the width of the viewport, and ids distance from the bottom is less than or equal to the height of the viewport.

How does the isinviewport () function work?

Let’s dive into the isInViewport () function to understand how it works. The method element.getBoundingClientRect () provides the element’s position and its relative position to the viewport. It returns an object that includes element’s height, width, and its distance from the top, left, bottom, and right of the viewport.


2 Answers

You can write a jQuery function like this to determine if an element is in the viewport.

Include this somewhere after jQuery is included:

$.fn.isInViewport = function() {     var elementTop = $(this).offset().top;     var elementBottom = elementTop + $(this).outerHeight();      var viewportTop = $(window).scrollTop();     var viewportBottom = viewportTop + $(window).height();      return elementBottom > viewportTop && elementTop < viewportBottom; }; 

Sample usage:

$(window).on('resize scroll', function() {     if ($('#Something').isInViewport()) {         // do something     } else {         // do something else     } }); 

Note that this only checks the top and bottom positions of elements, it doesn't check if an element is outside of the viewport horizontally.

like image 57
Tom Pažourek Avatar answered Sep 23 '22 23:09

Tom Pažourek


Check if element is visible in viewport using jquery:

First determine the top and bottom positions of the element. Then determine the position of the viewport's bottom (relative to the top of your page) by adding the scroll position to the viewport height.

If the bottom position of the viewport is greater than the element's top position AND the top position of the viewport is less than the element's bottom position, the element is in the viewport (at least partially). In simpler terms, when any part of the element is between the top and bottom bounds of your viewport, the element is visible on your screen.

Now you can write an if/else statement, where the if statement only runs when the above condition is met.

The code below executes what was explained above:

// this function runs every time you are scrolling  $(window).scroll(function() {     var top_of_element = $("#element").offset().top;     var bottom_of_element = $("#element").offset().top + $("#element").outerHeight();     var bottom_of_screen = $(window).scrollTop() + $(window).innerHeight();     var top_of_screen = $(window).scrollTop();      if ((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){         // the element is visible, do something     } else {         // the element is not visible, do something else     } }); 

This answer is a summary of what Chris Bier and Andy were discussing below. I hope it helps anyone else who comes across this question while doing research like I did. I also used an answer to the following question to formulate my answer: Show Div when scroll position.

like image 41
ADB Avatar answered Sep 22 '22 23:09

ADB