animate an image from bottom to top

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I have an image of a little tree and I would like to make it grow from bottom to top using jQuery and CSS.

For the moment the tree has bottom position to 0 and goes up with animate() jQuery function.

I can make a div that overlaps to the tree and animate it with animate() jquery function and removing the height to it, but the original background (of the body) uses a CSS gradient so I can't make the div overlap the image. Here is my code:

CSS:

.wrap_tree{     height:300px;     position:relative; } .tree{     overflow: hidden;     position:absolute;     display:none;     bottom:0px;     width:200px;     left:28%; }

HTML:

<div class="wrap_tree">     <div class="tree">         <img src="tree.png"/>     </div> </div>

JavaScript/jQuery:

$('.tree').animate({     height: 'toggle' },5000);

回答1:

How about doing this with Pure CSS? I made it from scratch using CSS3 @keyframe

Explanation: Am just overlapping the tree using an absolute positioned element, and than using @keyframe am collapsing the height property to 0, rest is self explanatory.

Demo

Demo 2 (Added position: relative; to the container element as this is important to do else your position: absolute; element will run out in the wild)

Demo 3 Tweaking up animation-duration for slower animation rate

.tree {     width: 300px;     position: relative; }  .tree > div {     position: absolute;     height: 100%;     width: 100%;     background: #fff;     top: 0;     left: 0;     -webkit-animation-name: hello;     -webkit-animation-duration: 2s;     -webkit-animation-fill-mode: forwards;     animation-name: hello;     animation-duration: 2s;     animation-fill-mode: forwards; }  .tree img {     max-width: 100%; }    @keyframes hello {     0% {         height: 100%;     }     100% {         height: 0%;     } }  @-webkit-keyframes hello {     0% {         height: 100%;     }     100% {         height: 0%;     } }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!