现在越来越多的交互设计师都希望添加动效,但有的动效的时间又比较复杂
今天提供一个很简单的动效,只需给要append的节点添加一个样式上去,并修改相对应的样式名称
把#parentElement > li 的样式改一下在添加时就会有动效 demo: http://fiddle.jshell.net/force2002/ua03ptzL/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width"/>
<title>Untitled</title>
<style>
@-webkit-keyframes nodeInserted {
from { opacity: 0; }
to { opacity: 1; }
}
#parentElement > li {
-webkit-animation-duration: 1s;
-webkit-animation-name: nodeInserted;
}
</style>
</head>
<body>
<div class="g-doc">
<ul id="parentElement">
</ul>
<input type="button" value="插入" id="insert"/>
</div>
<script>
var elm = document.getElementById('insert');
var parentElement = document.getElementById('parentElement');
elm.addEventListener('click',function(){
var li = document.createElement('li');
li.innerText ='Element has been injected!'
parentElement.appendChild(li);
})
</script>
</body>
</html>