0

我使用 javascript 和 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> 
4

2 回答 2

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');
于 2012-09-23T02:58:03.647 回答
0

您的问题是idhtml 标签中的属性应该在页面上是唯一的。

因此,您的选择器:将在 ID 为 #slideshow 的标签第一次$('#slideshow IMG:last')出现时找到最后一个 img 标签。

也许考虑使用jquery 插件模式

于 2012-09-23T02:58:29.220 回答