imgZoom.js 6.66 KB
var $=require("jquery");

+function($){
    'use strict';
    function ImgZoom(element,options){
        this.$element = $(element);
        if(!options){ 
            this.options = this.getDefaults();
        }else{
            this.options = $.extend(true,this.getDefaults(),options);
        }
        this.init();
    }
    ImgZoom.DEFAULTS = {
        isContainFather: false,
        overlay: true,
        template:'<div class="imgZoom" onselectstart="return false" unselecttable="on">'+
                    '<img src="" />'+
                    '<a href="javascript:;" class="close"></a>'+
                    '<a href="javascript:;" class="guideBtn prev"></a>'+
                    '<a href="javascript:;" class="guideBtn next"></a>'+
                '</div>'+
                '<div class="overlay"></div>',
        imgSrcAttr:'data-original'
    };
    ImgZoom.prototype.getDefaults = function () {
        return ImgZoom.DEFAULTS;
    };
    //初始化
    ImgZoom.prototype.init = function(){
        this.overlay = this.options.overlay;
        var $template = $('.imgZoom,.overlay');
        this.$template = !!$template.size() ? $template : ($(this.options.template).appendTo("body"));//Dom对象
        this.$box = this.$template.filter('.imgZoom');//相册对象
        this.$overlayObj = this.$template.filter('.overlay');//遮罩对象
        this.$btnPrev = this.$template.find('.guideBtn.prev');//上一张按钮
        this.$btnNext = this.$template.find('.guideBtn.next');//下一张按钮
        this.$btnClose = this.$template.find('.close');//关闭按钮
        this.currentObj = 'currentPhoto';//当前图片的标识类
        this.event();

        if(this.options.isContainFather){
            this.$element.find('['+this.options.imgSrcAttr+']').eq(0).trigger('click.imgzoom');
        }
    };
    //事件绑定集合
    ImgZoom.prototype.event = function(){
        var _this = this;
        this.btnClick();
        this.photoClick();
        
        this.$overlayObj.on('click',function(){
            _this.close();
        }); 
    };
    //左右按钮点击事件
    ImgZoom.prototype.btnClick = function(){
        var _this = this;
        this.$btnPrev.off('click');
        this.$btnNext.off('click');
        this.$btnPrev.on("click",function(){
            var that = _this.$template.data('target');
            that.photoSwitch(that.$element.find('['+_this.options.imgSrcAttr+']').index(that.$element.find("."+that.currentObj)),"left");

        });
        this.$btnNext.on("click",function(){
            var that = _this.$template.data('target');
            that.photoSwitch(that.$element.find('['+_this.options.imgSrcAttr+']').index(that.$element.find("."+that.currentObj)),"right");
            //console.log(_this.$element);
        }); 
    };
    //图片点击事件
    ImgZoom.prototype.photoClick = function(){
        var _this = this;

        this.$element.on("click.imgzoom",'['+_this.options.imgSrcAttr+']',function(){
            _this.$element.find("."+_this.currentObj).removeClass(_this.currentObj);
            $(this).addClass(_this.currentObj);
            _this.$template.data('target', _this);
            _this.photoEfficiency();
        }); 
    };
    //图片左右切换事件
    ImgZoom.prototype.photoSwitch = function(index,direction){

        this.$element.find("."+this.currentObj).removeClass(this.currentObj);
        var _index;
        if(direction=="left"){
            _index = index - 1;
        }else{
            _index = index + 1;
        }
        this.$element.find("["+this.options.imgSrcAttr+"]:eq("+_index+")").addClass(this.currentObj);
        this.photoEfficiency(); 
    };
    //检测首尾状态
    ImgZoom.prototype.getArrowState = function(){
        if(this.$element.find('['+this.options.imgSrcAttr+']').index(this.$element.find("."+this.currentObj))===0){
            this.$btnPrev.hide(); 
        }else{
            this.$btnPrev.show(); 
        }
        if(this.$element.find('['+this.options.imgSrcAttr+']').index(this.$element.find("."+this.currentObj))==this.$element.find('['+this.options.imgSrcAttr+']').size()-1){
            this.$btnNext.hide(); 
        }else{
            this.$btnNext.show(); 
        }
    };
    //图片显示效果
    ImgZoom.prototype.photoEfficiency = function(){
        var _this = this;
        var hiddenImg = $('<img src="'+_this.$element.find("."+this.currentObj).attr(this.options.imgSrcAttr)+'">').appendTo("body").css({"position":"fixed",'left':'-9999px','top':'-9999px'});

        this.loadImg(hiddenImg.attr("src"), function(){
            _this.$box.find("img").remove().end().prepend(hiddenImg.removeAttr("style"));

            if(!_this.$box.hasClass("show")){
                _this.open();
            }
            if(!_this.$box.hasClass("complete")){
                _this.$box.addClass("complete");
            } 
            _this.$box.css("margin-top",-_this.$box.outerHeight()/2);
            _this.$box.css("margin-left",-_this.$box.outerWidth()/2);
        });
        
        _this.getArrowState();
    };

    ImgZoom.prototype.loadImg = function(src, callback){
        var img = new Image();
        if($.browser.msie){
            obj.onreadystatechange=function(){
                if(this.readyState=="complete"){
                    callback.call(img);//将回调函数的this替换为Image对象
                    img = null;
                }
            };
        }else{
            img.onload = function () { //图片下载完毕时异步调用callback函数。
                callback.call(img);//将回调函数的this替换为Image对象
                img = null;
            };
        }
        img.src = src;
    };

    //打开事件
    ImgZoom.prototype.open = function(){
        this.$box.addClass("show");
        if(this.options.overlay){
            this.$overlayObj.fadeIn(100); 
        }
    };
    //关闭事件
    ImgZoom.prototype.close = function(){
        this.$box.removeClass("show complete");
        if(this.options.overlay){
            this.$overlayObj.fadeOut(100); 
        }
    };
    //组件封装处理
    function Plugin(option,object){
        this.each(function(){
            var $this = $(this),
                data = $this.data('hc.ImgZoom'),
                options = typeof option == 'object' && option;
            if(!data){
                $this.data('hc.ImgZoom',(data = new ImgZoom($this,options)));
            }
            if(typeof option =='string'){
                data[option](object);
            }
            return $this;
        });
    }
    var old = $.fn.ImgZoom;
    $.fn.ImgZoom = Plugin;
    $.fn.ImgZoom.Constructor = ImgZoom;
    $.fn.ImgZoom.noConflict = function () {
        $.fn.ImgZoom = old;
        return this;
    };
}($);