JavaScript实现透明度的改变 发表于 2017-09-26 | 分类于 HTML 鼠标的移入移出,显示的透明度在一定时间内发生的改变 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455<html><head> <title></title> <meta charset="utf-8"><style>#div1{ width:200px; height:200px; background:red; opacity:0.5;</style><script>window.onload=function(){ var oDiv=document.getElementById('div1'); oDiv.onmouseover=function(){ startMove(30); } oDiv.onmouseout=function(){ startMove(100); }}var timer=null;var alpha=30;function startMove(itarget){ clearInterval(timer); var oDiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function(){ var speed=0; if(alpha>itarget){ speed=-10; } else { speed=10; } if(alpha==itarget){ clearInterval(timer); } else{ alpha+=speed; oDiv.style.opacity=alpha/100; } },30)}</script></head><body><div id ="div1"> </div></body></html>