0

I'm rendering a website using js, and would like to optimize my render cycle.

Is there a way to get an event when a page element gets visible to the user?

this way delaying rendering of elements until they are visible?

Martin Kristiansen
  • 8,778
  • 10
  • 43
  • 81
  • Duplicate of [How to check if an element is in the view of the user with jquery](http://stackoverflow.com/questions/8229291/how-to-check-if-an-element-is-in-the-view-of-the-user-with-jquery)? – Rudie Mar 02 '13 at 19:42
  • 1
    Actually I say **no**, since this question is either tagged jQuery, nor does he mention he uses it. – Lukas Knuth Mar 02 '13 at 23:38

3 Answers3

1
 var viewportWidth = jQuery(window).width(),
 viewportHeight = jQuery(window).height(),
 documentScrollTop = jQuery(document).scrollTop(),
 documentScrollLeft = jQuery(document).scrollLeft(),
 minTop = documentScrollTop,
 maxTop = documentScrollTop + viewportHeight,
 minLeft = documentScrollLeft,
 maxLeft = documentScrollLeft + viewportWidth,
 $myElement = jQuery(element), // your element jquery object
 elementOffset = $myElement.offset();
 if (
 (elementOffset.top > minTop && elementOffset.top < maxTop) &&
 (elementOffset.left > minLeft &&elementOffset.left < maxLeft)
  ) {
  alert('element is visible');
  } else {
  alert('element is not visible');
  }
James Daly
  • 1,338
  • 13
  • 25
1

Since you did not explain if there is any specific lib/framework you are using, or is there any specific condition to render element visible I will give you this:

jQuery $(document).ready() can detects if whole page is loaded.

On the other hand if element was display:none or visibility:hidden, or both or whatever, you can use jQuery is like this : $('element').is(':visible') as a condition and trigger whatever inside if statement.

Rastko
  • 481
  • 2
  • 7