myrtis
2015-09-19 22:55
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>shareicon</title>
</head>
<style type="text/css">
*{
margin:0px;padding:0px;font:14px "微软雅黑" normal;
}
#content{
position:absolute;
left:0px;
top:200px;
width:200px;
height:200px;
background-color: #cc3399;
opacity: 0.4;
}
#content span{
position:absolute;
left:200px;
top:37.5%;
width:20px;
height:25%;
padding-top:10px;
background-color: #99cc00;
text-align: center;
}
}
</style>
<script type="text/javascript">
window.onload=function(){
var mBox=document.getElementById("content");
mBox.onmouseover=function(){
changeOpacity(1);
}
mBox.onmouseout=function(){
changeOpacity(0.4);
}
var timer=null;
var mOpacity=0.4;
function changeOpacity(itarget){
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
if(mOpacity<itarget){
speed=0.1;
}else{
speed=-0.1;
}
if(mOpacity==itarget){
clearInterval(timer);
}else{
mOpacity+=speed;
mBox.style.opacity=mOpacity;
}
},300)
}
}
</script>
<body>
<div id="content">
<span>分享</span>
</div>
</body>
</html>
通过控制台输出当前透明度和speed如下:
0.4/0.1
0.5/0.1
0.6/0.1
0.7/0.1
0.7999999999999999/0.1
0.8999999999999999/0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/0.1
1.0999999999999999/-0.1
0.9999999999999999/-0.1
0.8999999999999999/-0.1
0.7999999999999999/-0.1
0.7/-0.1
0.6/-0.1
0.5/-0.1
貌似原因就出在这个莫名其妙出来的小数点上,无解..
把这些参数,值全都乘以100,最后在mBox.style.opacity=mOpacity/100,这样设置的话就不会出现你说的那种情况,可以把时间调小一点,流畅一点。
感觉这个应该是本生存在的bug,在没有改变其他东西的情况下,把你的源代码的定时器的时间调小之后,有时会出现这种现象有时不会,我也不懂怎么回事
JS动画效果
113910 学习 · 1502 问题
相似问题