DIV CSS 佈局教程網

動手寫一個 jQuery 圖片 hover 插件 jCutter
編輯:JQuery特效代碼     

先看用法。HTML 只要包一層容器和一個內容層:

<div class="jcutter">
  <img src="1.jpg" alt="">
  <div class="jcutter-content">
  這是點開後的頁面的內容
  </div>
</div>

呼叫時傳入速度與 easing 設定:

$(document).ready(function(){
  options = {
    'speedIn': 600,           // 圖片進入時的動畫速度
    'speedOut': 400,          // 圖片退出時的動畫速度
    'easeIn': 'easeOutBounce',// 進入效果,需要 easing 庫
    'easeOut': ''             // 退出效果
  };
  $('.jcutter').jCutter(options);
})

一、jQuery 插件的基本骨架

(function($){
  $.fn.jCutter = function(o){
    o = $.extend({
      speedIn: 300,
      speedOut: 300,
      easeIn: '',
      easeOut: ''
    }, o || {});
  };
})(jQuery);

三個要點:包成閉包不污染命名空間;透過 jQuery 提供的介面掛上去(jCutter 換成你自己的名字即可);$.extend 會把第二個參數合併進第一個,同名鍵以「後者」覆蓋前者,因此使用者傳入的設定會蓋掉預設值。

二、拆出三個階段

(function($){
  $.jCutter = function(node, o){
    o = $.extend({
      speedIn: 300, speedOut: 300, easeIn: '', easeOut: ''
    }, o || {});
    var that = this;
    that.init     = function(){};
    that.generate = function(){};
    that.cutter   = function(){};
    that.init();
  };
  $.fn.jCutter = function(o){
    return this.each(function(i){ $.jCutter(this, o); });
  };
})(jQuery);

把 $.jCutter 掛在 jQuery 物件上而非 $.fn,等於替 jQuery 加了一個「類別」;$.fn.jCutter 則負責對每個符合的元素各建一份實例。init 做初始化、generate 產生需要的結構、cutter 掛動畫與事件,分工清楚。

三、初始化

that.init = function(){
  that.node = $(node);
  that.img = that.node.find('img');
  that.speedIn = o.speedIn;
  that.speedOut = o.speedOut;
  that.easeIn = o.easeIn;
  that.easeOut = o.easeOut;
  that.generate();
  that.cutter();
};

四、產生四塊拼圖

原理是把同一張圖複製到四個 div,各自用 background-position 顯示原圖的四分之一,拼回來就是完整圖片;hover 時讓這四塊往四個方向飛散。

that.generate = function(){
  var w = that.node.width() / 2;
  var h = that.node.height() / 2;
  that.imga = [];
  for (var i = 0; i < 4; i++) {
    that.imga[i] = document.createElement('div');
    that.imga[i] = $(that.imga[i]);
    that.imga[i].css({
      'position': 'absolute',
      'z-index': '2',
      'width': w,
      'height': h,
      'background': 'url("' + that.img.attr("src") + '") no-repeat'
    });
    $(that.node).append(that.imga[i]);
  }
  that.imga[0].css({ 'left':'0px', 'top':'0px' });
  that.imga[1].css({ 'right':'0px', 'top':'0px',
                     'background-position': '-' + w + 'px 0px' });
  that.imga[2].css({ 'left':'0px', 'bottom':'0px',
                     'background-position': '0px -' + h + 'px' });
  that.imga[3].css({ 'right':'0px', 'bottom':'0px',
                     'background-position': '-' + w + 'px -' + h + 'px' });
  that.img.remove();
};

這裡有個容易忽略的前提:外層容器的 position 必須是 relative,否則四塊絕對定位的元素會以整個頁面為參考,位置就全亂了。

五、加上動畫與事件

that.cutter = function(){
  var w = that.node.width() / 2;
  var h = that.node.height() / 2;
  that.node.hover(function(){
    that.imga[0].stop().animate({'left':'-'+w,'top':'-'+h}, that.speedOut, that.easeOut);
    that.imga[1].stop().animate({'right':'-'+w,'top':'-'+h}, that.speedOut, that.easeOut);
    that.imga[2].stop().animate({'left':'-'+w,'bottom':'-'+h}, that.speedOut, that.easeOut);
    that.imga[3].stop().animate({'right':'-'+w,'bottom':'-'+h}, that.speedOut, that.easeOut);
  }, function(){
    that.imga[0].stop().animate({'left':0,'top':0}, that.speedIn, that.easeIn);
    that.imga[1].stop().animate({'right':0,'top':0}, that.speedIn, that.easeIn);
    that.imga[2].stop().animate({'left':0,'bottom':0}, that.speedIn, that.easeIn);
    that.imga[3].stop().animate({'right':0,'bottom':0}, that.speedIn, that.easeIn);
  });
};

.stop() 的作用是在事件連續觸發時先中止上一段還在跑的動畫,避免動畫堆疊導致視覺延遲。easing 參數方面,jQuery 核心只內建 linear 與 swing 兩種,想要 easeOutBounce 這類彈跳效果要另外載入 jQuery Easing 外掛。

邏輯清楚、程式碼不長,是很適合拿來練手的小插件。完整打包可參考 jb51 的範例檔。

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