2393611203
2016-05-10 22:19
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>获取样式,任何属性opacity</title>
<style>
*{
padding:0;
margin:0;
list-style:none;
}
#box{
background:orange;
width:200px;
height:200px;
opacity:0.3;
filter:alpha(opacity:30);
margin-bottom:20px;
}
</style>
<script>
window.onload=function(){
var div=document.getElementById("box");
div.onmouseover=function(){
change(this,100,'opacity');
}
div.onmouseout=function(){
change(this,30,'opacity');
}
}
function getStyle(obj attr){
if(obj.currentStyle)
{
obj.currentStyle[attr];
}
else{
getComputedStyle(obj,false)[attr];
}
}
function change(obj,target,attr){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var cur=0;
if(attr=='opacity')
{
cur=Math.round(parseFloat(getStyle(obj,attr)))*100;
}
else{
cur=parseInt(getStyle(obj,attr));
}
var speed=(target-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(cur==target)
{
clearInterval(obj.timer);
}
else{
if(attr=='opacity')
{
obj.style[opacity]=cur+speed;
}
else{
obj.style.opacity=(cur+speed)/100;
obj.style.filter='alpha(opacity:'+(cur+speed)+')';
}
}
},30)
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
function getStyle(obj attr){
if(obj.currentStyle)
{
obj.currentStyle[attr];
}
else{
getComputedStyle(obj,false)[attr];
}
}
函数缺少返回值。。。 return 你都没写上。
cur=Math.round(parseFloat(getStyle(obj,attr)))*100; 这里多了一个括号
JS动画效果
113925 学习 · 1443 问题
相似问题