Get full height of a clipped DIV

前端 未结 6 1027
小鲜肉
小鲜肉 2020-12-14 06:03

How do I get the height of the div which includes the clipped area of the div ?

content
conte
6条回答
  •  独厮守ぢ
    2020-12-14 06:44

    Well, you cannot do it that way, but it's possible when adding a inner element to your container, like this:

    content
    content
    content
    content
    content
    content
    content
    content
    content

    sidenote: wrapping content inside paragraphs is a good practice too, plus that one extra element isn't giving that much of problems, if any at all...

    And JavaScript:

    var innerHeight = document.getElementById('innerElement').offsetHeight;
    alert(innerHeight);
    

    P.S. For this JavaScript to work, put it after your #element div, because plain JavaScript is executed before DOM is ready if it's not instructed to do so. To make this work when DOM is ready, check this.

    But I'd suggest getting jQuery, it will come in handy later on if you're going to extend JavaScript operations in your site.

    Plus, jQuery is the power, for real!

    That way, simply add this script to your (assuming you've jQuery included):

    $(document).ready(function() {
     var innerHeight = $('#innerElement').height();
     alert(innerHeight);
    });
    

    Example @jsFiddle using jQuery way!

提交回复
热议问题