求助結(jié)果有點(diǎn)問題!
我的目標(biāo)是鼠標(biāo)滑過,width=400,height=400,opactiy=1,鼠標(biāo)移除,回歸原樣,width=200,height=200,opacity=0.3;但是我的運(yùn)行出來鼠標(biāo)滑過width=396,height=396,opactiy=1,移除width=204,height=204,opactiy=0.3,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>練習(xí)鏈?zhǔn)竭\(yùn)動(dòng)</title>
<style type="text/css">
*{margin:0; ?padding: 0}
div{
height:200px;
width: 200px;
background: #fc6;
filter: alpha(opacity=30);
opacity: 0.3;
}
</style>
<script src="js/js.js "></script>
<script type="text/javascript">
window.onload=function(){
var div0=document.getElementById('div0');
div0.onmouseover=function(){
startMove(this,{'width':400,'height':400,'opacity':100});
}
div0.onmouseout=function(){
startMove(this,{'width':200,'height':200,'opacity':30});
}
}
</script>
</head>
<body>
<div id="div0"></div>
</body>
</html>
function getStyle(obj, attr) {
? ? if (obj.currentStylt) {
? ? ? ? return obj.currentStyle[attr];
? ? } else {
? ? ? ? return getComputedStyle(obj, false)[attr];
? ? }
}
function startMove(obj, json, fn) {
? ? clearInterval(obj.timer);
? ? obj.timer = setInterval(function() {
? ? ? ? for (var attr in json) {
? ? ? ? ? ? // 獲取當(dāng)前值
? ? ? ? ? ? var icurr = 0,
? ? ? ? ? ? ? ? flag = true;
? ? ? ? ? ? if (attr == 'opacity') {
? ? ? ? ? ? ? ? icurr = Math.round(parseFloat(getStyle(obj, attr) * 100));
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? icurr = parseInt(getStyle(obj, attr));
? ? ? ? ? ? }
? ? ? ? ? ? // 算速度
? ? ? ? ? ? var speed = (json[attr] - icurr) / 6;
? ? ? ? ? ? speed = (speed > 0 ? Math.ceil(speed) : Math.floor(speed));
? ? ? ? ? ? // 測試停止
? ? ? ? ? ? if (icurr != json[attr]) {
? ? ? ? ? ? ? ? flag = false;
? ? ? ? ? ? }
? ? ? ? ? ? if (attr == 'opacity') {
? ? ? ? ? ? ? ? obj.style.filter = 'alpha(opacity=' + (icurr + speed) + ')';
? ? ? ? ? ? ? ? obj.style[attr] = (icurr + speed) / 100;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? obj.style[attr] = (icurr + speed) + 'px';
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(flag){
? ? ? ? clearInterval(obj.timer);
? ? ? ? }
? ? ? ? if(fn){
? ? ? ? fn();
? ? ? ? }
? ? ? ? //?
? ? }, 30);
}
哪里出問題了
2016-08-05
把flag=true寫道for循環(huán)上面去