DIV CSS 佈局教程網

jQuery 圖片輪播焦點圖:isc 插件的設定與樣式
編輯:JQuery特效代碼     

首頁的焦點圖輪播幾乎是必備元素。與其自己刻,用現成的插件更省事——isc(image scale carousel)只要傳入一組圖片路徑就能跑起來,還支援自動播放與左右切換。

一、引入與初始化

<script src="https://libs.baidu.com/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
 <script src="js/jquery-image-scale-carousel.js" type="text/javascript" charset="utf-8"></script>
 <script>
 var carousel_images = [
  "images/01.jpg",
  "images/02.jpg",
  "images/03.jpg",
  "images/04.jpg",
  "images/05.jpg",
  "images/06.jpg",
  "images/07.jpg"
 ];
 // Example without autoplay
 $(window).load(function() {
  $("#photo_container").isc({
    imgArray: carousel_images
  });
 });
 // Example with autoplay
 /*
 $(window).load(function() {
  $("#photo_container").isc({
    imgArray: carousel_images,
    autoplay: true,
    autoplayTimer: 5000 // 5 seconds.
  });
 });
 */
 </script>

初始化放在 $(window).load 而非 $(document).ready,是因為輪播需要圖片的實際尺寸來計算版面;用 ready 時圖片可能尚未載入完成,會導致高度算錯、版面塌陷。

二、必要樣式

body {
 font-family:"HelveticaNeue-Light","Helvetica Neue Light","Helvetica Neue",Helvetica,Arial,sans-serif;
 color: #FFF;
 font-size: 12px;
 background: #000;
}
h1 { font-size: 52px; text-align: center; }
h1,h2,h3,h4 { font-weight: 100; }
#photo_container {
 width: 960px;
 height: 400px;
 margin: auto;
 background-color: #000;
}
p { text-align: center; }
/*定義文字樣式*/
A {FONT-SIZE: 12px; COLOR: #000;}
A:link {COLOR: #2d8931; TEXT-DECORATION: none;}
A:visited {COLOR: #333; TEXT-DECORATION: none;}
A:hover {COLOR: #333; TEXT-DECORATION:underline;}
A:active {COLOR: #333; TEXT-DECORATION: none;}
#swipe_nav_prev,#swipe_nav_next {
 position: absolute;
 top: 0;
 left: 0;
 z-index: 2000;
 background-color: #ccc;
 cursor: pointer;
 text-align: center;
 display: none;
}
#swipe_nav_prev {
 background: #333 url('prev.png') no-repeat center center;
}
#swipe_nav_next {
 background: #333 url('next.png') no-repeat center center;
}
.internal_swipe_container { position: relative; }
.trans {
 filter:alpha(opacity=75);
 -moz-opacity:0.75;
 -khtml-opacity: 0.75;
 opacity: 0.75;
}
.jq_swipe_image {
 background: url('loader.gif') no-repeat center center;
}
#count_container {
 padding: 0;
 margin: 0;
 position: absolute;
 top: 0;
 left: 0;
 background-color: pink;
 height: 6px;
 list-style: none;
}
.counter {
 float: left;
 height: 6px;
 background-color: #FFF;
 z-index: 200;
 padding: 0;
 margin: 0;
}
.counter:hover { cursor: pointer; background-color: #ff00fc !important; }
.current { background-color: #ff00fc !important; }

三、幾個 class 的用途

#photo_container 是容器,寬高必須明確設定,輪播才能計算版面。#swipe_nav_prev / next 是左右箭頭,預設 display:none,滑入時才由腳本顯示。.trans 提供半透明效果,四行 opacity 寫法是為了兼顧 IE 與各家前綴。.jq_swipe_image 用 loading 圖當背景,圖片載入時不會出現空白。#count_container 與 .counter 是底部那條進度條,寬度會依目前播放到第幾張而伸縮,.current 則標示當前位置。

實務上要注意兩點:箭頭圖片 prev.png、next.png 與 loader.gif 需自行準備並放在 CSS 的相對路徑下;另外輪播容器若在外層設了 overflow:hidden,箭頭的絕對定位可能會被裁切,需確認定位父元素為 .internal_swipe_container 而非更外層的容器。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved