【问题标题】:How do I make a parent container adjust it's height to match a scaled div如何使父容器调整其高度以匹配缩放的 div
【发布时间】:2013-03-22 01:28:53
【问题描述】:

我正在使用 jQuery 根据父容器的尺寸动态缩放和缩放 div。但是,我很难检测高度。我将父级设置为自动,但它没有检测到 div 重新缩放时的高度变化。

$(window).load(function () {
    console.log( "window load" );
    function zoomSquare() {
        var $square = $('.square');

        var vh = $square.parent().width(); // Get the height of whatever the parent of square is
        var squareHeight = $square.width(); // Grab the height of .square
        var desiredHeight = Math.round(vh * 0.95);
        var zoom = (desiredHeight / squareHeight);

        //$square.css('zoom', zoom);
        $square.css('-webkit-transform', 'scale(' + zoom + ')');
        $square.css('-moz-transform', 'scale(' + zoom + ')');
        $square.css('-o-transform', 'scale(' + zoom + ')');
        $square.css('transform', 'scale(' + zoom + ')');
    }

    // When the browser is resized
    $(window).on('resize', function () {
        console.log( "resize" );
        zoomSquare();
    });

    // When the page first loads
    $(document).ready(function () {
        console.log( "dom ready" );
        zoomSquare();
    });

});

我这里有个小提琴http://jsfiddle.net/sarahwhatsup/33cg5/3/

您可以看到我的背景包装保持相同的高度,并且不会扩展以适应缩放 div。我该如何修复它以使包装器始终适合内部缩放的 div?

【问题讨论】:

  • 我不确定这是否可以使用 CSS3 转换。应用变换不会改变 DOM 元素本身的大小(参见stackoverflow.com/questions/10858523/…)。您也许可以找到提供相同效果的替代解决方案(请参阅stackoverflow.com/questions/10627306/…
  • 为什么不调整包装 div 的大小?现在你只是在缩放它的孩子,这就是他们不适合包装的原因。
  • 最终的游戏是让 div 在下面,它会根据上面 div 的高度调整它们的位置(想象一个图片库,下面有三列文本)。就像在响应式网站中内容如何重排一样。
  • 感谢您的链接 - 我不认为它会为我所追求的工作:\

标签: jquery css zooming transform scale


【解决方案1】:

我有一个similar question,我想这个解决方案在这里也有效: http://jsfiddle.net/VTAH4/1/
此解决方案使用插件来检测大小调整:Ben Alaman's resize plugin

$(window).load(function () {

    $.fn.animateHeight = function (container) {
         var thisHeight = $(container).map(function (i, e) {
        return $(e).height();
         }).get();

         return this.animate({
        height: thisHeight
         });
    };

    // When the browser is resized
    $(window).resize(function () {
        $(".square").animateHeight(".wrapper")
    });

    // When the page first loads
    $(document).ready(function () {     
        $(".square").animateHeight(".wrapper")
    });

});

【讨论】:

  • 嘿,谢谢,但是您发布的小提琴不再使方形 div 调整大小...
  • @user1519225 是的,我想你必须以自己的方式实现它,宽度 - 我现在没有时间自己做:b
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 2010-12-25
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多