


画像を自動的に見せてくれるシンプルなスライドショーがJQueryを使うことで簡単にできてしまいます。
ソースコードはこちらからダウンロードできるようになっています。
使い方は、まずJQueryを読み込みます。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
JQueryを使うときは、以前紹介した「JQueryなどGoogleが提供してくれているサイトのタグを簡単に取得する方法」を使うと簡単ですね。
次に、JSを設定します。
<script type="text/javascript"> function slideSwitch() { var $active = $('#slideshow IMG.active'); if ( $active.length == 0 ) $active = $('#slideshow IMG:last'); // use this to pull the images in the order they appear in the markup 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>
setInterval の 5000 という数字を変えることで、ひとつの画像が表示している時間を調整できます。
また、 .animate の 1000 という数字を変えることで、次の画像に変わるまでの時間を調整できます。これを調整することで滑らかなスライドが作れます。
次が、CSSの設定。
<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>
最後に、表示させたい画像を並べます。
<div id="slideshow"> <img src="1.jpg" class="active" /> <img src="2.jpg" /> <img src="3.jpg" /> </div>
最初に表示させたい画像には class=”active” を設定します。
これだけでシンプルなスライドショーができます。