2013-03-31 33 views
0

我正在使用jQuery根據父容器的尺寸動態縮放div。但是,我很難檢測到身高。我有父設置爲自動,但它不檢測高度的變化,因爲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(); 
    }); 

}); 

我這裏有http://jsfiddle.net/sarahwhatsup/33cg5/3/

小提琴你可以看到我的背景包裝保持相同的高度,不會展開,以適應縮放股利。我該如何解決它,所以包裝始終適合在裏面的縮放div?

+0

我不確定這是可能的使用CSS3轉換。應用轉換不會改變DOM元素本身的大小(請參閱http://stackoverflow.com/questions/10858523/css-transform-with-element-resizing)。您可能能夠找到提供相同效果的替代解決方案(請參閱http://stackoverflow.com/questions/10627306/scale-zoom-a-dom-element-and-the-space-it-occupies-using-css3 -transform-scale) – metadept

+0

感謝您的鏈接 - 我不認爲它會爲我後來的工作:\ – user1519225

+0

爲什麼你不調整包裝div?現在你只是縮小它的孩子,這就是爲什麼他們不適合包裝。 – hjuster

回答

0

我有一個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") 
    }); 

}); 
+0

嘿謝謝,但你發佈的小提琴不會讓方格div的大小調整了... – user1519225

+0

@ user1519225是的,我想你必須以你的方式實現它,寬度 - 我現在沒有時間做它我自己:b – mahnouel

相關問題