DOM的CSS与JS动画效果

dom动画效果可以用CSS也可以用JS来实现,如果我们设置一个div让它从左往右动起来,就可以实现一个简单的动画效果。

1、CSS动画效果如下:

<style>
#dongqilai {
width: 100px;
height: 100px;
background-color: cadetblue;
margin-left:100px;*/
}
#dongqilai:hover{
margin-left: 100px;
transform: rotate(360deg);(旋转)
background-color: red;
transition: 0.5s;(过渡)
}
</style>

<body>
<div id="dongqilai" style="margin-left: 30px;"></div>

</body>

2、JS动画效果如下:

<script>

var x = document.getElementById("dongqilai");
alert(x.offsetLeft);(相对于父对象的左边距)
x.style.marginLeft = "100px";
alert(parseInt(x.style.marginLeft));

alert(window.screen.availWidth);
var a = window.setInterval("change()",15);
function change()
{
var x = document.getElementById("dongqilai");
var left = x.style.marginLeft;
left = parseInt(left);
left += 1;
if(left <= 500)
{
x.style.marginLeft = left + "px";
}
else
{
window.clearInterval(a);
}

}
</script>

 

posted @ 2017-03-23 15:04  梦深深处  阅读(670)  评论(0编辑  收藏  举报