您的位置:首页 > Web前端 > JavaScript

js实现鼠标滑入滑出背景透明度改变

2015-12-20 23:36 716 查看
js实现鼠标滑入滑出背景透明度改变


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分享</title>
</head>
<style type="text/css">
body{padding:0;margin:0;}
#n_content{width:200px;height:200px;background:red;opacity:0.3;filter:Alpha(opacity=30);}

</style>
<body>
<div id="n_content"></div>
<script type="text/javascript">
window.onload = function(){
var odiv = document.getElementById("n_content");
odiv.onmouseover = function(){
startMove(100);
}
odiv.onmouseout = function(){
startMove(30);
}
var timer = null;
var alpha = 30;
function startMove(iTarget){
var odiv = document.getElementById("n_content");
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.filter = 'alpha(opacity='+ alpha +')';
odiv.style.opacity= alpha/100;
}

},30);
};

}
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: