試試這個:http://jsfiddle.net/EpzDM/
它的工作原理,但我不懷疑的JavaScript可能會得到改善。
的JavaScript:
var $box = $('#box');
var $boxInner = $box.find(' > div');
var fixIt = function() {
var newWidth = 300;
var newHeight = 150;
$boxInner.fadeOut(function() {
$box.animate({
marginLeft: -newWidth/2,
marginTop: -newHeight/2,
width: newWidth,
height: newHeight
}, 500, function() {
$boxInner.fadeIn();
});
});
};
$(window).resize(function() {
$box.css({
marginLeft: -$box.width()/2,
marginTop: -$box.height()/2
});
}).resize();
$('<button>Clicky!</button>').appendTo(document.body).click(fixIt).css({
position: 'fixed',
top: 0,
left: 0
});
CSS:
#box {
width: 200px;
height: 100px;
background: #ccc;
position: fixed;
left: 50%;
top: 50%
}
HTML:
<div id="box">
<div>
<p>Contents of div</p>
<p>Contents of div</p>
<p>Contents of div</p>
</div>
</div>
,我給多了一個解決方案...嘗試它會成功。 – kobe 2011-05-02 03:52:59