<!DOCTYPE?html>
<html>
<head>
<meta?charset="UTF-8">
<title>鏈?zhǔn)酵竭\(yùn)動</title>
<style?type="text/css">
#div1?{
margin:0?auto;
width:200px;
height:200px;
background:red;
filter:alpha(opacity=100);
opacity:1;
}
#div2?{
margin:20px?auto;
width:200px;
height:200px;
background:green;
filter:alpha(opacity=100);
opacity:1;
}
</style>
</head>
<body>
<div?id="div1"></div>
<div?id="div2"></div>
<script?type="text/javascript">
var?div1?=?document.getElementById("div1");
var?div2?=?document.getElementById("div2");
div1.onmouseover?=?function()?{
change(this,{"width":400,"height":300,"opacity":20},function()?{
change(this,{"width":200,"height":200,"opacity":100},function()?{
change(this,{"width":400,"height":400,"opacity":10});
})
})
}
div1.onmouseout?=?function()?{
change(this,{"width":400,"height":300,"opacity":20},function()?{
change(this,{"width":300,"height":300,"opacity":60},function()?{
change(this,{"width":200,"height":200,"opacity":100});
})
})
}
div2.onmouseover?=?function()?{
change(this,{"width":400,"height":300,"opacity":10},function()?{
change(this,{"width":200,"height":200,"opacity":100},function()?{
change(this,{"width":400,"height":400,"opacity":10});
})
})
}
div2.onmouseout?=?function()?{
change(this,{"width":400,"height":300,"opacity":20},function()?{
change(this,{"width":300,"height":300,"opacity":60},function()?{
change(this,{"width":200,"height":200,"opacity":100});
})
})
}
function?change(obj,target,callback)?{
//首先清除上一個定時器
clearInterval(obj.timer);
//定義變量
var?origin={};
var?speed?=?0;
var?flag?=?true;
//遍歷獲取初始屬性
for(attr?in?target)?{
if(?attr?===?"opacity"?)?{
origin[attr]?=?Math.round(getStyle(obj,attr)*100);
}?else?{
origin[attr]?=?parseInt(getStyle(obj,attr));
}
}
//設(shè)置定時器
obj.timer?=?setInterval(function()?{
//初始化標(biāo)記true
flag?=?true;
//遍歷目標(biāo)值
for(?attr?in?target)?{
//計算速度
speed?=?(target[attr]-origin[attr])/10;
speed?=?speed>0?Math.ceil(speed):Math.floor(speed);
//改變初始數(shù)值
origin[attr]?+=?speed;
if(attr?===?"opacity")?{
obj.style.opacity?=?origin[attr]/100;
obj.style.filter?=?'alpha(opacity='+origin[attr]+')';
}?else?{
obj.style[attr]?=?origin[attr]?+?"px";
}
//若有一個不等則將標(biāo)記設(shè)為false
if(target[attr]?!=?origin[attr])?{
flag?=?false;
}
}
console.log("定時器清除了沒?");
//如果標(biāo)記值為true清除定時器
if(flag)?{
clearInterval(obj.timer);
//設(shè)置回調(diào)函數(shù)
if(callback)?{
callback.call(obj);
}
}
},30);
}
//獲取元素外聯(lián)和內(nèi)聯(lián)樣式
function?getStyle(obj,attr)?{
return?obj.currentStyle???obj.currentStyle[attr]?:?getComputedStyle(obj,false)[attr];
}
</script>
</body>
</html>
2016-11-11
題主的程序測著沒問題啊。。
2016-11-06
<!DOCTYPE html>
<html>
? ? <head>
? ? ? ? <meta charset="UTF-8">
? ? ? ? <title>鏈?zhǔn)酵竭\(yùn)動</title>
? ? ? ? <style type="text/css">
? ? ? ? ? ? #div1 {
? ? ? ? ? ? ? ? margin:0 auto;
? ? ? ? ? ? ? ? width:100px;
? ? ? ? ? ? ? ? height:100px;
? ? ? ? ? ? ? ? background:red;
? ? ? ? ? ? ? ? filter:alpha(opacity=100);
? ? ? ? ? ? ? ? opacity:1;
? ? ? ? ? ? }
? ? ? ? ? ? #div2 {
? ? ? ? ? ? ? ? margin:20px auto;
? ? ? ? ? ? ? ? width:100px;
? ? ? ? ? ? ? ? height:100px;
? ? ? ? ? ? ? ? background:green;
? ? ? ? ? ? ? ? filter:alpha(opacity=100);
? ? ? ? ? ? ? ? opacity:1;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <div id="div1"></div>
? ? ? ? <div id="div2"></div>
? ? ? ? <script type="text/javascript">
? ? ? ? ? ? var div1 = document.getElementById("div1");
? ? ? ? ? ? var div2 = document.getElementById("div2");
? ? ? ? ? ? div1.onmouseover = function() {
? ? ? ? ? ? ? ? change(this,{"width":200,"height":200,"opacity":20})
? ? ? ? ? ? }
? ? ? ? ? ? div1.onmouseout = function() {
? ? ? ? ? ? ? ? change(this,{"width":100,"height":100,"opacity":100})
? ? ? ? ? ? }
? ? ? ? ? ? div2.onmouseover = function() {
? ? ? ? ? ? ? ? change(this,{"width":200,"height":200,"opacity":10})
? ? ? ? ? ? }
? ? ? ? ? ? div2.onmouseout = function() {
? ? ? ? ? ? ? ? change(this,{"width":100,"height":100,"opacity":100})
? ? ? ? ? ? }
? ? ? ? ? ? function change(obj,target,callback) {
? ? ? ? ? ? ? ? //首先清除上一個定時器
? ? ? ? ? ? ? ? clearInterval(obj.timer);
? ? ? ? ? ? ? ? //定義變量
? ? ? ? ? ? ? ? var origin={};
? ? ? ? ? ? ? ? var speed = 0;
? ? ? ? ? ? ? ? var flag = true;
? ? ? ? ? ? ? ? //遍歷獲取初始屬性
? ? ? ? ? ? ? ? for(attr in target) {
? ? ? ? ? ? ? ? ? ? if( attr === "opacity" ) {
? ? ? ? ? ? ? ? ? ? ? ? origin[attr] = Math.round(getStyle(obj,attr)*100); ? ? ??
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? origin[attr] = parseInt(getStyle(obj,attr)); ? ??
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //設(shè)置定時器
? ? ? ? ? ? ? ? obj.timer = setInterval(function() {
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? //初始化標(biāo)記true
? ? ? ? ? ? ? ? ? ? flag = true;
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? //遍歷目標(biāo)值
? ? ? ? ? ? ? ? ? ? for( attr in target) {
? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? //計算速度
? ? ? ? ? ? ? ? ? ? ? ? speed = (target[attr]-origin[attr])/10;
? ? ? ? ? ? ? ? ? ? ? ? speed = speed>0?Math.ceil(speed):Math.floor(speed);
? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? //改變初始數(shù)值
? ? ? ? ? ? ? ? ? ? ? ? origin[attr] += speed;
? ? ? ? ? ? ? ? ? ? ? ? if(attr === "opacity") {
? ? ? ? ? ? ? ? ? ? ? ? ? ? obj.style.opacity = origin[attr]/100;
? ? ? ? ? ? ? ? ? ? ? ? ? ? obj.style.filter = 'alpha(opacity='+origin[attr]+')';
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? obj.style[attr] = origin[attr] + "px";
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? //若有一個不等則將標(biāo)記設(shè)為false
? ? ? ? ? ? ? ? ? ? ? ? if(target[attr] != origin[attr]) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? flag = false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? console.log("定時器清除了沒?");
? ? ? ? ? ? ? ? ? ? //如果標(biāo)記值為true清除定時器
? ? ? ? ? ? ? ? ? ? if(flag) {
? ? ? ? ? ? ? ? ? ? ? ? clearInterval(obj.timer);
? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置回調(diào)函數(shù)
? ? ? ? ? ? ? ? ? ? ? ? if(callback) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? callback.call(obj);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? },30);
? ? ? ? ? ? }
? ? ? ? ? ? ?
? ? ? ? ? ? //獲取元素外聯(lián)和內(nèi)聯(lián)樣式
? ? ? ? ? ? function getStyle(obj,attr) {
? ? ? ? ? ? ? ? return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,false)[attr];
? ? ? ? ? ? }
? ? ? ? </script>
? ? </body>
</html>