为什么我要宽度颜色却变了呢?

来源:4-4 任意属性值(二)

善良阿呆

2016-08-10 22:31

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>咋地呢</title>

</head>

<style>

* {

margin: 0;

padding: 0;

}


div {

background-color: #0C6;

width: 150px;

height: 50px;

margin-top: 20px;

cursor: pointer;

border: solid 2px #000;

filter: alpha(opacity :   30);

opacity: 0.3;

}

</style>

<script>

window.onload = function() {

var div = document.getElementsByTagName("div");

for ( var i = 0; i < div.length; i++) {

div[i].timer = null;


div[i].onmouseover = function() {

startmove(this, 'width', 600);


}

div[i].onmouseout = function() {

startmove(this, 'width', 150);


}

}

function getStyle(obj, attr) {

if (obj.currentStyle) {

return obj.currentStyle[attr];

} else {

return getComputedStyle(obj, false)[attr];

}

}

function startmove(obj, attr, itarget) {

clearInterval(obj.timer);

obj.timer = setInterval(function() {

var icur = 0;

if (attr = 'opacity') {

icur = Math.round(parseFloat(getStyle(obj, attr)) * 100);

} else {

icur = parseInt(getStyle(obj, attr));

}

var speed = (itarget - icur) / 5;

speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

if (icur == itarget) {

clearInterval(obj.timer);

} else {

if (attr = 'opacity') {

obj.style.filter = 'alpha(opacity:' + (icur + speed)

+ ')';

obj.style.opacity = (icur + speed) / 100;

} else {

obj.style[attr] = icur + speed + 'px';

}

}


}, 30);

}


}

</script>


<body>

<div id="div1"></div>

<div id="div2"></div>

<div id="div3"></div>

<div id="div4"></div>

</body>

</html>


写回答 关注

1回答

  • Lady严
    2016-08-10 23:40:27
    已采纳

    if (attr = 'opacity')改为if (attr == 'opacity')

    =是赋值 你把opacity赋值给了attr属性,所以不管你设置什么属性,都是opacity透明度在改变

    善良阿呆

    非常感谢!

    2016-08-11 19:46:42

    共 2 条回复 >

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113925 学习 · 1443 问题

查看课程

相似问题