动画div并从中心扩展

vik*_*shi 3 css animation expand center

我有一个简单的代码,从中心水平和垂直扩展div,但它只扩展到左侧或底部,我希望它扩展到两侧(左= 50px,右= 50px)或(顶部= 50px,底部= 50px)来自中心,总数等于100px.这里的代码:

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
    background-color:#bca;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    background-repeat: no-repeat;
    background-position: center center;
    border:1px solid green;
    padding:10px;
}
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="go">&raquo; Run</button>

<div id="block">Hello!</div>
<script>


$("#go").click(function(){
  $("#block").animate({
    width: "100px",
    height: "100px",
    opacity: 0.6,
  }, 1500 );
});
</script>

</body>
Run Code Online (Sandbox Code Playgroud)

谢谢.

ang*_*ust 16

这个怎么样?

HTML

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <button id="go">&raquo; Run</button>
    <div id="block">Hello!</div>
</body>
Run Code Online (Sandbox Code Playgroud)

CSS

div {
    background-color:#bca;
    background: #fff;
    position: absolute;
    top: 0px;
    left: 0px;
    margin-left: 50%;
    margin-top: 50%;
    background-repeat: no-repeat;
    background-position: center center;
    border:1px solid green;
    padding:10px;
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$("#go").click(function(){
    $("#block").animate({
        width: "100px",
        height: "100px",
        top: "-50px",
        left: "-50px",
        opacity: 0.6,
    }, 1500 );
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/theguywholikeslinux/87f4Y/

我使用margin-left和margin-top进行居中,然后使用top:和left:值来使盒子长大,左右以及向下和向右.

如果你想让"Hello"保持在盒子的中心,你可能需要添加一些额外的CSS.