2014-11-24 43 views
1

我使用這個JavaScript代碼,現在將一些雲彩,我需要檢查,如果用戶按下shift鍵 - 當他們這樣做,它停止這種雲回採一個JavaScript代碼上按shift鍵

的代碼我的動畫使用移動是:

<script language="javascript"> 

    function StartMove() { 
    var cssBGImage=new Image(); 
    cssBGImage.src="path to your image.jpg"; 

    window.cssMaxWidth=cssBGImage.width; 
    window.cssXPos=0; 
    setInterval("MoveBackGround()",50); 
    } 

    function MoveBackGround() { 
    window.cssXPos=window.cssXPos+1; 
    if (window.cssXPos>=window.cssMaxWidth) { 
     window.cssXPos=0; 
    } 
    toMove=document.getElementById("scroller"); 
    toMove.style.backgroundPosition=window.cssXPos+"px 0px"; 
    } 
</script> 

像這樣的代碼。注意這一個不和我一起工作

function GetShiftState (event) { 
    if (event.shiftKey) 
    { 
    document.getElementById("myimg").clearTimeout(t); 
    } 
} 

回答

0

你需要保持的interval

名稱的軌道看到MDN

var global_cloudInterval; 

function StartMove() { 
    var cssBGImage=new Image(); 
    cssBGImage.src="path to your image.jpg"; 

    window.cssMaxWidth=cssBGImage.width; 
    window.cssXPos=0; 

    // Keep track of the interval using a global variable 
    // also, don't need to wrap MoveBackGround in quotes 
    global_cloudInterval = setInterval(MoveBackGround, 50); 
} 

function MoveBackGround() { 
    window.cssXPos=window.cssXPos+1; 
    if (window.cssXPos>=window.cssMaxWidth) { 
     window.cssXPos=0; 
    } 
    toMove=document.getElementById("scroller"); 
    toMove.style.backgroundPosition=window.cssXPos+"px 0px"; 
} 

function GetShiftState (event){ 
    if (event.shiftKey){ 
     // now clear the interval using the global variable 
     clearInterval(global_cloudInterval); 
    } 
} 
此相關的例子
相關問題