简体   繁体   English

此代码有什么问题?

[英]What is wrong with this code?

I am trying to use jquery animate function to animate boxes but this simple code seems to jerk me off. 我正在尝试使用jquery animate函数来对框进行动画处理,但是这个简单的代码似乎让我讨厌。 I am not sure what have I mistaken ? 我不确定我弄错了什么? Sometimes the similar code would work perfect and sometime it seems very difficult. 有时,类似的代码会完美地工作,有时似乎很难。

<!doctype html>

    <html>
        <head>
            <script src="jquery.js">
            </script>

            <style>
                #main{
                    clear:both;
                }
                #boxes{
                    position:absolute;
                    width:75%;
                }
                #box1, #box2, #box3{
                    position:relative;
                    width:200px;
                    height:200px;
                    background-color:#FFD600;
                    margin:10px;
                }
                #buttons{
                    float:right;
                }
                input{
                    display:block;
                    width:150px;
                }
            </style>

            <script>
            $('document').ready(function(){
                $("#left").bind('click', function(){
                    console.log('Left');
                    $('#box1').animate({x:"+=20px"});
                });

            });
            </script>

        </head>
        <body>
            <div id="main">
                <div id="boxes">
                    <div id="box1">

                    </div>

                    <div id="box2">

                    </div>

                    <div id="box3">

                    </div>
                </div>

                <div id="buttons">
                        <input type="button" value="Left" id="left">
                    <input type="button" value="Right" id="right">
                    <input type="button" value="Top" id="top">
                    <input type="button" value="Bottom" id="bottom">
                    <input type="button" value="Height" id="height">
                    <input type="button" value="Width" id="width">
                </div>
            </div>
        </body>
    </html>

Try it on JSFiddle. 试试吧上的jsfiddle。

x isn't a CSS property. x不是CSS属性。 Try left : left尝试:

$('#box1').animate({left:"+=20px"});

Try it on JSFiddle. 试试吧上的jsfiddle。

Try this it works perfectly : 试试这个,它完美地工作:

$("#left").click(function(){
console.log('Left');
$('#box1').animate({left: '+=50'});

}); });

x: "+=20px"

should be: 应该:

left: "+=20px"

JSFIDDLE DEMO JSFIDDLE演示

Change this line: 更改此行:

$('#box1').animate({x:"+=20px"});

to this: 对此:

$('#box1').animate({left:"+=20px"});

The positions in CSS are top and left , not x and y. CSS中的位置在topleft ,而不是x和y。 Works here: http://jsfiddle.net/jfriend00/47vMV/ . 在这里工作: http : //jsfiddle.net/jfriend00/47vMV/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM