如何使用css3动画100%动画宽度和高度?

时间:2015-04-26 16:35:50

标签: html css css3 animation cakeyframeanimation

我有以下代码

HTML

<div></div>

CSS

div {
    background: tomato;
    width: 100px;
    height: 100px;
    -webkit-animation: animateThis 0.3s ease-in;
    -webkit-animation-fill-mode: forwards;
}

@-webkit-keyframes animateThis {
    0% {
        width: 100px;
        height: 100px;
    }
    100% {
        width: 100%;
        height: 100%;

    }
}

DEMO

我想要做的是我想扩展div的高度和高度100%来覆盖整个浏览器。我不知道如何动画100%的高度。我在google上搜索了一下。没找到运气

3 个答案:

答案 0 :(得分:4)

你需要指定100%的东西......在这种情况下,html/body

&#13;
&#13;
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
div {
  background: tomato;
  width: 100px;
  height: 100px;
  -webkit-animation: animateThis 1s ease-in;
  -webkit-animation-fill-mode: forwards;
}
@-webkit-keyframes animateThis {
  0% {
    width: 100px;
    height: 100px;
  }
  100% {
    width: 100%;
    height: 100%;
  }
}
&#13;
<div></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您还需要将htmlbody标记设置为100%以欺骗浏览器。

&#13;
&#13;
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

div {
  background: tomato;
  width: 100px;
  height: 100px;
  -webkit-animation: animateThis 0.3s ease-in;
  -webkit-animation-fill-mode: forwards;
}

@-webkit-keyframes animateThis {
  0% {
    width: 100px;
    height: 100px;
  }
  100% {
    width: 100%;
    height: 100%;
  }
}
&#13;
<div></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

.mydiv {
    animation-name: test-animation;
    animation-duration: 1s;
}

@keyframes test-animation {
    from {
        width:0px;
    }

    to {
        width:calc( 100% - 1px );
    }
}

这是一个很好的选择,可以使用calc()函数从0px动画到几乎100%,该函数将返回几乎100%的宽度。