為什么speed直接用小數(shù)不可以,只能用整數(shù),speed/100才可以,用小數(shù)判斷出錯????
<!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>
2017-09-19
javascript的計算能力不強,用小數(shù)的方式來做數(shù)學運算容易得出錯誤結(jié)果,建議需要運算的代碼盡量避開小數(shù)。