DIV CSS 佈局教程網

用 JavaScript 打造 Web 焦點圖輪播(Slider)完整範例
編輯:關於JavaScript     

焦點圖是首頁最常見的元件。這份範例不用任何外掛,只靠原生 JavaScript 加上少許 jQuery,實作出自動播放、滑鼠移入暫停、點頁碼跳轉的完整效果。

HTML 與樣式

<!DOCTYPE html>
<html>
<head>
<title>焦點圖範例</title>
<style>
*{padding:0;margin:0}
ul{list-style:none}
.slider-focus{width:670px;height:240px;overflow:hidden;position:relative;margin:100px auto}
.slider-focus .slider{width:3500px; position:absolute; left:0px; top:0px; height:240px}
.slider-focus .slider li{float:left;}
.slider-focus .btns{position: absolute; right: 0px; bottom: 5px}
.slider-focus .btns li{width:18px;height:18px; float:left; background:#fff; margin-right:5px; cursor:pointer}
.slider-focus .btns li.cur{background:#f60}
</style>
</head>
<body>
<div class="slider-focus">
  <ul class="slider">
    <li><img src="https://img14.360buyimg.com/da/g13/M03/0D/0D/rBEhVFJCwIQIAAAAAADs5q4P0g8AADgxQMhvMIAAOz-234.jpg"></li>
    <li><img src="https://img11.360buyimg.com/da/g13/M05/0D/0A/rBEhUlJCfiYIAAAAAADqWhDpUVsAADfqgDwmw4AAOpy960.jpg"></li>
    <li><img src="https://img11.360buyimg.com/da/g14/M07/11/15/rBEhVVI_5zMIAAAAAADDgfSaKlQAADc8AFf20cAAMOZ670.jpg"></li>
    <li><img src="https://img11.360buyimg.com/da/g13/M03/0D/13/rBEhVFJD_HcIAAAAAADsfenKOe0AADjVwPmryQAAOyV341.jpg"></li>
    <li><img src="https://img14.360buyimg.com/da/g15/M00/0C/0E/rBEhWlJEHcwIAAAAAAEFI3XGv_YAADj-wC9W60AAQU7805.jpg"></li>
  </ul>
  <ul class="btns">
    <li class="cur"></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
</div>
<script src="jquery-1.9.1.js"></script>
<script src="slider.js"></script>
</body>
</html>

排版的核心技巧:.slider-focus 設 overflow:hidden 當裁切視窗,內層 .slider 寬度是單張圖乘以張數(3500px),五張 li 全部 float 排成一列,靠改變 left 位移就能看起來像在換圖。

JavaScript

function Sliderfocus(options){
  this.focus = options.focus;
  this.slider = options.slider;
  this.btns = options.btns;
  this.width = options.width;
  this.speed = options.speed || 800;
  this.curIndex = options.curIndex || 0;
  this.size = this.btns.size();
  this.init();
  this.timeout = null;
  this.stopTemp = 1 ;
}

Sliderfocus.prototype = {
  init:function(){
    this.auto();
    this.bind();
  },
  play:function(){
    this.slider.stop().animate({
      left: -this.curIndex * this.width
    },this.speed);
  },
  auto:function(){
    var that = this;
    this.timeout = setTimeout(function(){
      if(that.stopTemp == 0){
        return;
      }else{
        that.next();
        that.auto();
      }
    },4000);
  },
  prev:function(){
    this.curIndex = --this.curIndex < 0 ? this.size - 1 : this.curIndex;
    this.play();
  },
  next:function(){
    this.curIndex = ++this.curIndex > this.size - 1 ? 0 : this.curIndex;
    this.play();
  },
  stop:function(){
    this.stopTemp = 0;
  },
  bind:function(){
    var that = this;
    this.focus.bind("mouseover",function(){
      that.stop();
    }).bind("mouseout",function(){
      that.stopTemp = 1;
    });
    this.letsgo();
  },
  letsgo:function(){
    var that = this;
    this.btns.bind("click",function(){
      var index = $(this).index();
      that.curIndex = index;
      that.play();
    });
  }
};

new Sliderfocus({
  focus: $(".slider-focus"),
  slider: $(".slider-focus .slider"),
  btns: $(".btns li"),
  width: 670
});

設計上的幾個關鍵

用類別封裝狀態。 把 curIndex、size、speed 都掛在物件上,避免全域變數互相干擾,同一頁也能放多組輪播。

循環播放靠取模運算。 next 到底就回到 0、prev 到頭就跳到最後一張,實作比複製頭尾節點簡單得多。

動畫前先 stop()。 連續快速點擊時,若不明確停止進行中的動畫,位移會錯亂。

auto 用遞迴 setTimeout 而非 setInterval。 這樣每次播放都等動畫結束才排下一次,不會堆疊。

若要優化,可以再加上:點頁碼時同步切換 cur 樣式、支援觸控滑動、以及用 CSS transition 取代 jQuery animate 以交給 GPU 合成。但作為理解原理的範例,上面這份程式碼的每個部分都值得細讀。

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