jquery使div反复淡入淡出

发布于 2024-10-03 05:59:27 字数 1730 浏览 7 评论 0原文

你好,我在 for 循环中创建了一系列 div,如下所示:

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({ 'min-width' : width[i], 'min-height' : '100px'  })
                $('#stripe'+i).width(width[i])
                $('#stripe'+i).height('100px')


            };

我想要的是让每个创建的 div 完全淡出,然后再次淡入,并让这个过程永远重复。如果我尝试使用 .animate() 并调用 Complete: 属性的函数,我会得到太多递归和分页符,我该怎么做

hello i have a series of divs created in a for loop like so:

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({ 'min-width' : width[i], 'min-height' : '100px'  })
                $('#stripe'+i).width(width[i])
                $('#stripe'+i).height('100px')


            };

what i want is to have each div created fade out completly then fade back in again and for this process to keep repeating for ever. if i try to use .animate() and call a function for the Complete: property i get too many recursions and the page breaks, how can i do this

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

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

发布评论

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

评论(1

一枫情书 2024-10-10 05:59:27

问题是从彼此内部递归调用 fade*() 例程,您需要将序列设置为可以以非递归方式重复调用的序列。

这是一个非常巧妙的方法:

此处演示:http://jsfiddle.net/ZZGhn/

$('.block').bind('fade-cycle', function() {
    $(this).fadeOut('fast', function() {
        $(this).fadeIn('fast', function() {
            $(this).trigger('fade-cycle');
        });
    });
});

// This next block of code just sets all the triggers off at random times
// The point is to start them using .trigger('fade-cycle');
// All can be started simultaneously with $('.block').trigger('fade-cycle');

$('.block').each(function(index, elem) {
    setTimeout(function() {
        $(elem).trigger('fade-cycle');
    }, index * 250);
});

The problem is recursively calling the fade*() routines from inside each other, you need to setup the sequence as something that can be called repeatedly in a non-recursive fashion.

This is quite a neat method:

Demo here: http://jsfiddle.net/ZZGhn/

$('.block').bind('fade-cycle', function() {
    $(this).fadeOut('fast', function() {
        $(this).fadeIn('fast', function() {
            $(this).trigger('fade-cycle');
        });
    });
});

// This next block of code just sets all the triggers off at random times
// The point is to start them using .trigger('fade-cycle');
// All can be started simultaneously with $('.block').trigger('fade-cycle');

$('.block').each(function(index, elem) {
    setTimeout(function() {
        $(elem).trigger('fade-cycle');
    }, index * 250);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文