Javascript jqueryanimate-isn';行不通

Javascript jqueryanimate-isn';行不通,javascript,jquery,css,Javascript,Jquery,Css,我知道这是张贴在这里多次其他时间,我看了每一个,并修改了我的代码无数次,但我似乎无法让我的动画功能的工作。我试图得到一张飞机在屏幕上从左向右移动的图像,中途停下来。这是我目前的代码: HTML: JavaScript/jQuery: function animatePlane() { var width = $(document).width / 2; $("#plane").animate({ left: '+=' + width }, 500); }

我知道这是张贴在这里多次其他时间,我看了每一个,并修改了我的代码无数次,但我似乎无法让我的动画功能的工作。我试图得到一张飞机在屏幕上从左向右移动的图像,中途停下来。这是我目前的代码:

HTML:

JavaScript/jQuery:

function animatePlane() {
    var width = $(document).width / 2;
    $("#plane").animate({
        left: '+=' + width
    }, 500);
}
问题是飞机根本不动。我试过移动div和图像。我已尝试将其分配为$(“#平面”)。单击。我尝试了更多的东西。这是我第一次尝试使用jQuery来移动一些东西,所以我可能遗漏了一些简单的东西。是的,函数正在被调用。谢谢你的帮助

这个

var width = $(document).width / 2;
返回
NaN
,因为从未调用
width
函数,这可能是您想要的

var width = $(window).width() / 2;
这个

var width = $(document).width / 2;
返回
NaN
,因为从未调用
width
函数,这可能是您想要的

var width = $(window).width() / 2;

width
在jQuery中不是函数属性

function animatePlane() {
    var width = $(document).width() / 2;
    $("#plane").stop().animate({
        left: '+=' + width
    }, 500);
}

width
不是jQuery中的函数属性

function animatePlane() {
    var width = $(document).width() / 2;
    $("#plane").stop().animate({
        left: '+=' + width
    }, 500);
}
$(document).width()
需要作为函数调用,您需要在方法名称后添加开始括号和结束括号
()

工作示例:

函数animatePlane(){
var width=$(文档).width()/2;
$(“#平面img”)。设置动画({
左:宽度+像素
}, 500);
}
#平面img{
位置:相对位置;
}


为图像设置动画
$(document).width()
需要作为函数调用,您需要在方法名称后添加开始括号和结束括号
()

工作示例:

函数animatePlane(){
var width=$(文档).width()/2;
$(“#平面img”)。设置动画({
左:宽度+像素
}, 500);
}
#平面img{
位置:相对位置;
}


为图像设置动画