Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if element is hidden by overflow:hidden JQuery/JavaScript

I have a calendar that has list of events per day. Currently I show a maximum of 3 events per day and allow the user to toggle to expand the list.

I hide the list with overflow:hidden and max-height:XXpx property. I am trying to detect the events that are currently hidden within that list.

I've looked around and cant find anything that detects this specifically

I have tried:

if (element.offsetHeight < element.scrollHeight || element.offsetWidth < element.scrollWidth) {
     // element has overflow value
 } else {
     // element doesn't have overflow value
 }

and both element.offsetHeight & element.scrollHeight return the same value for any of the elements in my list.

like image 564
Philippe Fisher Avatar asked Jun 12 '15 13:06

Philippe Fisher


People also ask

How check element is hidden or not in jQuery?

To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element.

How do you check if div is show or hide in jQuery?

Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.


1 Answers

scrollHeight and scrollWidth are DOM properties, not jQuery.

$('div').each(function() {
     // get scroll measurements from DOM element
     var contentHeight = this.scrollHeight;
     var contentWidth = this.scrollWidth;
     // get the visible measurements from jQuery object
     var $this = $(this);
     var visibleHeight = $this.height();
     var visibleWidth = $this.width();

     if (visibleHeight < contentHeight
         || visibleWidth < contentWidth ) {
         // element has overflow value
     } else {
         // element doesn't have overflow value
     }
 })
like image 63
Brad Avatar answered Oct 04 '22 21:10

Brad