OOP淡入淡出
Posted 我将枕中记忆抹去任岁月浮光掠影
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OOP淡入淡出相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>OOP淡入淡出</title> <style type="text/css"> div { width: 200px; height: 160px; margin: auto; border: 20px solid springgreen; background-color: palevioletred; filter: alpha(opacity: 30); opacity: 0.3; } </style> <script type="text/javascript"> function FnGuo(oDiv){ var _this = this; this.nowAlpha = 30; this.oTimer = null; oDiv.onmouseover = function(){ _this.fnOpacity(oDiv,100); }; oDiv.onmouseout = function(){ _this.fnOpacity(oDiv,30); }; } FnGuo.prototype.fnOpacity = function(oDiv,tarAlpha){ clearInterval(this.oTimer); //注意setInterval函数里面的this是定时器本身,如果访问类FnGuo的成员变量比如nowAlpha,必须在函数setInterval外保存this。 var _this = this; this.oTimer = setInterval(function(){ if(tarAlpha > _this.nowAlpha) { _this.nowAlpha += 2; oDiv.style.filter = ‘alpha(opacity:‘+ _this.nowAlpha +‘)‘; oDiv.style.opacity = _this.nowAlpha/100; } else if(tarAlpha < _this.nowAlpha) { _this.nowAlpha -= 2; oDiv.style.filter = ‘alpha(opacity:‘+ _this.nowAlpha +‘)‘; oDiv.style.opacity = _this.nowAlpha/100; } else { //clearInterval(_this.oTimer); } },30); }; window.onload = function(){ var oDiv = document.getElementsByTagName("div")[0]; var oFnGuo = new FnGuo(oDiv); }; </script> </head> <body> <div></div> </body> </html>
以上是关于OOP淡入淡出的主要内容,如果未能解决你的问题,请参考以下文章