我正在使用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?
我不確定這是可能的使用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
感謝您的鏈接 - 我不認爲它會爲我後來的工作:\ – user1519225
爲什麼你不調整包裝div?現在你只是縮小它的孩子,這就是爲什麼他們不適合包裝。 – hjuster