圖片放大縮小的簡單實現

/**
 * 圖片放大縮小的簡單實現
 * Depends: jquery.min.js
 * @author Lin Chenglin
 * @date 2012-12-04
 */
(function($) {
 $.fn.extend( {
  zoom : function(options) {
   var opts = $.extend( {}, $.fn.zoom.defaults, options);
   this.each(function(i, obj) {
    _zoom(obj, opts);
   });
   $(this).click(
     function() {
      if ($(this).height() > opts.height
        || $(this).width() > opts.width) {
       _zoom(this, opts);
      } else {// 放大到圖片的實際尺寸
       $(this).css( {
        width : '',
        height : ''
       });
      }
     })
  }
 })
 /**
  * 默認配置
  */
 $.fn.zoom.defaults = {
  height : 50,
  width : 50
 };
 /**
  * 縮小或放大
  */
 function _zoom(obj, opts) {
  var height = opts.height;
  var width = opts.width;
  var heightP = height / $(obj).height();
  var widthP = width / $(obj).width();
  if (heightP >= widthP) {
   $(obj).width(width);
  } else {
   $(obj).height(height);
  }
 }
})(jQuery);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章