poudoudou
2016-10-12 16:15
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div {
width: 1000px;
height: 300px;
background: #0e90d2;
filter: alpha(opacity:30);
opacity: 0.3
}
</style>
</head>
<body>
<div>
</div>
<script type="text/javascript">
window.onload = function () {
var oDiv = document.getElementsByTagName('div')[0],
timer = null;
oDiv.onmouseover = function () {
startMove('opacity',100);
}
oDiv.onmouseout = function () {
startMove('opacity',30);
}
}
function startMove(attr,iTarget) {
clearInterval(timer);
timer = setInterval(function(){
var oDiv = document.getElementsByTagName('div')[0],
var icur=0;
if(attr=='opacity'){
icur=Math.round(parseFloat(getStyle(oDiv,attr))*100);
}
else{
icur=parseInt(getStyle(oDiv,attr));
}
var speed=(iTarget-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(icur==iTarget){
clearInterval(timer);
}
else{
if(attr=='opacity'){
icur+=speed;
oDiv.style.filter='alpha(opacity:'+icur+')';
oDiv.style.opacity=icur/100;
}
else{
oDiv.style[attr]=icur+speed+'px'
}
}
}, 30)
}
//获取css样式
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
}
else {
return getComputedStyle(obj, null)[attr];
}
}
</script>
</body>
</html>
如加入obj参数 则
oDiv.onmouseover = function () {
startMove(this,'opacity',100);
}
在这里 我不用obj的参数可以么 直接默认为oDiv 不知道是不是这个原因?
理论上没什么问题,但是不能应用于多物体运动
谢谢亲的答复 但是在不传入obj的参数的情况下 之前已经有定义timer=null,因此之后可以开始就可以clearsetinterval 不知道对不对。。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> *{ margin: 0; padding: 0; } div { width: 1000px; height: 300px; background: #0e90d2; filter: alpha(opacity:30); opacity: 0.3 } </style> </head> <body> <div> </div> <script type="text/javascript"> window.onload = function () { var oDiv = document.getElementsByTagName('div')[0], timer = null; oDiv.onmouseover = function () { startMove(this,'opacity',100); } oDiv.onmouseout = function () { startMove(this,'opacity',30); } } function startMove(obj,attr,iTarget) { clearInterval(obj.timer); obj.timer = setInterval(function(){ var oDiv = document.getElementsByTagName('div')[0]; var icur=0; if(attr=='opacity'){ icur=Math.round(parseFloat(getStyle(oDiv,attr))*100); } else{ icur=parseInt(getStyle(oDiv,attr)); } var speed=(iTarget-icur)/8; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(icur==iTarget){ clearInterval(obj.timer); } else{ if(attr=='opacity'){ icur+=speed; oDiv.style.filter='alpha(opacity:'+icur+')'; oDiv.style.opacity=icur/100; } else{ oDiv.style[attr]=icur+speed+'px' } } }, 30) } //获取css样式 function getStyle(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj, null)[attr]; } } </script> </body> </html>
没听明白你说什么
JS动画效果
113925 学习 · 1443 问题
相似问题