简体   繁体   English

简化jQuery动画

[英]Adding ease to a jquery animation

Hi i found this animation, which i would like to use to give my navigation links a tiny, gentle random movement, but it just doesn't look very smooth. 嗨,我发现了这个动画,我想用它给我的导航链接一个细微,柔和的随机运动,但是看起来并不十分流畅。 http://jsfiddle.net/2TUFF/ http://jsfiddle.net/2TUFF/

( Random Movement in a Fixed Container ) 在固定容器中随机移动

    $(document).ready(function() {
    animateDiv();

});

function makeNewPosition($container) {

    // Get viewport dimensions (remove the dimension of the div)
    $container = ($container || $(window))
    var h = $container.height() - 50;
    var w = $container.width() - 50;

    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);

    return [nh, nw];

}

function animateDiv() {
    var $target = $('.a');
    var newq = makeNewPosition($target.parent());
    var oldq = $target.offset();
    var speed = calcSpeed([oldq.top, oldq.left], newq);

    $('.a').animate({
        top: newq[0],
        left: newq[1]
    }, speed, function() {
        animateDiv();
    });

};

function calcSpeed(prev, next) {

    var x = Math.abs(prev[1] - next[1]);
    var y = Math.abs(prev[0] - next[0]);

    var greatest = x > y ? x : y;

    var speedModifier = 0.1;

    var speed = Math.ceil(greatest / speedModifier);

    return speed;

}​

I've tried playing around with container size and speed but that doesn't help a lot, I also tried adding easing to the jquery myself but I have no knowledge of that mastery and failed. 我尝试过尝试使用容器的大小和速度,但这并没有多大帮助,我也尝试过自己向jQuery添加缓动,但是我不了解这种掌握并且失败了。

Thanks! 谢谢!

I believe you need http://ijin.net/crSpline/demo.html for smooth "light breeze" animations. 我相信您需要http://ijin.net/crSpline/demo.html才能获得流畅的“轻风”动画。

Related answer: how to smooth jquery animations 相关答案: 如何平滑jQuery动画

Here is a full example in fiddle using jQuery.crSpline : 这是一个使用jQuery.crSpline的完整示例:

http://jsfiddle.net/2TUFF/295/ http://jsfiddle.net/2TUFF/295/

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

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