public.js
// 日期格式化功能:
function createDate(){ var d = new Date(); var y = d.getFullYear(); var m = d.getMonth(); var mydate = d.getDate(); var myday = d.getDay(); var h = d.getHours(); var mts = d.getMinutes(); var s = d.getSeconds(); switch(myday){ case 0:myday = "星期日";break; case 1:myday = "星期一";break; case 2:myday = "星期二";break; case 3:myday = "星期三";break; case 4:myday = "星期四";break; case 5:myday = "星期五";break; case 6:myday = "星期六";break; } return { year:y, month:createZero(m+1), date:createZero(mydate), day:myday, hours:createZero(h), minutes:createZero(mts), seconds:createZero(s) }; } // 数值的补零(小于10补零) function createZero(n){ return n<10 ? "0"+n : n; }
// 计算两个日期间的差值:
function dateDiff(d1,d2){ var oldDate = new Date(d1); var newDate = d2 ? new Date(d2) : new Date(); var t = Math.abs(oldDate.getTime() - newDate.getTime()); var d = parseInt(t/1000/60/60/24); var h = parseInt((t - d*24*60*60*1000)/1000/60/60); var m = parseInt((t - d*24*60*60*1000 - h*60*60*1000)/1000/60); var s = parseInt((t - d*24*60*60*1000 - h*60*60*1000 - m*60*1000)/1000); return { day:d, hours:h, minutes:m, seconds:s } }
// 范围随机数:
function random(max,min){ return Math.round(Math.random()*(max-min)+min); }
// 随机颜色:
function randomColor(){ return "rgb("+random(0,255)+","+random(0,255)+","+random(0,255)+")"; }
//判断一个数(字符),是否存在这个数组(字符串当中)
function norepeat(a,n) { for(var i=0;i<a.length;i++){ if (a[i]===n){ return true //相同返回true,在实际应用当中,我们常用到不相等,此时if中的条件是if(!norepeat(c,b[j])); } } return false }
//获取非行类样式(考虑兼容)
function getStyle(ele,attr){ var a = ""; if(ele.currentStyle){ a = ele.currentStyle[attr]; }else{ a = getComputedStyle(ele,false)[attr]; } return a; }
// 阻止事件冒泡(考虑兼容)
function stopBubble(e){ if(e.stopPropagation){ e.stopPropagation(); }else{ e.cancelBubble = true; } }
阻止默认事件(考虑兼容)
function stopDefault(e){ if(e.preventDefault){ e.preventDefault() //IE }else{ e.returnValue = false; //GOOGLE } } //封装函数
//事件的绑定(考虑兼容)
function addEvent(ele,type,cb){ if(ele.addEventListener){ ele.addEventListener(type,cb) }else if(ele.attachEvent){ ele.attachEvent("on"+type,cb) }else{ ele["on"+type] = cb; }
// 绑定事件的删除(考虑兼容)
function removeEvent(ele,type,cb){ if(ele.removeEventListener){ ele.removeEventListener(type,cb) }else if(ele.detachEvent){ ele.detachEvent("on"+type,cb) }else{ ele["on"+type] = null; } }
//拖拽的封装
function shift(ele) { addEvent(ele,"mousedown",down) function down(eve) { var e1=eve||window.event addEvent(document,"mousemove",move) function move(eve) { var e2=eve||window.event; var l=e2.pageX-e1.offsetX; var m=e2.pageY-e1.offsetY; if (l>document.documentElement.clientWidth-ele.clientWidth){ l=document.documentElement.clientWidth-ele.offsetWidth } if(m>document.documentElement.clientHeight-ele.offsetHeight){ m=document.documentElement.clientHeight-ele.offsetHeight } if(l<0){ l=0; } if(m<0){ m=0; } window.getSelection ? window.getSelection().removeAllRanges():document.selection.empty(); ele.style.left=l+"px" ele.style.top=m+"px" } addEvent(document,"mouseup",up) function up(eve) { var e3=eve||window.event; removeEvent(document,"mousemove",move); } } }
//多元素多属性的链式缓冲
function move(ele,json,callback){ clearInterval(ele.t); ele.t = setInterval(() => { // 在计时器每次开启后,for-in每次遍历前,创建状态,用来记录是否有属性没到终点 var onoff = true; for(var i in json){ var iNow = parseInt(getStyle(ele,i)); var speed = (json[i] - iNow)/6; speed = speed<0 ? Math.floor(speed) : Math.ceil(speed); // 如果有一个属性到目标,不一定清除计时器 // 如果有一个属性没到目标,一定不清除计时器 // 如果没有属性没到目标,一定清除计时器 // 状态,记录,所有属性有没有到目标 // 只要有属性没到终点,改变状态 if(iNow != json[i]){ onoff = false; } ele.style[i] = iNow + speed + "px"; } // 在for-in结束后(每个属性都遍历一次后),如果状态没有被改变,意味着没有属性没到终点,可以清除了 if(onoff){ clearInterval(ele.t); callback && callback(); } }, 30); }
事件委托的封装
function fn(child,callback) { return function (eve) { var e=eve||window.event; var target=e.target||e.srcElement; for(var i=0;i<child.length;i++){ if (child[i]==target){ callback.bind(target)() } } } }
长风破浪会有时,直挂云帆济沧海