jquery animate:一项一项地改变元素的不透明度

发布于 2024-11-08 06:04:43 字数 1406 浏览 0 评论 0原文

知道如何将每个特定元素的不透明度一一设置为最多 16 个目标/元素吗?

这将改变所有元素的不透明度,

$('.block-item').animate({
        opacity:0
    },500);

请查看此处

但我希望不透明度逐渐改变。当到达第 16 个元素时就会停止。

这是 html,

<div id="parent_container">

<div class="block-item">1</div>
<div class="block-item">2</div>
<div class="block-item">3</div>
<div class="block-item">4</div>
<div class="block-item">5</div>
<div class="block-item">6</div>
<div class="block-item">7</div>
<div class="block-item">8</div>
<div class="block-item">9</div>
<div class="block-item">10</div>
<div class="block-item">11</div>
<div class="block-item">12</div>
<div class="block-item">13</div>
<div class="block-item">14</div>
<div class="block-item">15</div>
<div class="block-item">16</div>
<div class="block-item">17</div>
<div class="block-item">18</div>

</div>

我推出了这个函数,但它会使任何浏览器崩溃,

function opacity_test(index)
{
    $('.block-item').eq( index ).animate({
        opacity:0
    },500);

    setInterval( function() {
        opacity_test(index + 1);
    }, 1000 );
}

谢谢。

Any idea how to animate the opacity of each certain element one by one up to 16 targets/ elements only?

This will change the opacity of the elements all together,

$('.block-item').animate({
        opacity:0
    },500);

Have a look here.

But I want the opacity to change one after another. and this will stop when it reaches the 16th element.

Here is the html,

<div id="parent_container">

<div class="block-item">1</div>
<div class="block-item">2</div>
<div class="block-item">3</div>
<div class="block-item">4</div>
<div class="block-item">5</div>
<div class="block-item">6</div>
<div class="block-item">7</div>
<div class="block-item">8</div>
<div class="block-item">9</div>
<div class="block-item">10</div>
<div class="block-item">11</div>
<div class="block-item">12</div>
<div class="block-item">13</div>
<div class="block-item">14</div>
<div class="block-item">15</div>
<div class="block-item">16</div>
<div class="block-item">17</div>
<div class="block-item">18</div>

</div>

I came out this function but it crashes any browser on it,

function opacity_test(index)
{
    $('.block-item').eq( index ).animate({
        opacity:0
    },500);

    setInterval( function() {
        opacity_test(index + 1);
    }, 1000 );
}

Thanks.

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

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

发布评论

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

评论(3

樱&纷飞 2024-11-15 06:04:43

试试这个:

var delay = 0;
$('.block-item:lt(16)').each(function(){ 
               //^^ do for every instance less than the 16th (starting at 0)
    $(this).delay(delay).animate({
        opacity:0
    },500);
    delay += 500;
});

小提琴: http://jsfiddle.net/maniator/VS8tQ/3/

Try this:

var delay = 0;
$('.block-item:lt(16)').each(function(){ 
               //^^ do for every instance less than the 16th (starting at 0)
    $(this).delay(delay).animate({
        opacity:0
    },500);
    delay += 500;
});

Fiddle: http://jsfiddle.net/maniator/VS8tQ/3/

财迷小姐 2024-11-15 06:04:43

您可以使用 .animate() 的“完成回调”来开始下一个淡入淡出:

function fade(index) {
    $('.block-item').eq(index).animate({
        opacity: 0
    }, function() {
        // on completion, recursively call myself
        // against the next element
        if (index < 15) {
            fade(index + 1);
        }
    })
}

fade(0);

请参阅 http:// jsfiddle.net/alnitak/3DuTG/

You can use the "completion callback" of .animate() to start the next fade:

function fade(index) {
    $('.block-item').eq(index).animate({
        opacity: 0
    }, function() {
        // on completion, recursively call myself
        // against the next element
        if (index < 15) {
            fade(index + 1);
        }
    })
}

fade(0);

See http://jsfiddle.net/alnitak/3DuTG/

忆梦 2024-11-15 06:04:43

使用回调的选项,并在到达第 16 个元素时停止,您可以使用 .index()

var f = function($current) {
    $current.animate({
        opacity: 0
    }, function() {
        if ($current.next(".block-item").index() < 15) {
            f($current.next(".block-item"));
        }
    });
};

f($(".block-item:first"));

jsfiddle 上的示例

An option using the callback, and to stop when it reaches the 16th element you can use .index()

var f = function($current) {
    $current.animate({
        opacity: 0
    }, function() {
        if ($current.next(".block-item").index() < 15) {
            f($current.next(".block-item"));
        }
    });
};

f($(".block-item:first"));

Example on jsfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文