How do I get the real .height() of a overflow: hidden or overflow: scroll div?

前端 未结 6 1503
庸人自扰
庸人自扰 2020-11-28 20:29

I have a question regarding how to get a div height. I\'m aware of .height() and innerHeight(), but none of them does the job for me in this case.

6条回答
  •  死守一世寂寞
    2020-11-28 21:01

    Another simple solution (not very elegant, but not too ugly also) is to place a inner div / span then get his height ($(this).find('span).height()).

    Here is an example of using this strategy:

    $(".more").click(function(){
    if($(this).parent().find('.showMore').length) {
    $(this).parent().find('.showMore').removeClass('showMore').css('max-height','90px');
    $(this).parent().find('.more').removeClass('less').text('More');
    } else {
    $(this).parent().find('.text').addClass('showMore').css('max-height',$(this).parent().find('span').height());
    $(this).parent().find('.more').addClass('less').text('Less');
    }
    });
    * {transition: all 0.5s;}
    .text {position:relative;width:400px;max-height:90px;overflow:hidden;}
    .showMore {}
    .text::after {
      content: "";
        position: absolute; bottom: 0; left: 0;
            box-shadow: inset 0 -26px 22px -17px #fff;
        height: 39px;
      z-index:99999;
      width:100%;
      opacity:1;
    }
    .showMore::after {opacity:0;}
    .more {border-top:1px solid gray;width:400px;color:blue;cursor:pointer;}
    .more.less {border-color:#fff;
    }
    
    
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    More

    (This specific example is using this trick to animate the max-height and avoiding animation delay when collapsing (when using high number for the max-height property).

提交回复
热议问题