能幫我看下我的代碼嗎, 會出現小數不精確的現象,好著急求問
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>js透明度動畫</title>
<style type="text/css">
?body{
? margin:0px;
?}
?#div1{
? width:200px;
? height: 200px;
opacity: 0.3;
/* filter:alpha(opacity:30); */
background: red;
?}
</style>
<script type="text/javascript">
window.onload=function(){
var odiv=document.getElementById("div1");
odiv.onmouseover=function(){
startmove(1);
}
odiv.onmouseout=function(){
startmove(0.3);
}
}
var timer=null;
var alpha=0.3
function startmove(iTarget){
var odiv=document.getElementById("div1");
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
if (alpha>iTarget) {
speed=-0.1;
}
else{
speed=0.1;
}
if(alpha==iTarget){
clearInterval(timer);
}
else{
alpha+=speed;
/*odiv.style.filter='alpha(opacity:'+alpha+')'*/
odiv.style.opacity=alpha;
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
2014-11-25
alpha+=speed;在這句話后面加這句話alpha = alpha.toFixed(1)*1;就可以了。
我解釋一下這句話的意思toFixed(1)是取小數點后一位數字,而該方法返回的是一個字符串,那么在JS中字符串轉換成數值有兩種方式,一個是這種:字符串*1可以得到數字,前提字符串中是純數字,第二種是用parseInt()的方法去轉換,這里我比較懶,就用的第一種
2014-11-27
這節還沒有講到小數精確的問題,此課程是一點一點的滲透。??梢钥匆幌?span>Math 對象方法