AlerBottom.js 4.68 KB

/*
 *usage
 		//显示
 		$.AlerStatus({
			status:'loading',         //状态
			msg:'上传中...',           //文字0
			duration:2000             //默认多少秒后隐藏,设null则一直不隐藏
		})
		
		//隐藏
		$.AlerStatus({
			type:'hide',              //显示或隐藏,默认为show
			time:3000                 //3s后隐藏,默认为0
		})
		
		//loading
		$.AlerBottom({
			status:'loading',         //加载中
			duration:null,
			callback:function(){
			}
		})
		setTimeout(function(){	
			//加载完成		
			$.AlerBottom({
				status:'loadingEnd',              
				time:1000,
				duration:null,
				callback:function(){
					$.AlerBottom({
						status:'success',
						msg:'保存成功,正在为您跳转(5)',
						duration:null
					})
					var leftTime=5;
					setInterval(function(){
						if(leftTime==0){
							//window.location.href="http://www.baidu.com";
						}else{
							leftTime--
							$('#AlerBottom .info').text('保存成功,正在为您跳转('+leftTime+')');
						}
					},1000)
				}
			})
		},2000)
 */

(function (window, document) {
	
	//选择器初始化
	var $AlerBottom,$AlerBottomIn,$AlerBottomBg,$info,$status;
	
    $(function () {
		init(); //初始化
		
		//选择器初始化
		$AlerBottom=$('#AlerBottom');
		$AlerBottomBg=$AlerBottom.find('.AlerBottomBg');
		$AlerBottomIn=$AlerBottomBg.find('.AlerBottomBg-in');
		$info=$AlerBottom.find('.info');
		$status=$AlerBottom.find('.status');
    })
    function init() {
       var  _html = '<!--弹出框-->' +
				 '<div id="AlerBottom" style="display:none;">'+
					'<div class="AlerBottomBg">'+
						'<div class="AlerBottomBg-in"></div>'+
					'</div>'+
					'<span class="status"></span>'+
					'<div class="info"></div>'+
				 '</div>'+
				 '<!--End 弹出框-->';
        var AlerHTML = document.createElement('div');
        AlerHTML.innerHTML = _html;
        document.body.appendChild(AlerHTML);
    }
	
	
	
	//loading动画
	var _loadingN=0;
	var A;
	
	var opts;
    $.AlerBottom = function (options,callback) {
		
		//默认参数
        var defaults = {
            type: 'show',
            status: null,
            msg: null,
            time: 0,
            duration: 2000
        };
        opts = $.extend({}, defaults, options);//赋值到全局变量opts

        if (opts.type === 'show') {
			
			$AlerBottom.show();
			
			if(opts.msg){
				$info.text(opts.msg);//如果有内容,则填充内容
				$AlerBottom.css('padding','10px 0');
			}else if(opts.status!=="loadingEnd"){
				$info.empty();
				$status.empty();
				$AlerBottom.css('padding','3px 0');
			}
			
			if(opts.duration){
				setTimeout(function(){
					_hide();
				},opts.duration);
			}
			
            switch(opts.status){
				case 'loading':
					if(opts.msg){
						$status.html('<div class="la-ball-clip-rotate la-sm"><div></div></div>');
					}
					$AlerBottomBg.html('<div class="AlerBottomBg-in"></div>');
					$AlerBottomIn=$AlerBottomBg.find('.AlerBottomBg-in');
					$AlerBottom.attr('class','AlerBottom-loading');
					AlerBottomBgLoading();
					needCallback();
					break;
				case 'loadingEnd':
					setTimeout(function(){
						clearInterval(A);
						$AlerBottomIn.width('100%');
						needCallback();
					},opts.time);
					break;
				case 'success':
					setTimeout(function(){
						$status.html('<div class="la-success la-sm"><div></div><div></div></div>');
						$AlerBottom.css('-webkit-transition','none');
						$AlerBottom.attr('class','AlerBottom-success');
						needCallback();
					},opts.time);
					break;
				case 'error':
					setTimeout(function(){
						$status.html('<div class="la-error la-sm"><div></div><div></div></div>');
						$AlerBottom.css('-webkit-transition','none');
						$AlerBottom.attr('class','AlerBottom-error');
						needCallback();
					},opts.time);
					break;
			}
        } else if(opts.type === 'hide') {
			_hide();
        }
		
    }
	
	//执行自定义callback函数
	function needCallback(){
		if(opts.callback){
			opts.callback();
		}
	}
	
	function AlerBottomBgLoading(){
		setTimeout(function(){
			_loadingN=40;
			$AlerBottomIn.css('width',_loadingN+"%");
			A=setInterval(function(){
				if(_loadingN<70){
					_loadingN+=30;
					$AlerBottomIn.css('width',_loadingN+"%");
				}else if(_loadingN<90){
					_loadingN+=20;
					$AlerBottomIn.width(_loadingN+"%");
				}else if(_loadingN<98){
					_loadingN+=1;
					$AlerBottomIn.width(_loadingN+"%");
				}else{clearInterval(A);}
			},500);
		},800);
	}
	
	function _hide(){
		setTimeout(function(){
			$AlerBottom.css('-webkit-transform','translateY(-100%)');
			setTimeout(function(){
				$AlerBottom.hide().removeAttr('style class');
				$AlerBottomIn.removeAttr('style').width(0);
			},300);
		},opts.time);
	}
   
})(window, document);