求大神幫忙找錯(cuò),鼠標(biāo)移入透明值0.13,移出為0..14; ?誤差超大,不知道哪里出錯(cuò)了
<!doctype html>
<html>
?<head>
? <meta charset="UTF-8">
? <meta name="Generator" content="EditPlus?">
? <meta name="Author" content="">
? <meta name="Keywords" content="">
? <meta name="Description" content="">
? <title>任意屬性值</title>
? <style type="text/css">
? boy,
? div{margin:0; padding:0;}
? #div1{
?? ?width:400px;
?? ?height:400px;
?? ?border:2px solid black;
?? ?margin-bottom:20px;
?? ?background:yellow;
?? ?filter:alpha(opacity:30); /*IE瀏覽器設(shè)置透明度*/
?? ?opacity:0.3;??? /*火狐、谷歌瀏覽器*/
? }
? </style>
?</head>
?<body>
? <div id ="div1"></div>
?</body>
?<script>
?? ?window.onload = function(){
?? ??? ?var oDiv = document.getElementById("div1");
?? ??? ?oDiv.onmouseover = function(){
?? ??? ??? ?startMove(this,'opacity',100); //this為鼠標(biāo)在哪的對(duì)象
?? ??? ?}
?? ??? ?oDiv.onmouseout = function(){
?? ??? ??? ?startMove(this,'opacity',30);? //鼠標(biāo)離開(kāi)透明度變?yōu)?0;
?? ??? ?}
?? ?}
?? ?var timer = null;
?? ?function startMove(obj,attr,iTarget){ //傳入對(duì)象、屬性、目標(biāo)值
?? ??? ?clearInterval(obj.timer);? //清除當(dāng)前定時(shí)器
?? ??? ?obj.timer = setInterval(function(){
?? ??? ??? ?var icur = 0; //獲取當(dāng)前值
?? ??? ??? ?if(icur? == 'opacity'){ //屬性為透明度時(shí)
?? ??? ??? ??? ?icur = Math.round(parseFloat(getStyle(obj,attr))*100);
?? ??? ??? ??? ? // Math.round()函數(shù)為四舍五入
?? ??? ??? ?}
?? ??? ??? ?else{? //屬性為寬高時(shí)
?? ??? ??? ??? ?icur = parseInt(getStyle(obj,attr));
?? ??? ??? ??? ??? //parseInt()取整函數(shù)
?? ??? ??? ?}
?? ??? ??? ?var speed = (iTarget-icur)/8;
?? ??? ??? ?speed = speed > 0 ? Math.ceil(speed): Math.floor(speed);
?? ??? ??? ?if(icur == iTarget){
?? ??? ??? ??? ?clearInterval(obj.time);
?? ??? ??? ?}
?? ??? ??? ?else{
?? ??? ??? ??? ?if(attr == 'opacity'){? //是屬性是否為透明度
?? ??? ??? ??? ??? ?obj.style.filter = 'alpha(opacity:'+(icur+speed)+')';
?? ??? ??? ??? ??? ?//IE
?? ??? ??? ??? ??? ?obj.style.opacity = (icur+speed)/100; //緩沖速度
?? ??? ??? ??? ??? ?//Forefox
?? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ?obj.style[attr] = icur + speed +'px';
?? ??? ??? ??? ??? ?//屬性為寬高字體大小時(shí)
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?},30)
?? ?}
?? ?function getStyle(obj,attr){?? //獲取樣式
?? ??? ?if(obj.currentStyle){
?? ??? ??? ?return obj.currentStyle[attr];
?? ??? ?}else{
?? ??? ??? ?return getComputedStyle(obj,false)[attr];
?? ??? ?}
?? ?}
?</script>
</html>
2016-03-11