简体   繁体   English

是什么用jQuery动画打破了此功能?

[英]What is breaking this function with jQuery animations?

I am attempting to cycle through random shades of orange in a grid-work of divs. 我试图在div的网格中循环浏览橙色的任意阴影。 This is a version of the page WITHOUT the broken function: http://citrix.pewf.co/index_2.html 这是该页面的版本,其功能没有损坏: http : //citrix.pewf.co/index_2.html

    /*Randomized image-box colors, shades of orange, looping*/
        var red,green,blue;
            function colorLoop() {
                $(".img").each(function() {
                    red = parseInt((Math.random() * 50)+190);
                    green = parseInt((Math.random() * 25)+85);
                    blue = parseInt((Math.random() * 10));
                    $(this).animate({"backgroundColor":"rgba("+red+","+green+","+blue+",1)"},1000,function() {
                        colorLoop();
                    });
                });
            }

I based this function off of these two threads: 我基于以下两个线程创建此函数:

jQuery animate loop jQuery动画循环

How to make a jquery infinite animation? 如何制作一个jQuery无限动画?

This function not only does not work, but breaks all the other animation scripts I have in the document. 该功能不仅不起作用,而且破坏了文档中所有其他动画脚本。 (Those scripts all work if I comment out the colorLoop function) (如果我注释掉colorLoop函数,这些脚本都可以工作)

I call the function initially here: 我最初在这里调用该函数:

            $(document).ready(function() {
            $(".img").each(function() {
                /*Randomized image-box load times*/
                var loadTime = ((Math.random() * 1250) + 250);
                $(this).hide().fadeIn(loadTime, function() { colorLoop() });
                });
            });

Any an all help with getting this to work will be greatly appreciated because I have absolutely no idea what I am doing wrong. 我们将不胜感激,因为我绝对不知道自己在做什么错。 =_= = _ =


SOLVED! 解决了! Thanks to YuryTarabanko and SparK ! 感谢YuryTarabankoSparK :) :)


See it in action! 观看实际操作!

Final code: 最终代码:

        <script src="jquery.color-2.1.0.js" type="text/javascript"></script>
        <script>
    /*Randomized image-box colors, shades of orange, looping*/
        var red,green,blue;
            function colorLoop() {
                setTimeout(function() { 
                    $(".img").each(function() {
                        red = parseInt((Math.random() * 75)+180);
                        green = parseInt((Math.random() * 25)+50);
                        blue = parseInt((Math.random() * 15));
                        $(this).animate({"backgroundColor":"rgba("+red+","+green+","+blue+",1)"},1000);
                    });
                    colorLoop();
                },1000);
            }
    </script>

First of all you need a special plugin to animate nonnumeric properties. 首先,您需要一个特殊的插件来对非数字属性进行动画处理。 http://jqueryui.com/animate/ http://jqueryui.com/animate/

Try this code http://jsfiddle.net/tarabyte/h8s4r/ 试试这个代码http://jsfiddle.net/tarabyte/h8s4r/

$(function(){
    function animate(el){ //animate only 1 el
        var color = [parseInt((Math.random() * 50)+190),
                   parseInt((Math.random() * 25)+85),
                    parseInt((Math.random() * 10))];
        el.animate({'backgroundColor': 'rgb('+color.join(',')+')'}, 1000, function(){ animate(el)});
    }

    $('.img').each(function(){
        var el = $(this);
        el.hide().fadeIn(((Math.random() * 1250) + 250), function() {
            animate(el); //call it for each .img once.
        });
    })
})

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

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