Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在固定大小的容器中,如何使图像高度根据其下方内容的大小调整大小_Javascript_Html_Css - Fatal编程技术网

Javascript 在固定大小的容器中,如何使图像高度根据其下方内容的大小调整大小

Javascript 在固定大小的容器中,如何使图像高度根据其下方内容的大小调整大小,javascript,html,css,Javascript,Html,Css,我知道这可能是一个艰难的过程,我有10多年的HTML工作经验,但我找不到一个解决方案不涉及使用Javascript进行复杂计算 <!-- <div id="image"> UNCOMMENT THIS WHEN YOU'RE READY </div> --> </div><!-- mainContainerDiv--> <script>

我知道这可能是一个艰难的过程,我有10多年的HTML工作经验,但我找不到一个解决方案不涉及使用Javascript进行复杂计算

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
我有一个固定大小的容器分成两部分,所以容器有上半部分和下半部分。上半部分有一个图像。下半部分有文本。我想要的是图像和文本占据所有可用空间。如果文本较短,则图像有更多可用空间。如果文本较大,则图像会动态地变小。我添加了几个截图来说明我想要什么。在所有情况下,它们都占据固定大小容器的100%可用垂直空间

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

这里有一个纯css方法

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
您可能可以尝试使用
display:table row

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
尝试并修改底部文本的长度,然后查看图像的大小

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
有一些警告,但它是有效的

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
或者,您也可以使用flexbox,但要使用背景图像:

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

或者,最简单的方法可能是使用JavaScript的element.getBoundingClientRect()函数。这就需要将文本放置在它自己的相对分区内,然后应用所述函数计算完成文本的高度(以像素为单位)。
        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
由于主容器分区使用的是固定尺寸,因此可以计算并使用图像的剩余空间。 这里可能需要一两个棘手的小算法,比如计算纵横比以匹配高度特性

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
希望这有帮助;我不知道你试图避免的方法有多复杂

        <head>
<style>
    body{
        background: #111;
        color:      #d00;
    }   

    #mainContainerDiv{
            top:        20%;
            height:     49%;

            left:       30%;
            width:      40%;

            background: #c90;
            color:      #d00;

            position: absolute;     
    }       



    #image{
            top:        10%;
            height:     30%;

            left:       30%;
            width:      40%;

            background: #0d0;
            color:      #ddd;

            position: absolute;     
    }       



    #textAtBottom{
            bottom: 2%;
            /*height:       40%;*/

            left:       30%;
            width:      40%;

            background: #d00;
            color:      #eee;

            padding: 1%;
            position: absolute;     
    }       
        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

身体{
背景:#111;
颜色:#d00;
}   
#主容器IV{
最高:20%;
身高:49%;
左:30%;
宽度:40%;
背景:#c90;
颜色:#d00;
位置:绝对位置;
}       
#形象{
排名前10%;
身高:30%;
左:30%;
宽度:40%;
背景:#0d0;
颜色:#ddd;
位置:绝对位置;
}       
#底部文本{
底部:2%;
/*身高:40%*/
左:30%;
宽度:40%;
背景:#d00;
颜色:#eee;
填充:1%;
位置:绝对位置;
}       

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl abc定义ghi jkl

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

var a=document.getElementById('mainContainerDiv');
var ar=a.getBoundingClientRect();
警报('Main container element是其包含元素(即')的'+ar.top+'像素);
//警报('und hauptBeinhalter Unten ist'+ar.bottom');
//比尔迪斯·尼德里格(Bildis niedriger),所以他是“+ar.height-ar.top+”霍克总数“;
var e=document.getElementById('textAtBottom');
var x=e.getBoundingClientRect();
警报('底部的文本开始'+x.top+'垂直像素向下');
var numm=parseInt(x.top)-parseInt(ar.top);
警报('sooooo,你在文本上方有'+numm+'像素的空间来压缩你的图像!!!');

使用小js/jquery代码,您可以计算容器和文本的高度,然后调整图像的大小

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
演示:

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
使用JQUERY:

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
var resizeUI = function(){
        var container = $('.container').eq(0);
        var textFrame = $('.container .textFrame');
        var maxH = container.innerHeight();
        var textH = textFrame.outerHeight();
        $('.container .imageFrame').height(parseInt(maxH-textH,10)).show();
    };
css:

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
div.container{
    background: #BB0000;
    width: 300px;
    height: 300px;
    overflow: hidden;
}

div.imageFrame{
    max-width: 100%;
    max-height: 100%;
    display: none;
}
div.imageFrame img{
    display: block;
    height: 100%;
}

div.textFrame{
    max-width: 100%;
    max-height: 100%;
    background: #333;
    color: #DDD;
}
<div class="container">
    <div class="imageFrame">
        <img src="http://www.conservatoryweb.co.uk/wp-content/uploads/2012/04/iStock_000011293178XSmallPaulMaguire.jpg"/>
    </div>

    <div class="textFrame">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div> 
</div>
html:

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
div.container{
    background: #BB0000;
    width: 300px;
    height: 300px;
    overflow: hidden;
}

div.imageFrame{
    max-width: 100%;
    max-height: 100%;
    display: none;
}
div.imageFrame img{
    display: block;
    height: 100%;
}

div.textFrame{
    max-width: 100%;
    max-height: 100%;
    background: #333;
    color: #DDD;
}
<div class="container">
    <div class="imageFrame">
        <img src="http://www.conservatoryweb.co.uk/wp-content/uploads/2012/04/iStock_000011293178XSmallPaulMaguire.jpg"/>
    </div>

    <div class="textFrame">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div> 
</div>

他说:“我的工作是在工作中完成的,我的工作是在工作中完成的,我的工作是在工作中完成的。”。在vero eos和accusam以及justo duo dolores和ea Reum。斯泰特·克莱塔·卡斯德·古伯格伦(Stet clita kasd gubergren),没有海洋保护区是属于同一领域的。Lorem ipsum dolor sit amet。

您可以使用JS+CSS calc获得此效果。您需要使用JS获取文本的高度,并对图像高度应用cal以使其适当缩放

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
calc("100% - " + paragraphHeightVariable + "px - 2em")
2em用于说明段落标记的填充。下面的示例使用jQuery获取段落高度,但您可以使用vanillajs库获取段落高度

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>
$(“.container”)。每个(函数(a){
var$container=$(这个);
var$image=$container.children(“img”);
var$p=$container.children(“p”);
$image.css(“高度”,“计算(100%-”+$p.height()+“px-2em)”);
log($image.css(“height”),$p.height());
});
.container{
宽度:400px;
高度:450px;
背景颜色:灰色;
}
.货柜{
背景颜色:紫色;
}
.集装箱img{
最大宽度:100%;
}

你好,世界

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

Lorem ipsum dolor sit amet,是一位杰出的献身者。在《莫丽丝·利奥》中。Nunc vel finibus tortor。Nam suscipit quam purus,一个无效的诅咒。整数cursus arcu vel libero blandit效率。在利古拉,马蒂斯·拉库斯·森佩尔,康瓦利斯·洛雷姆。

        <!-- <div id="image">     UNCOMMENT THIS WHEN YOU'RE READY
             </div> -->

    </div><!-- mainContainerDiv-->


    <script>
        var a = document.getElementById('mainContainerDiv');
        var ar = a.getBoundingClientRect();
        alert('Main container element is ' + ar.top + 'pixels from its containing element i.e. <body>');

        // alert(' und hauptBeinhalter Unten ist ' + ar.bottom ');
        // Bildis niedriger, so Teil ist ' + ar.height-ar.top + 'hoch total');


        var e = document.getElementById('textAtBottom');
        var x = e.getBoundingClientRect();

        alert('Text at Bottom starts ' + x.top + ' vertical pixels down from <body>');

        var numm = parseInt(x.top)-parseInt(ar.top);
        alert('sooooo, you got '+ numm + ' pixels of space above the text to squeeze in your image!!!');
    </script>

Lorem ipsum dolor sit amet,是一位杰出的献身者。在《莫丽丝·利奥》中。Nunc vel finibus tortor。Nam suscipit quam purus,一个无效的诅咒。整数cursus arcu vel libero blandit效率。在ligula placerat,mattis lacus semper,convallis lorem.Donec Concertitur maximus mauris,Egesta sollicitudin nunc-amet。麦格纳酒店。在《爱洛斯·莫利斯》中,玛莱苏亚达·阿库坐在阿梅特,帝王。Mauris vitae augue id nisi Vulputte ultrices。Morbi a ipsum lacinia arcu consequeat sollicitudin ut quis mauris。奎斯克酒后驾车,无前庭矢状肌,精英静脉曲张。便利性前庭