2012-09-23 154 views
0

我使用JavaScript和JQuery创建了这个幻灯片。我想在网页上多次使用它。我可以同时出现,但只有一个会循环遍历所有图片,然后重新开始。第二个只循环,然后结束。有人可以帮助我的代码?我希望网页上的所有幻灯片都能持续循环播放。谢谢。自动JQuery幻灯片

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/> 

<title></title> 

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> 

<script type="text/javascript"> 
function slideSwitch() { 
    var $active = $('#slideshow IMG.active'); 

    if ($active.length == 0) $active = $('#slideshow IMG:last'); 
    var $next = $active.next().length ? $active.next() 
     : $('#slideshow IMG:first'); 

    $active.addClass('last-active'); 

    $next.css({opacity: 0.0}) 
     .addClass('active') 
     .animate({opacity: 1.0}, 1000, function() { 
      $active.removeClass('active last-active'); 
     }); 
} 

$(function() { 
    setInterval("slideSwitch()", 5000); 
}); 
</script> 

<style type="text/css"> 

#slideshow { 
    position:relative; 
    height:350px; 
} 

#slideshow IMG { 
    position:absolute; 
    top:0; 
    left:0; 
    z-index:8; 
    opacity:0.0; 
} 

#slideshow IMG.active { 
    z-index:10; 
    opacity:1.0; 
} 

#slideshow IMG.last-active { 
    z-index:9; 
} 

</style> 

</head> 

<div id="slideshow"> 
    <img src="ocean.png" alt="Slideshow Image 1" class="active" /> 
    <img src="ocean2.png" alt="Slideshow Image 2" /> 
    <img src="ocean3.png" alt="Slideshow Image 3" /> 
</div> 

<br /> 

<div id="slideshow"> 
    <img src="flower.png" alt="Slideshow Image 1" class="active" /> 
    <img src="flower2.png" alt="Slideshow Image 2" /> 
    <img src="flower3.png" alt="Slideshow Image 3" /> 
</div> 

</body> 
</html> 
+0

尝试使div ID独一无二。你可以为他们两个使用相同的类。 – Asdfg

+0

你能举个例子说明你的意思吗?我有点理解,但我试着玩弄它,并没有任何工作。我需要更改div id =“slideshow”的名称吗?如果是这样,我还需要在CSS和查询部分更改以上的内容吗? – user1657160

回答

0

你可以生成幻灯片的一个惟一的ID,因为你$('#slideshow IMG.active');东西拿起你有多个实例。尝试是这样的部分:

function slideShow(element_of_slideshow){ 
    var now = new Date().getTime(), uid = "ss" + now; 
    $(element_of_slideshow).attr("id",uid); 

    // then call it like this in the function 
    $("#"+uid+" IMG.active'); 
0

你的问题是,在html标签id属性都应该是在页面上独一无二的。

因此,您的选择器:$('#slideshow IMG:last')将会在第一个中找到id为#slideshow的标记的最后一个img标记。可以考虑使用jquery plugin pattern