Get the element which is the most visible on the screen

Yates

I would like to get the one element which is the most visible on the screen (takes up the most space). I have added an example picture below to understand my question a bit more.

example

The two black borders are the sides of a screen. As you can see, the green box (div2) is the most visible on the screen - I would like to know how I can get that element. The most visible element should not have to be fully visible.

I have done a quick (it wasn't THAT quick) seach but to no avail, if I have missed it - my apologies.

Andy

TLDR:

Inspired by this question and the necessity for similar functionality in my own projects, I've written a module/jQuery plugin based on the code below. If you're not interested in the 'how', just download that or install with your favourite package manager.

Original Answer:

The answer provided by exabyssus works well in most cases, apart from when neither of an element's top or bottom is visible e.g when the element height is greater than the window height.

Here's an updated version which takes that scenario into account and uses getBoundingClientRect which is supported right the way down to IE8:

// Usage: var $element = getMostVisible($('.elements' ));
function getMostVisible($elements) {
    var element,
        viewportHeight = $(window).height(),
        max = 0;

    $elements.each(function() {
        var visiblePx = getVisibleHeightPx($(this), viewportHeight);

        if (visiblePx > max) {
            max = visiblePx;
            element = this;
        }
    });

    return $elements.filter(element);
}

function getVisibleHeightPx($element, viewportHeight) {
    var rect = $element.get(0).getBoundingClientRect(),
        height = rect.bottom - rect.top,
        visible = {
            top: rect.top >= 0 && rect.top < viewportHeight,
            bottom: rect.bottom > 0 && rect.bottom < viewportHeight
        },
        visiblePx = 0;

    if (visible.top && visible.bottom) {
        // Whole element is visible
        visiblePx = height;
    } else if (visible.top) {
        visiblePx = viewportHeight - rect.top;
    } else if (visible.bottom) {
        visiblePx = rect.bottom;
    } else if (height > viewportHeight && rect.top < 0) {
        var absTop = Math.abs(rect.top);

        if (absTop < height) {
            // Part of the element is visible
            visiblePx = height - absTop;
        }
    }

    return visiblePx;
}

This returns the most visible element based on pixels rather than as a percentage of the height of the element, which was ideal for my use-case. It could easily be modified to return a percentage if desired.

You could also use this as a jQuery plugin so you can get the most visible element with $('.elements').mostVisible() rather than passing the elements to the function. To do that, you'd just need to include this with the two functions above:

$.fn.mostVisible = function() {
    return getMostVisible(this);
};

With that in place you can chain your method calls rather than having to save the element into a variable:

$('.elements').mostVisible().addClass('most-visible').html('I am most visible!');

Here's all of that wrapped up in a little demo you can try out right here on SO:

(function($) {
  'use strict';

  $(function() {
    $(window).on('scroll', function() {
      $('.the-divs div').html('').removeClass('most-visible').mostVisible().addClass('most-visible').html('I am most visible!');
    });
  });

  function getMostVisible($elements) {
    var element,
      viewportHeight = $(window).height(),
      max = 0;

    $elements.each(function() {
      var visiblePx = getVisibleHeightPx($(this), viewportHeight);

      if (visiblePx > max) {
        max = visiblePx;
        element = this;
      }
    });

    return $elements.filter(element);
  }

  function getVisibleHeightPx($element, viewportHeight) {
    var rect = $element.get(0).getBoundingClientRect(),
      height = rect.bottom - rect.top,
      visible = {
        top: rect.top >= 0 && rect.top < viewportHeight,
        bottom: rect.bottom > 0 && rect.bottom < viewportHeight
      },
      visiblePx = 0;

    if (visible.top && visible.bottom) {
      // Whole element is visible
      visiblePx = height;
    } else if (visible.top) {
      visiblePx = viewportHeight - rect.top;
    } else if (visible.bottom) {
      visiblePx = rect.bottom;
    } else if (height > viewportHeight && rect.top < 0) {
      var absTop = Math.abs(rect.top);

      if (absTop < height) {
        // Part of the element is visible
        visiblePx = height - absTop;
      }
    }

    return visiblePx;
  }



  $.fn.mostVisible = function() {
    return getMostVisible(this);
  }

})(jQuery);
.top {
  height: 900px;
  background-color: #999
}

.middle {
  height: 200px;
  background-color: #eee
}

.bottom {
  height: 600px;
  background-color: #666
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="the-divs">
  <div class="top"></div>
  <div class="middle"></div>
  <div class="bottom"></div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get on-screen visible element objects in jQuery?

From Dev

WPF determine element is visible on screen

From Dev

How to make the Draggable element to get visible on top of all the element in the screen while dragging?

From Dev

UITableView with one visible cell: determine which is most visible

From Dev

Best practice for when element is visible on the screen?

From Dev

Check if element is visible for certain percentage on screen (viewport)?

From Dev

Check if element is visible on screen JavaScript/jQuery/iScroll

From Dev

Creating hidden screen sessions which are not visible via "screen -ls" command

From Dev

Creating hidden screen sessions which are not visible via "screen -ls" command

From Dev

Tabbing to an element which is not yet visible in HTML

From Dev

Identify visible portion of an element which has an overflow

From Dev

Python and Selenium - Get element that is visible

From Dev

SVG element not visible on screen, can see element added in console

From Dev

SVG element not visible on screen, can see element added in console

From Dev

Get the coordinates of visible part of screen when zoomed in

From Dev

How to get the time for which screen is ON

From Dev

In WebDriver, is it possible to get the content of the tag which is not visible?

From Dev

The best way to load markers which are only visible on the screen (Android)?

From Dev

The best way to load markers which are only visible on the screen (Android)?

From Dev

What is the cause of these "dead" pixels which are visible even when the screen is off?

From Dev

How to tap on a button inside a CollectionView cell which is not visible on screen

From Dev

Get the bit which is most at the left with dichotomy

From Dev

Determining which screen a window is on (by checking where the most surfacearea is located)

From Dev

Get element to stick to bottom of the screen

From Dev

Add class on html element when it gets visible on screen

From Dev

How to know if an element is visible on the screen. If not, scroll the page

From Dev

Automatically loading more posts (checking if element is visible on screen)

From Dev

jQuery: get value of visible element out of class

From Dev

How to get the last visible element in jQuery?

Related Related

  1. 1

    How to get on-screen visible element objects in jQuery?

  2. 2

    WPF determine element is visible on screen

  3. 3

    How to make the Draggable element to get visible on top of all the element in the screen while dragging?

  4. 4

    UITableView with one visible cell: determine which is most visible

  5. 5

    Best practice for when element is visible on the screen?

  6. 6

    Check if element is visible for certain percentage on screen (viewport)?

  7. 7

    Check if element is visible on screen JavaScript/jQuery/iScroll

  8. 8

    Creating hidden screen sessions which are not visible via "screen -ls" command

  9. 9

    Creating hidden screen sessions which are not visible via "screen -ls" command

  10. 10

    Tabbing to an element which is not yet visible in HTML

  11. 11

    Identify visible portion of an element which has an overflow

  12. 12

    Python and Selenium - Get element that is visible

  13. 13

    SVG element not visible on screen, can see element added in console

  14. 14

    SVG element not visible on screen, can see element added in console

  15. 15

    Get the coordinates of visible part of screen when zoomed in

  16. 16

    How to get the time for which screen is ON

  17. 17

    In WebDriver, is it possible to get the content of the tag which is not visible?

  18. 18

    The best way to load markers which are only visible on the screen (Android)?

  19. 19

    The best way to load markers which are only visible on the screen (Android)?

  20. 20

    What is the cause of these "dead" pixels which are visible even when the screen is off?

  21. 21

    How to tap on a button inside a CollectionView cell which is not visible on screen

  22. 22

    Get the bit which is most at the left with dichotomy

  23. 23

    Determining which screen a window is on (by checking where the most surfacearea is located)

  24. 24

    Get element to stick to bottom of the screen

  25. 25

    Add class on html element when it gets visible on screen

  26. 26

    How to know if an element is visible on the screen. If not, scroll the page

  27. 27

    Automatically loading more posts (checking if element is visible on screen)

  28. 28

    jQuery: get value of visible element out of class

  29. 29

    How to get the last visible element in jQuery?

HotTag

Archive