Javascript jQueryReplacing以时间间隔显示图像

Javascript jQueryReplacing以时间间隔显示图像,javascript,jquery,image,Javascript,Jquery,Image,我想在我的标志上加上圣诞灯。我本来打算在flash中实现这一点,但我正试图远离flash,所以我决定在jQuery中尝试 一个快速的谷歌搜索返回。这让我走上了正轨。问题是,我不希望图像淡入淡出,所以我更换了 $active.fadeOut(function() $next.fadeIn().addClass('active'); 与 $active.show(函数()$next.show().addClass('active') 问题是,它只会在图像中旋转一次,然后停止。我尝试使用hide,但

我想在我的标志上加上圣诞灯。我本来打算在flash中实现这一点,但我正试图远离flash,所以我决定在jQuery中尝试

一个快速的谷歌搜索返回。这让我走上了正轨。问题是,我不希望图像淡入淡出,所以我更换了

$active.fadeOut(function() $next.fadeIn().addClass('active');
与 $active.show(函数()$next.show().addClass('active')

问题是,它只会在图像中旋转一次,然后停止。我尝试使用
hide
,但它会产生奇怪的缩小效果

简而言之,我有4张图片,我正试图使用以下代码循环浏览它们:

    function swapImages(){
  var $active = $('#myGallery .active');
  var $next = ($('#myGallery .active').next().length > 0) ? $('#myGallery .active').next() : $('#myGallery img:first');
  $active.show(function(){
  $active.removeClass('active');
  $next.show().addClass('active');
  });
}
  $(document).ready(function(){
      setInterval('swapImages()', 1000);
})
Html:


或者不工作

试试这个

function swapImages() {
    var $current = $('#myGallery img:visible');
    var $next = $current.next();
    if($next.length === 0) {
        $next = $('#myGallery img:first');
    }
    $current.hide();
    $next.show();
}

$(document).ready(function() {
    // Run our swapImages() function every 0.5 secs
    setInterval(swapImages, 500);
});

奖金()

试试这个

function swapImages() {
    var $current = $('#myGallery img:visible');
    var $next = $current.next();
    if($next.length === 0) {
        $next = $('#myGallery img:first');
    }
    $current.hide();
    $next.show();
}

$(document).ready(function() {
    // Run our swapImages() function every 0.5 secs
    setInterval(swapImages, 500);
});

奖金()

啊,已经回答了

试试这个

您使用了show()函数,该函数将display:block样式添加到元素中。因此,在运行一次之后,所有图像同时显示,最后一个图像位于其他图像之上,因此显示了一个图像。

啊,已经回答了

试试这个


您使用了show()函数,该函数将display:block样式添加到元素中。因此,在一次运行后,所有图像同时显示,最后一个图像位于其他图像之上,因此显示了一个图像。

您是否尝试过使用show()/hide()而不是fadeIn()/fadeOut()来显示图像?是的,这是我尝试的第一件事。show很好,但是hide使图像缩小了。实际上看起来类的应用是正确的,只是没有正确地隐藏图像。您尝试过show()/hide()而不是fadeIn()/fadeOut()吗是的,这是我尝试的第一件事。show很好,但是hide使图像缩小了。实际上,看起来类的应用是正确的,只是没有正确地隐藏图像。很酷,我通过将
$active.removeClass('active');
更改为“$active.hide().removeClass('active')使其正常工作“但是我认为你的代码更干净。只有一个问题。能对Jumpynes做些什么吗?你说的Jumpynes是什么意思?我的意思是显然FF是个混蛋。Chrome很高兴。无视并感谢你的帮助!我增加了奖金,你看到了吗?:)@enre,是的,谢谢,这很酷。Javascript是一种令人惊叹的语言,我仍在掌握它。很酷,我通过更改
$active.removeClass('active')到“$active.hide().removeClass('active');”但我认为你的代码要干净得多。只有一个问题。对这种神经质能做些什么吗?你说的神经质是什么意思?我的意思是,显然FF是个混蛋。我很高兴。不管怎样,谢谢你的帮助!我加了一笔奖金,你看到了吗?:)@恩,是的,谢谢,那很酷。Javascript是一种神奇的语言,我仍在掌握它。
function swapImages() {
    var random = Math.floor(Math.random()*3),
        $current = $('#myGallery img:visible');
    $current.hide();
    if($current.index() == random) {
        random = ++random % 4;
    }
    $('#myGallery img').eq(random).show();
}

$(document).ready(function() {
    // Run our swapImages() function every 0.5 secs
    setInterval(swapImages, 500);
});