Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery在同一函数中将两个div交叉淡入_Javascript_Jquery_Html - Fatal编程技术网

Javascript jquery在同一函数中将两个div交叉淡入

Javascript jquery在同一函数中将两个div交叉淡入,javascript,jquery,html,Javascript,Jquery,Html,我在jquery中有一个旋转/淡入淡出的div,它工作得很好,但我现在希望它对同一页上的第二个div执行相同的操作,但无法使其工作 我目前的代码是: $(window).load(function() { setInterval('cycleImages()', 5000); }) function cycleImages() { var $active = $('#main-photo-area .active'); var $next = ($('#main-photo-area .act

我在jquery中有一个旋转/淡入淡出的div,它工作得很好,但我现在希望它对同一页上的第二个div执行相同的操作,但无法使其工作

我目前的代码是:

$(window).load(function() {
setInterval('cycleImages()', 5000);
})

function cycleImages() {
var $active = $('#main-photo-area .active');
var $next = ($('#main-photo-area .active').next().length > 0) ? $('#main-photo-area .active').next() : $('#main-photo-area div:first');
$next.css('z-index', 2);
$active.fadeOut(1500,function() {
  $active.css('z-index', 1).show().removeClass('active');
  $next.css('z-index', 3).addClass('active');
});
}
它可以很好地工作,并通过更改容器中div的z索引来交叉淡入div id=“main photo area”精细

现在,我还希望同样的代码使另一组div褪色,例如:id=“second photo area”

有人知道我如何修改这个cycleImages()脚本,使两个div集同时淡入淡出,而不需要添加另一个例程来实现这一点吗

我知道还有很多其他的方法可以做到这一点,但我希望能够高效地使用已有的代码,如果可能的话,不要再添加其他例程


提前感谢。

您可以通过以下方式更改功能:

function cycleImages(val) {
    var $active = $('#' + val + ' .active');
    var $next = ($('#' + val + ' .active').next().length > 0) ? $('#' + val + ' .active').next() : $('#' + val + ' div:first');
    $next.css('z-index', 2);
    $active.fadeOut(1500, function () {
        $active.css('z-index', 1).show().removeClass('active');
        $next.css('z-index', 3).addClass('active');
    });
}

$(window).load(function() {
    setInterval('cycleImages("main-photo-area")', 5000);
    setInterval('cycleImages("second-photo-area")', 5000);
})

我假设两个分区的结构相同。

您可以通过以下方式更改函数:

function cycleImages(val) {
    var $active = $('#' + val + ' .active');
    var $next = ($('#' + val + ' .active').next().length > 0) ? $('#' + val + ' .active').next() : $('#' + val + ' div:first');
    $next.css('z-index', 2);
    $active.fadeOut(1500, function () {
        $active.css('z-index', 1).show().removeClass('active');
        $next.css('z-index', 3).addClass('active');
    });
}

$(window).load(function() {
    setInterval('cycleImages("main-photo-area")', 5000);
    setInterval('cycleImages("second-photo-area")', 5000);
})

我假设两个分区的结构相同。

您可以通过以下方式更改函数:

function cycleImages(val) {
    var $active = $('#' + val + ' .active');
    var $next = ($('#' + val + ' .active').next().length > 0) ? $('#' + val + ' .active').next() : $('#' + val + ' div:first');
    $next.css('z-index', 2);
    $active.fadeOut(1500, function () {
        $active.css('z-index', 1).show().removeClass('active');
        $next.css('z-index', 3).addClass('active');
    });
}

$(window).load(function() {
    setInterval('cycleImages("main-photo-area")', 5000);
    setInterval('cycleImages("second-photo-area")', 5000);
})

我假设两个分区的结构相同。

您可以通过以下方式更改函数:

function cycleImages(val) {
    var $active = $('#' + val + ' .active');
    var $next = ($('#' + val + ' .active').next().length > 0) ? $('#' + val + ' .active').next() : $('#' + val + ' div:first');
    $next.css('z-index', 2);
    $active.fadeOut(1500, function () {
        $active.css('z-index', 1).show().removeClass('active');
        $next.css('z-index', 3).addClass('active');
    });
}

$(window).load(function() {
    setInterval('cycleImages("main-photo-area")', 5000);
    setInterval('cycleImages("second-photo-area")', 5000);
})

我假设两个分区的结构相同。

一种可能的方法是为所有照片区域指定一个公共类名,比如说
照片区域
,然后使用
。each()
迭代每个实例:

$(window).load(function() {
    setInterval(cycleImages, 5000);
})

var cycleImages = function() {
    $('.photo-area').each(function() {
        var $active = $(this).find('.active'),
            $next = ($active.next().length > 0) ? $active.next() : $(this).find('div:first');

        $next.css('z-index', 2);
        $active.fadeOut(1500,function() {
            $active.css('z-index', 1).show().removeClass('active');
            $next.css('z-index', 3).addClass('active');
        });
    });
}
我对您的代码做了一些更改:

  • 将函数声明为变量,并直接在
    setInterval()中通过其变量名调用它
  • 利用缓存的jQuery对象,并在定义
    $next
    时参考
    $active
    ,以节省在DOM节点中查找感兴趣元素的工作量

一种可能的方法是为所有照片区域指定一个公共类名,比如说
photo area
,然后使用
。each()
迭代每个实例:

$(window).load(function() {
    setInterval(cycleImages, 5000);
})

var cycleImages = function() {
    $('.photo-area').each(function() {
        var $active = $(this).find('.active'),
            $next = ($active.next().length > 0) ? $active.next() : $(this).find('div:first');

        $next.css('z-index', 2);
        $active.fadeOut(1500,function() {
            $active.css('z-index', 1).show().removeClass('active');
            $next.css('z-index', 3).addClass('active');
        });
    });
}
我对您的代码做了一些更改:

  • 将函数声明为变量,并直接在
    setInterval()中通过其变量名调用它
  • 利用缓存的jQuery对象,并在定义
    $next
    时参考
    $active
    ,以节省在DOM节点中查找感兴趣元素的工作量

一种可能的方法是为所有照片区域指定一个公共类名,比如说
photo area
,然后使用
。each()
迭代每个实例:

$(window).load(function() {
    setInterval(cycleImages, 5000);
})

var cycleImages = function() {
    $('.photo-area').each(function() {
        var $active = $(this).find('.active'),
            $next = ($active.next().length > 0) ? $active.next() : $(this).find('div:first');

        $next.css('z-index', 2);
        $active.fadeOut(1500,function() {
            $active.css('z-index', 1).show().removeClass('active');
            $next.css('z-index', 3).addClass('active');
        });
    });
}
我对您的代码做了一些更改:

  • 将函数声明为变量,并直接在
    setInterval()中通过其变量名调用它
  • 利用缓存的jQuery对象,并在定义
    $next
    时参考
    $active
    ,以节省在DOM节点中查找感兴趣元素的工作量

一种可能的方法是为所有照片区域指定一个公共类名,比如说
photo area
,然后使用
。each()
迭代每个实例:

$(window).load(function() {
    setInterval(cycleImages, 5000);
})

var cycleImages = function() {
    $('.photo-area').each(function() {
        var $active = $(this).find('.active'),
            $next = ($active.next().length > 0) ? $active.next() : $(this).find('div:first');

        $next.css('z-index', 2);
        $active.fadeOut(1500,function() {
            $active.css('z-index', 1).show().removeClass('active');
            $next.css('z-index', 3).addClass('active');
        });
    });
}
我对您的代码做了一些更改:

  • 将函数声明为变量,并直接在
    setInterval()中通过其变量名调用它
  • 利用缓存的jQuery对象,并在定义
    $next
    时参考
    $active
    ,以节省在DOM节点中查找感兴趣元素的工作量

这是一个简洁的解决方案。看来效果不错。谢谢你的意见。这是一个简洁的解决方案。看来效果不错。谢谢你的意见。这是一个简洁的解决方案。看来效果不错。谢谢你的意见。这是一个简洁的解决方案。看来效果不错。谢谢你的意见。