简体   繁体   English

如何使父容器调整其高度以匹配缩放的div

[英]How do I make a parent container adjust it's height to match a scaled div

I'm using jQuery to dynamically scale and zoom a div based on the dimensions of the parent container. 我正在使用jQuery根据父容器的尺寸动态缩放和缩放div。 However, I'm having a difficult time detecting the height. 但是,我很难检测高度。 I have the parent set to auto, but it doesn't detect the change in height as the div rescales. 我将父级设置为自动,但是当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();
    });

});

I have a fiddle here http://jsfiddle.net/sarahwhatsup/33cg5/3/ 我在这里有一个小提琴http://jsfiddle.net/sarahwhatsup/33cg5/3/

You can see my background wrapper stays the same height and doesn't expand to fit the scaling div. 您可以看到我的背景包装器保持相同的高度,并且没有展开以适应缩放div。 How do I go about fixing it so the wrapper always fits around the scaled div inside? 我该如何修复它,以便包装器始终适合内部缩放后的div?

i had a similar question , and i guess the solution works here too: http://jsfiddle.net/VTAH4/1/ 我有一个类似的问题 ,我猜想解决方案也可以在这里工作: http : //jsfiddle.net/VTAH4/1/
This solution uses a plugin to detect the re-sizing: Ben Alaman's resize plugin . 该解决方案使用一个插件来检测调整大小: Ben Alaman的resize插件

$(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")
    });

});

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

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