JQuery动画不起作用.可能是不正确的语法

JQuery animation not working. possibly incorrect syntax

本文关键字:不正确 语法 动画 不起作用 JQuery      更新时间:2023-09-26

我很不明白为什么这个基本的JQuery代码不起作用,我只想在点击按钮时将文本向右移动500px。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src = "jquery-2.0.1.js"></script>
<script>
$(document).ready(function(){
$('button').click(function(){
    $('div').animate({right:'500px'}, fast);
 });
});
</script>
</head>
<body>
<div style = "position: absolute; top: 100px;">
    Test Box
</div>
<button>click me</button>
</body>
</html>

在部分中:$('div').animate({right:'500px'}, fast); fast必须是:"fast"
它对我有用!

在这里,代码写得像"fast"是一个var:

     $('div').animate({right:'500px'}, fast);

"速度"参数应该在引号中,如下所示:

     $('div').animate({right:'500px'}, 'fast');

这样就可以了。

单词fast需要用引号括起来。或者传入以毫秒为单位的int值。

http://jsfiddle.net/Buwn9/

$('button').click(function(){
    $('div').animate({right:'500px'}, 'fast');
 });
$('div').animate({right:'500px'}, 'fast');

在这种情况下,效果应该是一个字符串,我们可以在下面的中给出数字

$('div').animate({right:'500px'}, 500);