weibo_东东呛嘿_03463907
2016-08-05 13:41
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>单个物体适应各种属性变化(尤其opacity)</title>
<style>
*{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
ul li{
width: 200px;
height: 100px;
background: blue;
margin-bottom: 20px;
/*解决边框带来的BUG问题*/
border: 5px solid #000;
filter: alpha(opacity:30);
opacity: 0.3;
}
</style>
<script>
window.onload=function(){
var aLi1=document.getElementById('li1');
aLi1.onmouseover=function(){
startMove(this,'opacity',100);
}
aLi1.onmouseout=function(){
startMove(this,'opacity',30);
}
}
var alpha=30;
function startMove(obj,attr,target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
//注意这个当前宽度curl是会随speed变得,不能放在定时器外部
var curl=0;
if(attr='opacity'){
curl=parseFloat(getStyle(obj,attr))*100;
}else{
var curl=parseInt(getStyle(obj,attr));
}
var speed=(target-curl)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(curl==target){
clearInterval(obj.timer);
}
else{
if(attr='opacity'){
obj.style.filter='alpha(opacity:'+(curl+speed)+')';
obj.style.opacity=(curl+speed)/100;
}
else{
obj.style[attr]=curl+speed+'px';
}
}
},30)
}
function getStyle(obj,attr){
//currentStyle针对IE
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
//getComputedStyle 针对firefox浏览器
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li id="li1"></li>
</ul>
</body>
</html>
speed=speed>0?Math.ceil(speed):Math.floor(speed); 你已经约过了
可能是浏览器的原因吧 我的也可以
JS动画效果
113925 学习 · 1443 问题
相似问题
回答 6