我需要在鼠标悬停时增加图像不透明度,并在鼠标移出时减少图像不透明度,并且我需要它是渐进的,而不是像 CSS 那样即时。
<html>
<head>
</head>
<body>
<img id="test" src="space1.png" onmouseover="updateOpacity()"/>
<script type="text/javascript">
var element = document.getElementById('test');
var opacity = element.style.opacity;
function updateOpacity()
{
opacity = opacity += 0.2;
if(opacity >= 1)
{
// done
opacity = 1;
}
else
{
element.style.opacity = opacity; // this updates the opacity
setTimeout(function(){updateOpacity();}, 50); // 50 is the delay, 50milliseconds
}
}
</stript>
</body>
</html>