js中最最常用的函数封装,方便调用
作者:互联网
事件委托的封装
function eveEnt(child,cb){
return function(eve){
var e = eve || window.event;
var mubiaoEle = e.target || e.srcElement;
for(var i = 0;i<child.length;i++){
if(child[i]==mubiaoEle){
cb.bind(mubiaoEle)();
}
}
}
}
事件监听式绑定事件
function addEvent(ele,type,cb){
if(ele.addEventListener){
ele.addEventListener(type,cb,false);
}else{
ele.attachEvent("on"+type,cb)
}
}
阻止事件冒泡的封装
function stopBubble(eve){
if(event.stopPropagation){
eve.stopPropagation();
}else{ eve.cancelBubble=true; }
}
非行内样式的兼容
function getStyle(ele,attr){
if(ele.currentStyle){
return ele.currentStyle[attr];
}else{
return getComputedStyle(ele,false)[attr];
}
}
缓冲运动的封装
function move(ele,attr,target){
clearInterval(ele.t);
ele.t=setInterval(()=>{
var s=getStyle(ele,attr);
var iNow=parseInt(s);
let speed=(target-iNow)/10;
speed=speed<0? Math.floor(speed) :Math.ceil(speed);
if(target==iNow){
clearInterval(ele.t);
}else{
ele.style[attr]=iNow+speed+"px";
}
},30);
}
随机数的封装
function random(min, max) {
return Math.round(Math.random()*(max - min)+min);
}
后期还会更新,敬请期待哦!!!
标签:function,最最,封装,attr,eve,ele,var,js 来源: https://blog.csdn.net/MZTing/article/details/104652771