Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if overflow:hidden html class has cut out from view content using jquery or javascript?

<div class="scrollable" style="overflow: hidden"> </div>

$(function() {
    if($(".scrollable").hasElementsInsideItThatAreCutOffByOverflowHidden == false){
    $(".scrollable").scrollable({ vertical: true, mousewheel: true });
    }   
}


<a onClick="isHidingMyStuff"> check if your stuff is hidden <a>

this doesnt work

like image 378
slex Avatar asked Jan 26 '11 20:01

slex


People also ask

How will you determine the content of HTML elements overflow or not?

Select the element to check form overflow. Check its style. overflow property, if it is 'visible' then the element is hidden. Also, check if its clientWidth is less then scrollWidth or clientHeight is less then scrollHeight then the element is overflowed.

How do you check if an element is in the 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).

Which jQuery filter can be used to check if element is visible?

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.

How do you check if an HTML element is hidden?

Another way to check if an element is hidden is to use the window. getComputedStyle() method. It will return the display property value of the object. We can check if the string is equal to none meaning the element is hidden and do something.


1 Answers

We wrap the contents in a div so we can get a height from it and compare to the .scrollable height (which is not scrollable..)

function isHidingMyStuff(){
    var $s = $('.scrollable');
    
    $s.wrapInner('<div />'); // wrap inner contents
    var hidden = $s.height() < $s.children('div').height();
    
    $s.children('div').replaceWith( $s.children('div').html() ); //unwrap
    
    return hidden;
}

demo :

function isHidingMyShit() {
  var $s = $('.scrollable');

  $s.wrapInner('<div />'); // wrap inner contents
  var hidden = $s.height() < $s.children('div').height();

  $s.children('div').replaceWith($s.children('div').html()); //unwrap

  return hidden;
}
.scrollable {
  height: 50px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="scrollable" style="overflow:hidden;">
  test testtest testtest testtest<br /> testtest testtest testtest testtest<br /> testtest testtest testtest testtest<br /> testtest test TOST
</div>


<a href="#" onClick="alert(isHidingMyShit())"> check if your shit is hidden <a>
like image 154
Gabriele Petrioli Avatar answered Nov 16 '22 00:11

Gabriele Petrioli