2017-02-24 81 views
0

我有一些按鈕/ img,它顯示數據庫中項目的狀態。在盤旋時,如果src變成gif,那麼在動畫部分之後結束。一旦離開,另一個GIF來了。如果我進入並離開第一個按鈕,並且我用另一個按鈕做同樣的事情,在第二次離開時,所有兩個gif都會重新加載。同樣的情況也發生了兩次以上。如果其他Gif加載,Gif會再次啓動!我不爲什麼?

這裏一個例子: enter image description here

function lightimgchange (ID, Val) { 
 
\t \t var imgID = 'light' + ID; 
 
\t \t var elem = document.getElementById(imgID); 
 
\t \t if (Val == 1) { 
 
\t \t \t elem.src = "img/light_on_off.gif"; 
 
\t \t } 
 
\t \t else { 
 
\t \t \t elem.src = "img/light_off_on.gif"; 
 
\t \t } 
 
}
while ($reihe = mysql_fetch_array($light_db)) { 
 
\t \t if ($reihe['Status'] == 'Y') { 
 
    \t \t echo "<img src='img/light_on.png' alt='On' id='light".$reihe['ID']."' height='75px' width='75px' onmouseenter='lightimgchange(".$reihe['ID'].", 1)' onmouseleave='lightimgchange(".$reihe['ID'].", 0)' style='border-radius:20%;'>"; 
 
\t \t } 
 
\t \t else { 
 
\t \t echo "<img src='img/light_off.png' alt='Off' id='light".$reihe['ID']."' height='75px' width='75px' onmouseenter='lightimgchange(".$reihe['ID'].", 0)' onmouseleave='lightimgchange(".$reihe['ID'].", 1)' style='border-radius:20%;'>"; 
 
\t \t } 
 
}

回答

0

I`ve一招解決它。結束後,我將gif更改爲png。

Here`s js的溶液:

function lightimgchange (ID, Val) { 
    var imgID = 'light' + ID; 
    var elem = document.getElementById(imgID); 
    if (Val == 1) { 
     elem.src = "img/light_on_off.gif"; 
     setTimeout(function(){ 
      elem.src = "img/light_off.png"; 
     }, 450); 
    } 
    else { 
     elem.src = "img/light_off_on.gif"; 
     setTimeout(function(){ 
      elem.src = "img/light_on.png"; 
     }, 450); 
    } 
}