慕粉3601794
2017-09-18 09:38
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style type="text/css">
#one{
width:200px;
height:200px;
opacity: 0.3;
background: red;
}
</style>
<script type="text/javascript">
window.onload=function(){
var one=document.getElementById("one");
one.onmouseover=function(){
startMove(1);
}
one.onmouseout=function(){
startMove(0.3);
}
}
var timer=null;
var alpha=0.3;
function startMove(target){
var one=document.getElementById("one");
clearInterval(timer);
var speed=0;
if(alpha>target){
speed=-0.1;
}else{
speed=0.1;
}
timer=setInterval(function(){
if(alpha==target){
clearInterval(timer);
}
else{
alpha+=speed;
one.style.opacity=alpha;
}
},30);
}
</script>
<body>
<div id="one"></div>
</body>
</html>
javascript的计算能力不强,用小数的方式来做数学运算容易得出错误结果,建议需要运算的代码尽量避开小数。
JS动画效果
113925 学习 · 1443 问题
相似问题