动画元素是随机的负数或正数

时间:2016-04-13 20:06:50

标签: javascript jquery animation random

我尝试使用math.random通过math.random以随机数量为这些单词的字符设置动画,但它不起作用。 这是我的数学问题DEMO

 $('.reverse_button').on('click', function(){

    var chars = $('.reverse_body').blast({
        delimiter: 'character'
    });
             var moveup=parseInt((Math.random()* document.body.clientHeight) - document.body.clientHeight);
         var moveleft=parseInt((Math.random()* document.body.clientWidth) - document.body.clientWidth);
    chars.each(function(idx,obj) {


         $(obj).animate({
            left: moveleft,
            top: moveup
            });
         });
        });

这就是我试图先获得工作的原因。这里有一个错误。

 $('.reverse_button').on('click', function(){

    var chars = $('.reverse_body').blast({
        delimiter: 'character'
    });

    chars.each(function() {

        $(this).animate({
           left: 100,
           top: 100
           }, 500);
        });
       });

1 个答案:

答案 0 :(得分:0)

@KevinB

我需要给受影响的元素一个相对的位置。这是工作代码和演示。

 $('.reverse_button').on('click', function(){

    var chars = $('.reverse_body').blast({
        delimiter: 'character'
    });

    chars.each(function() {
                var moveup=parseInt((Math.random()* document.body.clientHeight) - document.body.clientHeight);
                 var moveleft=parseInt((Math.random()* document.body.clientWidth) - document.body.clientWidth);
        $(this).css({
            position: 'relative',
            left: 0
        })
        .animate({
           left: moveleft,
           top: moveup
       }, 1500);
        });
       });

DEMO

然而,它仍然没有动画负数量,任何人都知道创建随机负数的公式?

相关问题