纯js实现图片匀速淡入淡出效果
程序员文章站
2022-04-29 08:03:44
图片匀速淡入淡出效果如下:
...
图片匀速淡入淡出效果如下:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>淡入效果</title> <style> * { margin: 0; padding: 0; } div { border: 2px solid #aaa; } img { width: 300px; height: 300px; filter: alpha(opacity:30); opacity: .3; margin: 0 auto; } </style> </head> <body> <div> <img src="img/timg.jpg" alt="" id="img"> </div> <script> var alpha=30; var img = document.getelementbyid("img"); img.onmouseover=function(){ startmove(100) }; img.onmouseout=function(){ startmove(30) } var timer; function startmove(tar) { var img = document.getelementbyid("img"); clearinterval(timer); timer = setinterval(function () { var ispeed=0; ispeed= alpha<tar?5:-5; if(alpha==tar){ clearinterval(timer) } else{ alpha+=ispeed; img.style.filter="alpha(opacity:"+alpha+")"; img.style.opacity=alpha/100; } }, 30) } </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。