工具类

调用方法:

window.methods.+funname();

代码:

//active
window.onload = function() {
		document.body.addEventListener('touchstart', function() {});
		document.body.addEventListener('touchmove', function() {});
		document.body.addEventListener('touchend', function() {});
	}
	(function(root, factory) {
		if (typeof define === "function" && define.amd) {
			define(["jquery"], factory);
		} else if (typeof exports === 'object') {
			module.exports = factory();
		} else {
			root.methods = factory(jQuery);
		}
	}(this, function($) {
		//提示框
		var time, $al;

		function Alert(str) {
			$al && $al.remove();
			$al = $('<div>' + str + '</div>').css({
				'position': 'fixed',
				'bottom': '25%',
				'z-index': '99999',
				'-webkit-border-radius': '3px',
				'border-radius': '3px',
				'text-align': 'center',
				'background': 'rgba(0,0,0,0.8)',
				'font-size': '14px',
				'color': '#ccc',
				'padding': '4px 7px'
			});
			time && clearTimeout(time);
			$("body").append($al);
			$al.css('left', parseFloat($(window).width() - $al.width() - 30) / 2).show();
			time = setTimeout(function() {
				$al.remove();
			}, 3000)
		}
		var move = function(e) {
				e.preventDefault && e.preventDefault();
				e.returnValue = false;
				e.stopPropagation && e.stopPropagation();
				return false;
			}
			//可以滚动
		function remliste() {
			window.removeEventListener('touchmove', move);
		}
		//不能滚动
		function addliste() {
			window.addEventListener('touchmove', move);
		}
		/*匹配出字符串的数字*/
		function returnInt(str) {
			return str.match(/\d+/g);
		}

		/* loading效果 */
		function loadingStart(a) {
			var top = $(window).height() / 2 - 40;
			var left = $(window).width() / 2 - 40;
			var _a = a || 1;
			var top = '<div class="back-layer bg-layer"><div class="loading" style="top:' + top + 'px;left:' + left + 'px;">'
			var tpl = {
				1: function() {
					return top + '<div class="spinner1">' + '<div class="bounce1"></div>' + '<div class="bounce2"></div>' + '<div class="bounce3"></div>' + '</div>' + '</div></div>';
				},
				2: function() {
					return top + '<div class="spinner2">' + '<div class="rect1"></div> ' + '<div class="rect2"></div> ' + '<div class="rect3"></div> ' + '<div class="rect4"></div> ' + '<div class="rect5"></div> ' + '</div>' + '</div></div>';
				},
				3: function() {
					return top + '<div class="spinner3">' + '<div class="cube1"></div>' + '<div class="cube2"></div>' + '</div>' + '</div></div>';
				},
				4: function() {
					return top + '<div class="spinner5">' + '<div class="double-bounce1"></div>' + '<div class="double-bounce2"></div>' + '</div>' + '</div></div>'
				}

			}[_a]
			$(tpl()).appendTo('body');
		}

		/* loading效果 */
		function loadingEnd() {
			$(".bg-layer").remove();
		}

		/* 返回顶部 */
		function toTop() {

			$backToTopEle = $('<div class="toTop" style="position:fixed; bottom:10px; right:5px; z-index:999; width:34px; height:35px;"><img src="./imgs/toTop.png" width=34px height=35px></div>').appendTo($("body")).click(function() {
				$("html, body").animate({
					scrollTop: 0
				}, 200);
			});

			$backToTopFun = function() {
				var st = $(document).scrollTop(),
					winh = $(window).height();
				(st > 0) ? $backToTopEle.show(): $backToTopEle.hide();
			};

			$(window).bind("scroll", $backToTopFun);
			$(function() {
				$backToTopFun();
			});
		}

		/*提取连接参数*/
		function getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			var result = "";
			if (r != null) {
				result = unescape(r[2]);
			}
			return result;
		}

		/*替换电话4-8位*/
		function replaceTel(str) {
			return str.replace(str.slice(3, 7), "****")
		}

		/*判断手机号*/
		function verifyPhone(str) {
			var reg = /^(\+86)|(86)?1[3,5,8,7]{1}[0-9]{1}[0-9]{8}$/;
			if (reg.test(str)) {
				return true;
			} else {
				return false;
				//alert("请确认手机号是否正确");
			}
		}

		//      判断电话号
		function zhuoPhone(str) {
			var reg = /^(\d{3,4}-)?\d{7,8}$/;
			if (reg.test(str)) {
				return true;
			} else {
				return false;
			}
		}

		/*判断电话和手机*/
		function phoneAll(str) {
			var reg = /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[3578]\d{9})$)/;
			if (reg.test(str)) {
				return true;
			} else {

			}
		}
		/*判断邮箱*/
		function verifyEmail(str) {
			var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
			if (reg.test(str)) {
				return true;
			} else {
				//alert("请确认email是否正确");
			}
		}

		/*判断邮编*/
		function verifyPost(str) {
			var reg = /^[1-9][0-9]{5}$/;
			if (reg.test(str)) {
				return true;
			} else {
				//alert("请确认邮编是否正确");
			}
		}

		/*判断姓名长度*/
		function verifyName(str) {
			var len = str.length;
			if (len <= 10) {
				return true;
			} else {
				//alert("名字不能超过10个字符");
			}
		}

		/*判断身份证*/
		function isIdCardNo(idCardNo) {
			//15位和18位身份证号码的基本校验
			var check = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idCardNo);
			if (!check) {
				return false;
			} else {
				return true;
			}
			//判断长度为15位或18位  
			//          if(idCardNo.length==15){
			//              return idCardNoUtil.check15IdCardNo(idCardNo);
			//          }else if(idCardNo.length==18){
			//              return idCardNoUtil.check18IdCardNo(idCardNo);
			//          }else{
			//              return false;
			//          }
		}

		/*字符串拼加,相加*/
		function strJoin(str, n) {
			return (new Array(n + 1)).join(str)
		}

		/*模拟alert框,有cb就执行cb*/
		function alert(text, cb) {
			var tpl = '<div class="back-layer-alert alert-box"><div class="alertBox" style="width:300px;min-height:100px;position:fixed;top:50%;left:50%;margin:-50px 0 0 -150px;z-index:20005;box-shadow:1px 1px 3px rgba(0,0,0,.1);background:#fff;border:1px solid #ccc;color:#333;border-radius:5px"><div class="modal-body" style="padding:15px;word-break: break-all;font-size: 15px;">' + text + '</div><div style="padding:14px 15px 10px;text-align:center;background-color:#f5f5f5;border-top:1px solid #ddd;-webkit-border-radius:0 0 6px 6px"><a class="btn btn-primary closeAlertBox" href="javascript:;" style="color:#fff">确定</a></div></div></div>';
			$(tpl).appendTo('body');

			$('.alert-box').delegate('.closeAlertBox', 'click', function() {
				$(".alert-box").remove();
				if (cb && typeof(cb) === 'function') {
					cb()
				}
			});

		}

		/*模拟comfirm框,有cb就执行cb*/
		function comfirm(text, cbOk, cbNo) {
			var tpl = '<div class="back-layer-comfirm comfirm-box"><div class="comfirmBox" style="width:300px;min-height:100px;position:fixed;top:50%;left:50%;margin:-50px 0 0 -150px;z-index:20005;box-shadow:1px 1px 3px rgba(0,0,0,.1);background:#fff;border:1px solid #ccc;color:#333;border-radius:5px"><div class="modal-body" style="padding:15px;word-break: break-all;font-size: 15px;">' + text + '</div><div style="padding:14px 15px 10px;text-align:center;background-color:#f5f5f5;border-top:1px solid #ddd;-webkit-border-radius:0 0 6px 6px"><a class="btn cancelComfirmBox" href="javascript:;" style="margin-right:10px">取消</a><a class="btn btn-primary closeComfirmBox" href="javascript:;">确定</a></div></div></div>';
			$(tpl).appendTo('body');

			$('.comfirm-box').delegate('.closeComfirmBox', 'click', function() {
				$(".comfirm-box").remove();
				if (cbOk && typeof(cbOk) === 'function') {
					cbOk()
				}
			})

			$('.comfirm-box').delegate('.cancelComfirmBox', 'click', function() {
				$(".comfirm-box").remove();
				if (cbNo && typeof(cbNo) === 'function') {
					cbNo()
				}
			})

		}

		/*模拟comfirm框input输入框,有cb就执行cb*/
		function inputComfirm(title, text, cbOk, cbNo) {
			var mb = title == "" ? '' : 'margin-bottom: 10px;';
			var tpl = '<div class="back-layer-comfirm inputComfirm-box"><div class="inputComfirmBox" style="width:300px;min-height:100px;position:fixed;top:50%;left:50%;margin:-50px 0 0 -150px;z-index:20005;box-shadow:1px 1px 3px rgba(0,0,0,.1);background:#fff;border:1px solid #ccc;color:#333;border-radius:5px"><div class="modal-body" style="padding:15px;word-break: break-all;font-size: 15px;"><h4 style="text-align: center;font-size: 1rem;color: #777;font-weight: normal;' + mb + '">' + title + '</h4><input type="text" class="inputComfirm" value="' + text + '" style="width:100%" placeholder=""></div><div style="padding:14px 15px 10px;text-align:center;background-color:#f5f5f5;border-top:1px solid #ddd;-webkit-border-radius:0 0 6px 6px"><a class="btn cancelInputComfirmBox" href="javascript:;" style="margin-right:10px">取消</a><a class="btn btn-primary closeInputComfirmBox" href="javascript:;">确定</a></div></div></div>';
			$(tpl).appendTo('body');

			$('.inputComfirm-box').delegate('.closeInputComfirmBox', 'click', function() {
				var inputVal = $('.inputComfirm').val();
				$(".inputComfirm-box").remove();
				if (cbOk && typeof(cbOk) === 'function') {
					cbOk.call(this, inputVal, title)
				}

			})

			$('.inputComfirm-box').delegate('.cancelInputComfirmBox', 'click', function() {
				$(".inputComfirm-box").remove();
				if (cbNo && typeof(cbNo) === 'function') {
					cbNo()
				}
			})

		}

		/*提示框,7个字以内*/
		function tipsBox(text) {

			var tpl = '<div class="tipsBox" style="width:100px;min-height:20px;position:fixed;top:50%;left:50%;margin:-10px 0 0 -50px;z-index: 20005!important;background:rgba(0,0,0,.6);color:#fff;padding:3px 4px;border-radius:4px;font-size:13px;text-align:center;overflow:hidden;display:none;">' + text + '</div>';
			$(tpl).appendTo('body').fadeIn();

			function tipsClose() {
				$(".tipsBox").fadeOut(function() {
					$(this).remove();
				})
			}

			setTimeout(tipsClose, 1200)

		}

		/*时间戳转时间*/
		function convertDate(str, hasMS) {
			var hms = "";
			var date = new Date(str);
			Y = date.getFullYear() + '-';
			M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			D = date.getDate() + ' ';
			h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
			m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
			s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
			hms = hasMS ? h + m + s : "";
			return Y + M + D + hms; //Y+M+D+h+m+s
		}
		
		/*时间戳转时间*/
        function convertDate2(str, hasMS) {
            var hms = "";
            var date = new Date(str);
            Y = date.getFullYear() + '-';
            M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
            m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes();
            s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            hms = hasMS ? h + m + s : "";
            return M + D + h+m; //Y+M+D+h+m+s
        }
        
        /*时间戳转时间*/
        function convertDate3(str, hasMS) {
            var hms = "";
            var date = new Date(str);
            Y = date.getFullYear() + '-';
            M = (date.getMonth() + 1 < 10 ?   (date.getMonth() + 1) : date.getMonth() + 1);
            D = date.getDate() + '';
            h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
            m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes();
            s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            hms = hasMS ? h + m + s : "";
            return M+"月"+D+"日"; //Y+M+D+h+m+s
        }

		/*获取验证码*/
		function getVerifyCode() {
			var getBtn = $('.getVerifyCode'),
				sys_second = 60;

			var timer = setInterval(function() {
				if (sys_second > 1) {
					sys_second -= 1;

					getBtn.html('<b class="second" style="color: #fff;">' + sys_second + 's 重新获取</b>');
					getBtn.removeClass('getVerifyCode').addClass('disabled');
				} else {
					clearInterval(timer);
					getBtn.removeClass('disabled').addClass('getVerifyCode');
					getBtn.text('获取验证码')
					$(document).delegate('.getVerifyCode', 'click', function(event) {
						getVerifyCode();
					});
				}
			}, 1000);
		}
		function getVerifyCode2() {
            var getBtn = $('.getVerifyCode'),
                sys_second = 120;
            
            var timer = setInterval(function() {
                if (sys_second > 1) {
                    getBtn.html('<b class="second" style="color: #fff;">' + sys_second + 's </b>');
                    sys_second -= 1;

                    getBtn.html('<b class="second" style="color: #fff;">' + sys_second + 's </b>');
                    getBtn.removeClass('getVerifyCode').addClass('disabled');
                } else {
                    clearInterval(timer);
                    getBtn.removeClass('disabled').addClass('getVerifyCode');
                    getBtn.text('获取')
                    $(document).delegate('.getVerifyCode', 'click', function(event) {
                        getVerifyCode2();
                    });
                }
            }, 1000);
        }

		/*匹配筛选tel*/
		function matchTel(str, reg) {
			var pos = "",
				len = reg.length;
			if (str.search(new RegExp(reg)) != -1) {
				//console.log(len)
				pos = str.search(new RegExp(reg)); //匹配为第几个位置
				if (pos == 0) { //首位
					//console.log(len)
					return "<b>" + str.substr(0, len) + "</b>" + str.substring(len)
				} else if (pos == 7) { //末尾
					//console.log(pos)
					return str.substring(0, pos) + "<b>" + str.substr(pos, len) + "</b>"
				} else {
					//console.log(str.substring(pos+len))
					return str.substring(0, pos) + "<b>" + str.substr(pos, len) + "</b>" + str.substring(pos + len)
				}

			} else {
				return str
			}

		}

		/*去链接的参数值*/
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return decodeURIComponent(r[2]);
			return null;
		}

		/*0点过期*/
		/*function oneDayOutdate(n){
		    var date=new Date();
		    date.setDate( date.getDate()+1);
		    date.setHours(0);
		    date.setMinutes(0);
		    date.setSeconds(0);
		    document.cookie="date_cookie;expires=" + date.toGMTString();
		}*/
		function oneDayOutdate(name, value) {
			var date = new Date();
			date.setDate(date.getDate() + 1);
			date.setHours(0);
			date.setMinutes(0);
			date.setSeconds(0);
			document.cookie = name + "=" + escape(value) + ";expires=" + date.toGMTString();
		}

		function getCookie(name) {
			var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

			if (arr = document.cookie.match(reg))
				return decodeURIComponent(arr[2]);

			else return null;

		}

		function addCookie(objName, objValue, objHours) { //添加cookie
			var str = objName + "=" + escape(objValue);
			if (objHours > 0) { //为0时不设定过期时间,浏览器关闭时cookie自动消失
				var date = new Date();
				var ms = objHours * 3600 * 1000;
				date.setTime(date.getTime() + ms);
				str += "; expires=" + date.toGMTString();
			}
			document.cookie = str;
			//alert("添加cookie成功");
		}

		//比较日期大小,a为开始,b为结束
		function compareDate(a, b) {

			var dateA = Date.parse(a.replace(/-/g, "/")).valueOf();
			var dateB = Date.parse(b.replace(/-/g, "/")).valueOf();

			if (dateA > dateB) {

				//fun.alert('结束时间不能小于开始时间');
				return false;

			} else {

				return true;
			}

		}
		
		
		//倒计时
		function daojishi(el,intDiff,text,a){//el:位置;intDiff:倒计时秒数;text:倒计时注;a:a有值就用时分秒,反之用“:”
			var time = "";
			time += '<span id="text_show"></span><strong id="day_show"></strong><strong id="hour_show"></strong><strong id="minute_show"></strong><strong id="second_show"></strong>';
			$(el).append(time);
			window.setInterval(function(){
				var day=0,
					hour=0,
					minute=0,
					second=0;//时间默认值		
		
				if(intDiff > 0){
					day = Math.floor(intDiff / (60 * 60 * 24));
					hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
					minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
					second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
				}else{
					parent.location.reload();
				}
			
				if (minute <= 9) minute = '0' + minute;
				if (second <= 9) second = '0' + second;
				
				$('#text_show').html(text);
				$('#day_show').html("<span>"+day+"</span>"+(a?'天':':'));
				$('#hour_show').html("<span>"+hour+"</span>"+(a?'时':':'));
				$('#minute_show').html("<span>"+minute+"</span>"+(a?'分':':'));
				$('#second_show').html("<span>"+second+"</span>"+(a?'秒':''));
				
				intDiff--;
		
			}, 1000);
		}

		
		return {

			returnInt: returnInt,
			loadingStart: loadingStart,
			loadingEnd: loadingEnd,
			toTop: toTop,
			getUrlParam: getUrlParam,
			replaceTel: replaceTel,
			verifyPhone: verifyPhone,
			verifyEmail: verifyEmail,
			verifyPost: verifyPost,
			verifyName: verifyName,
			strJoin: strJoin,
			alert: alert,
			comfirm: comfirm,
			tipsBox: tipsBox,
			inputComfirm: inputComfirm,
			convertDate: convertDate,
			convertDate2: convertDate2,
			convertDate3:convertDate3,
			getVerifyCode: getVerifyCode,
			isIdCardNo: isIdCardNo,
			matchTel: matchTel,
			getQueryString: getQueryString,
			oneDayOutdate: oneDayOutdate,
			getCookie: getCookie,
			addCookie: addCookie,
			compareDate: compareDate,
			Alert: Alert,
			zhuoPhone: zhuoPhone,
			phoneAll: phoneAll,
			remliste: remliste,
			addliste: addliste,
			daojishi: daojishi,
			getVerifyCode2:getVerifyCode2
		}

	}));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值