2015-08-09 63 views
0

儘管我努力從一堆png文件中製作出高質量的gif動畫,但我仍然收到錯誤...我已經得到了很多像「document.getElementById不是函數」的東西,並且「Uncaught TypeError:無法設置屬性'src'爲null」。我決定在這裏問我做錯了什麼。這是一個古老的代碼,我發現的地方,我一直在嘗試使用:使用JavaScript製作一堆png文件的gif動畫效果?

<script> 

var min = 1; 
var max = 12; 
var current = min; 

var keep_switching_icon = true; 

function rotateIcon() { 
    if (keep_switching_icon) { 
    var nyanframe = document.getElementById('nyan') 
    nyanframe.src = ({path:"nyan" + current + ".png"}); 
    console.log(current); 
    if (current++ > max) { 
     current = min; 
    }; 

    window.setTimeout(rotateIcon, 300); 
    } 
} 

rotateIcon(); 

</script> 

<body> 

<img src="nyan1.png" id="nyan"></img> 

</body> 
+2

什麼是預期的結果代nyanframe.src = "nyan" + current + ".png";: 「nyan」+ current +「.png」});'? – guest271314

+0

謝謝,現在圖像正在更新,但它不會更改爲目錄中的圖像...我有名爲nyan1.png,nyan2.png,..... nyan12.png的圖像。檢查元素說圖像是'[object Object]'... – Mashpoe

+0

我希望它從nyan1.png更改爲nyan2.png,直到它到達nyan12.png,然後我希望它重新開始... – Mashpoe

回答

1

嘗試使用`nyanframe.src =({路徑nyanframe.src = ({path:"nyan" + current + ".png"});

var min = 1; 
var max = 12; 
var current = min; 

var keep_switching_icon = true; 

function rotateIcon() { 
    if (keep_switching_icon) { 
    var nyanframe = document.getElementById('nyan'); 
    nyanframe.src = "nyan" + current + ".png"; 
    console.log(current); 
    if (current++ > max) { 
     current = min; 
    }; 

    window.setTimeout(rotateIcon, 300); 
    } 
} 

window.onload = rotateIcon; 
+0

'eventListener'不是'window.onload = rotateIcon;'的更好選擇嗎? –