Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在特定像素处停止图像?_Javascript_Html_Css - Fatal编程技术网

Javascript 如何在特定像素处停止图像?

Javascript 如何在特定像素处停止图像?,javascript,html,css,Javascript,Html,Css,如何在特定像素处停止运动图像? 对javascript来说有点陌生 一直在尝试使用if-else,不确定输入的代码是否正确 var imgObj = null; var animate ; function init(){ imgObj = document.getElementById('xTank'); imgObj.style.position= 'relative'; imgObj.style.left = '0px'; } function moveRight(

如何在特定像素处停止运动图像? 对javascript来说有点陌生

一直在尝试使用if-else,不确定输入的代码是否正确

var imgObj = null;
var animate ;
function init(){
   imgObj = document.getElementById('xTank');
   imgObj.style.position= 'relative'; 
   imgObj.style.left = '0px'; 
}

function moveRight()
{
    if(imgObj<400){
        imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
        animate = setTimeout(moveRight,70); // call moveRight in 20msec
       }
    else
       {
        stopanimate = setTimeout(moveRight,70);
       }
}

window.onload =init;
var imgObj=null;
变量动画;
函数init(){
imgObj=document.getElementById('xTank');
imgObj.style.position='relative';
imgObj.style.left='0px';
}
函数moveRight()
{

如果(imgObj需要检查左侧样式值,而不是图像对象本身:

function moveRight()
{
    if(parseInt(imgObj.style.left, 10) < 400){
        imgObj.style.left = parseInt(imgObj.style.left, 10) + 10 + 'px';
        setTimeout(moveRight,70); // call moveRight in 20msec
       }
    else
       {
        //stopanimate = setTimeout(moveRight,70); // no need to animate it anymore
       }
}
函数moveRight()
{
if(parseInt(imgObj.style.left,10)<400){
imgObj.style.left=parseInt(imgObj.style.left,10)+10+'px';
setTimeout(moveRight,70);//在20秒内调用moveRight
}
其他的
{
//stopanimate=setTimeout(moveRight,70);//不再需要设置动画
}
}
另外,不要忘记在某个时候调用moveRight函数

请看这里: