简体   繁体   English

jQuery的无限无限旋转动画

[英]Random infinit rotation animation with jQuery

I've been playing around with JS+CSS animations for the first time. 我是第一次玩JS + CSS动画。 I am trying to rotate a div (or a bunch of them independently) with these random factors: 我正在尝试使用以下随机因素旋转div(或一堆独立地):

  • Random direction 随机方向
  • Random degrees 随机度
  • With random pauses 随机停顿

I managed to gather this script , which worked well. 我设法收集了这个脚本,效果很好。 It rotates a div with id="square" infinity, into a [random] direction, by [random] degrees 它将带有id =“ square” infinity的div旋转到[random]方向,旋转[random]度

    function redo() {  
    $.fn.animateRotate = function(angle, duration, easing, complete) {
            return this.each(function() {
            var $elem = $(this);

            $({deg: 0}).animate({deg: angle}, {
                duration: duration,
                easing: easing,
                step: function(now) {
                    $elem.css({
                        transform: 'rotate(' + now + 'deg)'
                    });
                },
                complete: complete //|| $.noop
            });
        });
    };
    plusOrMinus = Math.random() < 0.5 ? -1 : 1;
    randAngle = Math.floor(Math.random()*70+30) * plusOrMinus;
    randDelay = Math.floor(Math.random()*3000+2000);
    $('#square').animateRotate(randAngle);
    timer = setTimeout(function() { redo(); },randDelay);
}

redo();

http://jsfiddle.net/NWLVY/1 http://jsfiddle.net/NWLVY/1

Now I've been having a few difficulties: 现在我遇到了一些困难:

  1. I would like to pass more than one element into this loop, and I want them to behave independently. 我想将多个元素传递到此循环中,并且希望它们独立运行。 I wouldn't mind if they all had the same class, or if I just needed to write multiple lines to execute each one. 我不介意它们是否都具有相同的类,或者我是否只需要编写多行来执行每一行。 There won't be too many. 不会太多。
  2. This rotation doesn't continue from the last angle. 此旋转不会从最后一个角度继续。 It keeps jumping back. 它不断跳回去。 I can't figure out how to fix this. 我不知道如何解决此问题。
  3. I can't seem to pass any easing options like swing or linear , nor any other options such as duration and complete 我似乎无法通过任何诸如swinglinear宽松选项,也无法传递诸如durationcomplete任何其他选项。

Solved. 解决了。

I used a slightly different script but it now works. 我使用了略有不同的脚本,但现在可以使用了。 I added Transition and Animation lines to the CSS of the rotating element, 我向旋转元素的CSS添加了Transition和Animation行,

transition:2s;
animation:ease-in-out;

Those seemed to have fixed the issue for the jagged spinning, and let me add some easing. 这些似乎已经解决了锯齿状旋转的问题,并让我添加一些缓解措施。 Then I used a wrapping function to pass different elements through the function. 然后,我使用了包装函数将不同的元素传递给该函数。

<Script>
 function rotate (elementID) {

    var $rota = $(elementID),
        degree = 0,
        timer;

    function spin() {    
        $rota.css({ transform: 'rotate(' + degree + 'deg)'});   // rotate element
        plusOrMinus = Math.random() < 0.5 ? -1 : 1;       // random spin direction
        randAngle = Math.floor(Math.random()*70+50) * plusOrMinus; // random degrees
        randDelay = Math.floor(Math.random()*1000+2000);  //random delay
        timer = setTimeout(function() {  // set delay
            degree += randAngle;  // add random degree to current variable
            spin(); // loop it
        },randDelay);
    }

    spin();    // start first spin for element

};

rotate ('#square'); // run it
rotate ('#square2'); // run it again

</script>

Here it is at work http://jsfiddle.net/NWLVY/2/ 这是在工作中http://jsfiddle.net/NWLVY/2/

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

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