简体   繁体   English

根据div的内部文字计算高度

[英]Calculate height according to div's inner text

I've got a PHP loop to create multiple divs which display data from a database. 我有一个PHP循环来创建多个div,这些div显示数据库中的数据。

<?php 

    foreach($result as $row) {
        ?>
        <div class="container-fluid" id="content-fluid">
            <div class="container-update">
                <div class="update-group">
                    <div class="update-header">
                        <?php echo $row['update_title'] ?>
                        <div class="update-author">
                            by <?php echo $row['update_creator'] ?>
                        </div>
                    </div>
                    <div class="update-body">
                        <?php echo $row['update_text']; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }
?>

I made them clickable through jQuery and they do increase in height: 我通过jQuery使它们可点击,并且它们的高度确实增加了:

        $(document).ready(function() {

            $('.update-group').click(function() {

                var clicks = $(this).data('clicks');
                if(clicks) {
                    $(this).removeAttr('style');
                }
                else {
                    $(this).animate({height: '200px'}, 300);
                }
                $(this).data("clicks", !clicks);
            });
        });

Default height is set to a specific value to ensure that all divs are of the same height and overflow is set to hidden. 默认高度设置为特定值,以确保所有div都具有相同的高度,并且溢出设置为隐藏。 Clicking on them should expand them downwards according to the text inside of it. 单击它们应根据其中的文本向下扩展它们。

Divs of which the text fits just fine, shouldn't be able to expand at all. 文本适合的Divs应该根本不能扩展。 And divs with an excessive amount of text should expand to a calculated height. 并且包含过多文本的div应该扩展到计算出的高度。

I've set up a jsfiddle program to demonstrate what I mean: https://jsfiddle.net/vz6s9brd/ 我已经建立了一个jsfiddle程序来演示我的意思: https ://jsfiddle.net/vz6s9brd/

Right now it just animates to 200px and back. 现在,它只是动画到200px并返回。 I think you're looking for something like this: 我认为您正在寻找这样的东西:

$('.update-group').click(function() {

  var clicks = $(this).data('clicks');
  if(clicks) {
    $(this).removeAttr('style');
  }
  else {
    var bod = $(this).find('.update-body');
    var head = $(this).find('.update-header');
    var neededHeight = bod.height() + head.height();
    if ($(this).height() < neededHeight) {
        $(this).animate({height: neededHeight + 'px'}, 300);
    }

  }
  $(this).data("clicks", !clicks);
});

Rather than calculating the height, you can instead change your overflow settings. 除了计算高度外,您还可以更改溢出设置。 You're wanting to toggle between two modes - one with limited height and no overflow, and the other with height determined by the content. 您想在两种模式之间进行切换-一种模式具有有限的高度且没有溢出,而另一种模式则具有由内容决定的高度。

  if(clicks) {
    $(this).css({overflow: 'hidden', height: '6em'});
  }
  else {
    $(this).css({overflow: 'inherit', height: '100%'});
  }

Unfortunately, doing it this way doesn't animate very well. 不幸的是,以这种方式进行动画效果不佳。 Even if you replace the css() calls above with calls to animate() , the result is unsatisfying. 即使将上面的css()调用替换为对animate()调用,结果也不令人满意。 How important the animation is to you is up to you. 动画对您的重要性取决于您。

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

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