代碼如下。如果快速大幅度的劃過鼠標(biāo),div便會消失。求解
<!doctype html>
<html>
<head>
??? <meta charset="UTF-8">
??? <title>下拉菜單</title>
?? ?<style type="text/css">
#div1{width: 200px; height: 200px; background: red; line-height: 100px; position: relative; left: 200px; top: 0px; filter: alpha(opacity:30); opacity: 0.3; top: 50px;}
body{ margin:0; padding:0;}
?? ?</style>
?? <script type="text/javascript">
?? window.onload=function(){
?? var oDiv=document.getElementById("div1");
?? oDiv.onmouseover=function(){
? ??? ?stareMove(100);
?? }
?? oDiv.onmouseout=function(){
? ??? ?stareMove(20);
?? }
? ??? ?var timer=null,
? ??? ?? ??? ?filter=30;
? ??? ?function stareMove(iTarget){
? ??? ??? ?clearInterval(timer);
? ??? ??? ?var oDiv=document.getElementById("div1");
? ??? ??? ?var speed=0;
? ??? ??? ?if (filter<iTarget) {
? ??? ??? ??? ?speed=10;
? ??? ??? ?}
? ??? ??? ?else{
? ??? ??? ??? ?speed=-10;
? ??? ??? ?}
??????? timer=setInterval(function(){
?????? ??? ?filter+=speed;
?????? ??? ?if (filter==iTarget) {
?????? ??? ??? ?clearInterval(timer);
?????? ??? ?}
?????? ??? ?else{
?????? ??? ??? ?oDiv.style.filter='alpha(opacity:filter)';
?????? ??? ??? ?oDiv.style.opacity=filter/100;
?????? ??? ?}
??????? },30)
? ??? ?}
?? }
?? </script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
2016-12-14
首先,如果快速大幅度的劃過鼠標(biāo),div便會消失,實(shí)際上是一種錯覺,div并沒有消失,而是透明度值變到0以下了,你看不到了div的背景顏色了而以.
2016-12-10
首先filter+=speed;應(yīng)該放在else里面,否則透明度顯示范圍(0.3,0.9),而我要求的是(0.2,1)。但是我不明白filter+=speed;放在else外,鼠標(biāo)小浮動的劃過,正常。當(dāng)大幅度快速劃過,透明度便一直在減小,就像沒有走clearInterval(timer);一直執(zhí)行else語句。