依次淡入LI元素,然后进行重定向。

3

我需要一个列表能够淡入显示,然后等待几秒钟后重定向到一个新页面。

以下是淡入效果的代码,可以逐个按顺序显示每个列表项。请问如何在最后添加延时,并进行重定向操作?

    function fadeLi(elem) {
        elem.fadeIn(800, function() {
            fadeLi($(this).next().delay(900));
        });

    }

    fadeLi( $('#welcome li:first'));

感谢您的帮助。
1个回答

4
不错的递归。这允许您检查jQuery对象的长度,以便检测是否已经枚举完列表项。
function fadeLi(elem) {
    // elem is a jQuery object which support length property
    if (elem.length === 0){
        // we are out of elements so we can set the location change
        setTimeout(function(){
            // set the window location to whatever url you like
            window.location = 'https://www.where.ever/you/are/taking/the/user';
        // adjust the timeout in milliseconds
        }, 900);
        // in this case you no longer want to recursively call so return
        return;
    }
    elem.fadeIn(800, function() {
        // note: I don't think delay call has any impact here.
        fadeLi($(this).next().delay(900));
    });

}

fadeLi( $('#welcome li:first'));

太棒了。我知道我需要做一个if语句,只是没有充分说服自己。谢谢你的帮助! - Ryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接