how to smooth jquery animations

前端 未结 4 1484
走了就别回头了
走了就别回头了 2020-12-17 05:19

I would like to smooth the chaining of some jquery.animate functions.

Here is a jsfiddle where I describe the problem : http://jsfiddle.net/xavier_seignard/KTxbb/4/<

相关标签:
4条回答
  • 2020-12-17 05:42

    Is it the change in speed that you're describing?

    That is because the animations have the same timings but the distance the square box is covering differs. You might need to alter the time for each animation dependant in the distance travelled.

    0 讨论(0)
  • 2020-12-17 05:46

    Thats the problem with jsfiddle.. I tested your jsfiddle link and it was not looking great as you mentioned in your question.

    But then I created new page on my pc and copied everything from your fiddle and checked it. It looks alright.

    Copy and paste this and save it as html and test it :

    <html>
    <head>
      <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
      <link rel="stylesheet" type="text/css" href="http://fiddle.jshell.net/css/normalize.css">
        <style type="text/css">
        #point {
        position: absolute;
        background-color: black;
        width: 15px;
        height: 15px
    }
        </style>
    </head>
    <body onload="initPage()">
        <div class="start" id="point"></div>
    <script type="text/javascript">
        var json = [
                    {'x' : '300' , 'y' : '200'},
                    {'x' : '250' , 'y' : '150'},
                    {'x' : '209' , 'y' : '387'},
                    {'x' : '217' , 'y' : '323'},
                    {'x' : '261' , 'y' : '278'},
                    {'x' : '329' , 'y' : '269'},
                    {'x' : '406' , 'y' : '295'}
                ];
    
    
    function initPage() {
        $.each(json, function() {
            $("#point").animate({
                left: this.x,
                top: this.y
            },
            "linear");
        });
    }
    </script>
        </body>
    
    </html>
    
    0 讨论(0)
  • 2020-12-17 05:47

    You can change the speed for a more "fine" animation, you see that stop because the speed it's too fast and different size to cover:

    function initPage() {
        $.each(json, function() {
            $("#point").animate({
                left: this.x,
                top: this.y
            },
            1000, 'linear');
        });
    }​
    
    0 讨论(0)
  • 2020-12-17 05:56

    I think you should just try out the jQuery Easing plugin - http://gsgd.co.uk/sandbox/jquery/easing/

    Include the file and instead of "liner" add some other easing.

    0 讨论(0)
提交回复
热议问题