【问题标题】:jQuery animation not runningjQuery动画没有运行
【发布时间】:2012-05-18 03:38:34
【问题描述】:

这是我的 HTML 页面:

<!DOCTYPE html>
<html lang="en">

    <head>

        <title>Example</title>

        <meta charset="utf-8">

        <link rel="stylesheet" type="text/css" href="content/main.css">

        <script src="content/jquery.js" type="text/javascript"></script>
        <script type="text/javascript">
        $("#container").animate({
            'background-position': '1110px 1110px'
        }, 1000, function () {
        });
        </script>

    </head>

    <body>

        <div id="container">

            <p>hello</p>

        </div>

    </body>

</html>

div 容器应该使用该 JavaScript 代码进行动画处理,但在打开页面时没有任何反应。我在这里做错了什么?

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    您的代码在 DOM 树完全构建之前运行。所以你需要用

    $(function() { // <--- begin of the wrap
    
            $("#container").animate({
                'background-position': '1110px 1110px'
            }, 1000, function () {
            });
    
    }); // <---- end of the wrap
    

    http://api.jquery.com/ready/

    【讨论】:

    • 我似乎已经通过迁移到 jQuery 1.4.4 让它工作了,但是这个动画代码只移动了一次背景,它可以循环吗?
    • 将您的动画代码包装在一个函数中并在动画回调调用本身中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2012-06-23
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多