简体   繁体   English

Javascript根据多个绝对定位的子元素的高度调整容器的大小

[英]Javascript to resize container based on height of multiple absolutely positioned child elements

I'm using js-graph.it ( http://js-graph-it.sourceforge.net/index.html ) to create a visual representation of a workflow which is created by the end user of the application. 我正在使用js-graph.it( http://js-graph-it.sourceforge.net/index.html )创建由应用程序的最终用户创建的工作流的可视表示。 I've got a lightbox (Fancybox) popup to display the process map and all works well. 我有一个灯箱(Fancybox)弹出窗口来显示流程图,并且一切正常。 However, the steps are all absolutely positioned elements and as such I am unable to set the container's height since there could be any number of rows of elements. 但是,这些步骤都是绝对定位的元素,因此我无法设置容器的高度,因为可能存在任意数量的元素行。

So my question is, is there any way to calculate the total height of absolutely positioned elements and then use that value to update the height for the container element? 所以我的问题是,有什么方法可以计算绝对定位的元素的总高度,然后使用该值来更新容器元素的高度?

I realize that relative positioning/floats would be better but unfortunately that's not an option with js-graph-it since it needs absolute positioning to draw the connecting arrows. 我意识到相对定位/浮动效果会更好,但不幸的是,这不是js-graph-it的选择,因为它需要绝对定位才能绘制连接箭头。

Here's a JSFiddle of what I'm dealing with: http://jsfiddle.net/db3ef/1/ and since code is required, all the HTML is created by PHP so here's how I set it to display 4 items in a row and calculate the absolute positioning: 这是我正在处理的内容的JSFiddle: http : //jsfiddle.net/db3ef/1/并且由于需要代码,因此所有HTML都是由PHP创建的,因此这是我将其设置为连续显示4个项目的方式,计算绝对定位:

    <?php
    if ($steplist) {
        $oddcounter = 0;
        $evencounter = false;
        $row = 1;
        foreach ($steplist as $step) {
            if (is_int($oddcounter)) {
                $oddcounter++;
                $counter = $oddcounter;
            }
            else {
                $evencounter--;
                $counter = $evencounter;
            }
            if ((is_int($oddcounter) && $oddcounter > 0 && ($oddcounter % 5 == 0)) || (is_int($evencounter) && ($evencounter == 0))) {
                $row++;
                if ($row & 1) { // Odd row
                    $evencounter = false;
                    $oddcounter = 1;
                    $counter = $oddcounter;
                }
                else { // Even row
                    $evencounter = 4;
                    $oddcounter = false;
                    $counter = $evencounter;
                }
            }
            $x = ($counter - 1) * 280;
            $y = ($row - 1) * 100;
            // Set status classes
            switch ($step['status']) {
                case 1:
                    $statusclass = ' active';
                    break;
                case 2:
                    $statusclass = ' complete';
                    break;
                case 0:
                    $statusclass = ' pending';
                    break;
            }
            $map .= '<div class="block draggable' . $statusclass . '" id="step' . $step['pgsid'] . '" style="left:' . $x . 'px; top:' . $y . 'px;"><p><strong>' . stripslashes(neat_trim($step['stepname'], 50, '...')) . '</strong><br />';
            $map .= $step['assignee'] . '<br />';
            ($step['enddate'] != '') ? $map .= date('n/j/Y g:ia', strtotime($step['enddate'])) . '</p></div>' : $map .= '</p></div>';
            if (isset($step['links'][0])) {
                foreach ($step['links'] as $connection) {
                    $connectors .= '<div class="connector step' . $step['pgsid'] . ' step' . $connection . '">
                        <img src="/images/arrow.png" class="connector-end">
                        </div>';
                }
            }
        }
    }

Thanks in advance! 提前致谢!

Whats the problem? 有什么问题?

The $y Variable at the end of your loop contains the total height if you add the height of one element, which is in your case 68pixel (4.6875em) 如果添加一个元素的高度,循环末尾的$ y变量将包含总高度,在本例中为68pixel(4.6875em)

Just print it out eg into a javascript variable. 只需将其打印出例如javascript变量即可。

echo '<script type="text/javascript">totalHeight = '.($y+68).';</script>';

position() and outerHeight() for the bottommost positioned element imply the height of the container; 最底部定位的元素的position()outerHeight()表示容器的高度; the just set it with height() or css('min-height: …') . 只需将其设置为height()css('min-height: …')

Outputting the sum in PHP might work but remember PHP is "blind" to what actually shows on the client; 用PHP输出总和也许可以,但是请记住PHP对客户端实际显示的内容是“盲目的”。 maybe have PHP "guess" the height and then finish it in javascript. 也许让PHP“猜测”高度,然后用javascript完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM