我正在使用此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 :(得分: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);
}
}