請問這個(gè)BUG是什么情況?http://idcbgp.cn/video/2879
<!DOCTYPE?html> <html> <head> <meta?charset="UTF-8"> <title></title> <style?type="text/css"> *?{ margin:?0; padding:?0; } #divs?{ height:?260px; width:?160px; background-color:?antiquewhite; border-radius:?10px; position:?relative; left:?-160px; } #spans?{ height:?100px; width:?20px; background-color:?cornflowerblue; border-radius:?5px; position:?absolute; left:?160px; margin-left:?1px; box-shadow:?5px?2px?10px?cornflowerblue; border:?1px?solid?white; } #div_opacity?{ height:?100px; width:?100px; background-color:?brown; border-radius:?100px; box-shadow:?5px?2px?10px?#A52A2A; border:?1px?solid?white; opacity:?0.1; } </style> <script?type="text/javascript"> window.onload?=?function()?{ var?div?=?document.getElementById("divs"), span?=?document.getElementById("spans"), div_o?=?document.getElementById("div_opacity"); var?speed?=?5; div.onmouseover?=?function()?{ star_1(speed,?div,?0); } div.onmouseleave?=?function()?{ star_1(speed,?div,?-160); } } var?timer?=?null; function?star_1(speed,?div,?itarget)?{ clearInterval(timer); timer?=?setInterval(function()?{ if?(div.offsetLeft?==?itarget)?{ clearInterval(timer); }?else?{ if?(itarget?==?0)?{ speed?=?speed; }?else?if?(itarget<0)?{ speed?=?-speed; } if?(div.offsetLeft?>?-160?/?2)?{ speed?=?speed?*?3; }?else?{ speed?=?speed; } div.style.left?=?div.offsetLeft?+?speed?+?"px"; //解決抖動 if?(div.offsetLeft?>?0)?{ clearInterval(timer); div.style.left?=?"0px"; } if?(div.offsetLeft?<?-160)?{ clearInterval(timer); div.style.left?=?"-160px"; } } },?50) } </script> </head> <body> <div?id="divs"><span?id="spans">分享</span></div> <div?id="div_opacity"></div> </body> </html>
在代碼的第68行,”speed=-speed“,當(dāng)onmouseleave事件觸發(fā),div就會不停彈跳。
如果是改成”speed=-5“就不會出錯(cuò),請問這是發(fā)生了啥?蟹蟹
2016-07-01
問題出現(xiàn)當(dāng) 觸發(fā)onmouseleave 時(shí),傳入speed=5,而star_1 中的局部變量speed=5,由于itarget=-160<0,所以,speed=-speed,即此時(shí)局部變量speed=-5,而下一個(gè)50ms,由于itarget=-160<0不變,繼續(xù)執(zhí)行speed=-speed,即此時(shí)局部變量speed=5了,到此發(fā)現(xiàn)問題了,在定時(shí)器的作用下,speed會在5與-5之間來回變動,有沒有達(dá)到任何 你設(shè)置的 停止條件,就出現(xiàn)了 無限制的抖動。而如果speed=-5,固定值 就不會出現(xiàn) 負(fù)負(fù)得正的情況了
2016-07-01
speed=-speed這么寫容易亂,你的目的大概是簡單的變換一下符號,第一次進(jìn)這個(gè)語句speed由5變?yōu)?5,下面*3之后變?yōu)?15,當(dāng)?shù)诙螆?zhí)行這個(gè)語句的時(shí)候speed就會由-15變?yōu)?5,這樣問題就出來了。建議你傳參數(shù)的時(shí)候參數(shù)名和局部變量名不要相同。