我現在看到我是複製粘貼錯誤的文本/問題,根本沒有意義!對不起!我在我的代碼中發現錯誤。這是一個工作版本:每個在jquery後追加
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body{
margin:0;
}
#gallery{
width:100%;
height:100%;
position:fixed;
left:0;
top:0;
background:#000;
opacity:0.9;
}
.child{
width:auto;
height:auto;
position:fixed;
left:0;
top:0;
}
</style>
</head>
<body>
<div id="click_me">click me</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#click_me').click(function(){
$('body').append('<div id="gallery"></div><div class="child" style="width:300px;height:600px;background:#0F0"></div><div class="child" style="width:500px;height:400px;background:#C60"></div><div class="child" style="width:600px;height:200px;background:#390"></div>');
height_of_window = $('#gallery').height();
width_of_window = $('#gallery').width();
max_height = height_of_window - 100;
max_width = width_of_window - 100;
$('.child').each(function() {
var $this = $(this);
height_of_child = $this.height();
width_of_child = $this.width();
if (width_of_child >= max_width) {
$this.css({
'max-width': max_width + 'px'
})
}
if (height_of_child >= max_height) {
$this.css({
'max-height': max_height + 'px'
})
}
margin_top = (height_of_window - $this.height())/2;
margin_left = (width_of_window - $this.width())/2;
$this.css({
'margin-top': margin_top + 'px',
'margin-left': margin_left + 'px'
})
}); // end click
}); // end each
}); // end document redy
</script>
</body>
</html>
問題是什麼? – 2012-01-04 10:48:09
將更好地解釋BRB – Hakan 2012-01-04 10:57:59