<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>透明度动画</title> <style type="text/css"> .div{ width: 300px; height: 300px; background-color: blue; opacity: 0.5; } </style> <script type="text/javascript"> window.onload=function(){ var div = document.getElementById("div") div.onmouseover=function(){ startMove(1); } div.onmouseout=function(){ startMove(0.5); } } var timer; var opacity=0.5; function startMove(itarget){ clearInterval(timer) var div = document.getElementById("div") timer = setInterval(function(){ var speed = 0; if ( div.style.opacity>itarget){ speed=-0.5; }else { speed=0.5; } if(opacity==itarget){ clearInterval(timer) }else { opacity +=speed; div.style.opacity=opacity; } },30) } </script> </head> <body> <div class="div"></div> </body> </html>
木子舟义
相关分类