jquery以不同的时间淡入多个div

发布于 2024-10-03 13:31:59 字数 2304 浏览 4 评论 0原文

正在将 flash 动画转换为 jquery#

我基本上有一堆不同颜色的 div,我希望这些 div 淡入和淡出,每个 div 根据斐波那契序列有不同的计时,

我在分配淡入淡出时遇到问题函数到 div,我希望淡入淡出函数淡出 div,然后在完成后再次淡入,并继续对每个 div 重复该过程。

这是我当前的代码,但它使 Firefox 崩溃,大概是因为我有太多 setintervals ,有人能给我指出正确的方向吗?

var myDiv ='#bannerHolder'
    var fib_str = '1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 5, 8, 13'
    var widths_str = '33px, 31px, 35px, 9px, 16px, 50px, 33px, 24px, 40px, 20px, 63px, 30px, 10px, 29px, 11px, 12px, 51px, 31px, 35px, 11px, 14px, 50px, 30px, 25px, 38px, 20px, 35px'
    var pos_str = '0px, 44px, 105px, 144px, 153px, 203px, 236px, 269px, 280px, 354px, 374px, 437px, 447px, 457px, 506px, 517px, 529px, 604px, 646px, 687px, 698px, 712px, 762px, 787px, 823px, 895px, 915px'
    var color_str = '#D5E1D7, #18D4C9,#BF51C3,#1E82C9, #EDEDED, #E1C981, #C9A94F, #BDBCAA, #5DB522, #EB2F34, #823133, #004BD8, #A6A0A0, #DS9F36, #FFDB00, #69944F, #18D4C9, #BF51C3, #1E82C9, #6B949A, #FFDB00, #819E64, #BDBCAA, #54BA43, #EB2F34, #823133'
    var width = widths_str.split(",");
    var pos = pos_str.split(",");
    var color = color_str.split(",");
    var fib = fib_str.split(",");
    console.log(width);
    console.log(pos);
    console.log(color);
    var counter = width.length
    var stopper = false;
          for(i=0;i<counter;i++){
        var html = '<div id ="stripe'+i+'"/>'   
        $(myDiv).append(html)
        $('#stripe'+i).css({  'top': 0,  'left': pos[i],'position': 'absolute', 'float': 'left', 'background-color' : color[i]})
        $('#stripe'+i).attr({'height': 100, 'width': width[i], 'min-width' : width[i], 'min-height' : '100px'  })
        $('#stripe'+i).width(width[i])
        $('#stripe'+i).height('100px')
        var myfibtime = (fib[i] * 200);
        setInterval(stripeFadeOut(i), myfibtime );
                setInterval(stripeFadeIn(i), myfibtime );
    };
    function stripeFadeOut(id){
        $('#stripe'+id).fadeOut('slow');
        var myfibtime = (fib[id] * 200);
    }
    function stripeFadeIn(id){
        $('#stripe'+id).fadeIn('slow');
        var myfibtime = (fib[id] * 200);
    }

})

如果我使用 setInterval('stripeFadeIn(' + id + ')', myfibtime+'; 我得到 stripeFadeIn 未定义,如果我尝试按照 mathew 的建议使用 .animate 我得到太多递归

am converting a flash animation to jquery#

i basically have a load of divs which are different colours, i want to the divs to fade in and out, each div has a different timing based on a fibonachi sequence,

i am having problems assigning a fade function to the div, i want the fade function to fade out the div then when is completed fade it in again, and keep repeating the process for each div.

here is my current code but it crashes firefox presumably to do with me having so many setintervals can anyone point me in the right direction please?

var myDiv ='#bannerHolder'
    var fib_str = '1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 5, 8, 13, 21, 1, 2, 3, 5, 8, 13'
    var widths_str = '33px, 31px, 35px, 9px, 16px, 50px, 33px, 24px, 40px, 20px, 63px, 30px, 10px, 29px, 11px, 12px, 51px, 31px, 35px, 11px, 14px, 50px, 30px, 25px, 38px, 20px, 35px'
    var pos_str = '0px, 44px, 105px, 144px, 153px, 203px, 236px, 269px, 280px, 354px, 374px, 437px, 447px, 457px, 506px, 517px, 529px, 604px, 646px, 687px, 698px, 712px, 762px, 787px, 823px, 895px, 915px'
    var color_str = '#D5E1D7, #18D4C9,#BF51C3,#1E82C9, #EDEDED, #E1C981, #C9A94F, #BDBCAA, #5DB522, #EB2F34, #823133, #004BD8, #A6A0A0, #DS9F36, #FFDB00, #69944F, #18D4C9, #BF51C3, #1E82C9, #6B949A, #FFDB00, #819E64, #BDBCAA, #54BA43, #EB2F34, #823133'
    var width = widths_str.split(",");
    var pos = pos_str.split(",");
    var color = color_str.split(",");
    var fib = fib_str.split(",");
    console.log(width);
    console.log(pos);
    console.log(color);
    var counter = width.length
    var stopper = false;
          for(i=0;i<counter;i++){
        var html = '<div id ="stripe'+i+'"/>'   
        $(myDiv).append(html)
        $('#stripe'+i).css({  'top': 0,  'left': pos[i],'position': 'absolute', 'float': 'left', 'background-color' : color[i]})
        $('#stripe'+i).attr({'height': 100, 'width': width[i], 'min-width' : width[i], 'min-height' : '100px'  })
        $('#stripe'+i).width(width[i])
        $('#stripe'+i).height('100px')
        var myfibtime = (fib[i] * 200);
        setInterval(stripeFadeOut(i), myfibtime );
                setInterval(stripeFadeIn(i), myfibtime );
    };
    function stripeFadeOut(id){
        $('#stripe'+id).fadeOut('slow');
        var myfibtime = (fib[id] * 200);
    }
    function stripeFadeIn(id){
        $('#stripe'+id).fadeIn('slow');
        var myfibtime = (fib[id] * 200);
    }

})

if i use setInterval('stripeFadeIn(' + id + ')', myfibtime+'; i get stripeFadeIn is undefined, if i try to use .animate as suguested by mathew i get too much recursion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所谓喜欢 2024-10-10 13:31:59

我建议研究 jQuery 动画 http://api.jquery.com/animate/ 并设置进行相反淡入淡出的回调。

他们的示例使用动画的不透明度,因此也许这会给您带来所需的效果。

I'd recommend looking into jQuery animations http://api.jquery.com/animate/ and setup a callback to do the opposite fade.

Their example uses opacity for the animation, so perhaps that will give you the desired effect.

爺獨霸怡葒院 2024-10-10 13:31:59

当您在循环以及 setFadeIn 函数中创建间隔对象时,会导致以指数方式创建间隔对象。最终导致浏览器崩溃。

相反,您可以在循环本身中做一件事,创建两个间隔,即一个用于淡出,一个用于淡入。代替“慢”,使用您选择的几毫秒。

然后你的 fadeOut 应该在每个 myfibtime + animatingMilliseconds = fadeOutInterval 上调用。并且您的 fadeIn 应该在每个 fadeOutInterval + animatingMilliseconds 上调用。

我希望这会对您有所帮助

我的建议完全参考您的旧代码。所以这样做

for(i=0;i<counter;i++){
        var html = '<div id ="stripe'+i+'"/>'   
        $(myDiv).append(html)
        $('#stripe'+i).css({  'top': 0,  'left': pos[i],'position': 'absolute', 'float': 'left', 'background-color' : color[i]})
        $('#stripe'+i).attr({'height': 100, 'width': width[i], 'min-width' : width[i], 'min-height' : '100px'  })
        $('#stripe'+i).width(width[i])
        $('#stripe'+i).height('100px')
        var myfibtime = (fib[i] * 200);
        var animTime = 1500;
        var fadeOutInterval = myfibtime + animTime;
        var fadeInInterval = fadeOutInterval  + animTime;
        setInterval(stripeFadeOut(i), fadeOutInterval );
        setInterval(stripeFadeIn(i), fadeInInterval );
    };
    function stripeFadeOut(id){
        $('#stripe'+id).fadeOut(1500);            
    }
    function stripeFadeIn(id){
        $('#stripe'+id).fadeIn(1500);
    }

As you are creating interval object in your loop as well as in your setFadeIn function that causes creating interval objects exponentially. Ultimately ends up by crashing browser.

Instead you can do one thing in the loop itself, create two intervals i.e one for fadeOut and one for fadeIn. Insted of 'slow', use some milliseconds of your choice.

Then your fadeOut should be called on every myfibtime + animatingMilliseconds = fadeOutInterval. And your fadeIn should be called on every fadeOutInterval + animatingMilliseconds.

I hope this will help you

My suggestion completely refers to your old code. So do like this

for(i=0;i<counter;i++){
        var html = '<div id ="stripe'+i+'"/>'   
        $(myDiv).append(html)
        $('#stripe'+i).css({  'top': 0,  'left': pos[i],'position': 'absolute', 'float': 'left', 'background-color' : color[i]})
        $('#stripe'+i).attr({'height': 100, 'width': width[i], 'min-width' : width[i], 'min-height' : '100px'  })
        $('#stripe'+i).width(width[i])
        $('#stripe'+i).height('100px')
        var myfibtime = (fib[i] * 200);
        var animTime = 1500;
        var fadeOutInterval = myfibtime + animTime;
        var fadeInInterval = fadeOutInterval  + animTime;
        setInterval(stripeFadeOut(i), fadeOutInterval );
        setInterval(stripeFadeIn(i), fadeInInterval );
    };
    function stripeFadeOut(id){
        $('#stripe'+id).fadeOut(1500);            
    }
    function stripeFadeIn(id){
        $('#stripe'+id).fadeIn(1500);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文