0

このスライドショーは、javascriptとJQueryを使用して作成しました。Webページで複数回使用したい。両方を表示することはできますが、1つだけがすべての画像を循環して、最初からやり直します。2つ目は、循環して終了するだけです。誰かがコードを手伝ってくれますか?Webページ上のすべてのスライドショーを継続的に循環させたい。ありがとう。

<!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

あなたの問題はid、htmlタグの属性がページ上で一意であると想定されていることです。

したがって、セレクター:IDが#slideshowのタグが最初$('#slideshow IMG:last')に出現したときに最後のimgタグが検索されます。

たぶん、jqueryプラグインパターンの使用を検討してください。

于 2012-09-23T02:58:29.220 に答える