简体   繁体   English

卡在Jquery动画无限循环中

[英]Stuck with Jquery animate infinite loop

I am trying create bubbles, after few repetitions my browser getting stuck. 我尝试创建气泡,重复几次后我的浏览器卡住了。 here is my code. 这是我的代码。 someone please help.... How do I get it done with out making many requests . 有人请帮忙。。。。。。。。。。。。。。

It looks like my post is mostly code , but I added enough details for this Stackoverflow :) 看来我的帖子主要是代码,但是我为此Stackoverflow添加了足够的细节:)

Thanks! 谢谢!

Jquery: jQuery的:

    $(document).ready(function() {

                           function bubbles()
                       {


                       var i = 0;


                      $(".circle").remove();



                       for(i = 0; i < 3; i ++ )

                       {

                             var CreateDiv =   document.createElement('div');

                      var AppendElem =  document.body.appendChild(CreateDiv);

                      AppendElem.setAttribute('class','circle');

                      CreateDiv.style.position = "absolute";

                      var randomPosTop = Math.floor((Math.random() * 800) + 1);

                      CreateDiv.style.top = randomPosTop +"px";

                       var randomPosLeft = Math.floor((Math.random() * 1200) + 1);

                       CreateDiv.style.left = randomPosLeft +"px";

                        }

                        $( ".circle" ).animate({ opacity :0.0,height:100, width:100 }, 5000,bubbles);


                       }

                       bubbles();


                       });

CSS CSS

        .circle{ width: 20px;
                 height: 20px;
                 background-color: #000;
                 border-radius: 100px; position:absolute;}

jsfiddle jsfiddle

     https://jsfiddle.net/krishnakamal549/u4krxq8o/

The issue is that the callback in the animation gets called for each element it finds. 问题是动画中的回调会针对找到的每个元素进行调用。 So.. the first time it gets called 3 times, the second time 9, the third time 18, it gets tripled each time, eventually you're running hundreds of instances of "bubbles". 因此..第一次调用3次,第二次调用9,第三次调用18,则每次将其调用三倍,最终您将运行数百个“气泡”实例。

You want to use a promise to do the callback like so. 您想使用一个promise来进行回调。

   $(".circle").animate({
     opacity: 0.0,
     height: 100,
     width: 100
   }, 5000).promise().done(bubbles);

This way, the callback only fires once for the entire animation, not per element. 这样,整个动画只会触发一次回调,而不是每个元素触发一次。

FIDDLE 小提琴

$(".circle").animate({
   opacity: 0.0,
   height: 100,
   width: 100
}, 5000);

$("body").animate({ opacity: 1 }, 5000, bubbles);

Try this, this also works. 试试这个,这也可以。 But i suggest to use approach specified by Smeegs. 但我建议使用办法由Smeegs规定。

This method adds an extra process to the body for an animation while approach by Smeegs directly deals where the animation is applied and no need for an extra animation process on body 这种方法增加了额外的过程中对body的动画,同时通过Smeegs方法直接处理其中施加动画,无需在额外的动画制作过程body

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

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