实用小技巧备份

css: 父元素   display:flex;

子元素:margin0;

即可实现子元素的水平与垂直居中。


js: 封装一个获取id函数,类似JQuery

function $(id){

return document.getElementById(id.substring(1))

}


碰撞检测封装
function pengzhuang(obj1,obj2){
var obj1_top=obj1.offsetTop;
var obj1_bottom=obj1.offsetTop+obj1.offsetHeight;
var obj2_top=obj2.offsetTop;
var obj2_bottom=obj2.offsetTop+obj2.offsetHeight;

var obj1_left=obj1.offsetLeft;
var obj1_right=obj1.offsetLeft+obj1.offsetWidth;
var obj2_left=obj2.offsetLeft;
var obj2_right=obj2.offsetLeft+obj2.offsetWidth;

if(obj1_bottom>=obj2_top&&obj1_top<=obj2_bottom&&obj1_left<=obj2_right&&obj1_right>=obj2_left){
alert("booom!!");   // 碰撞的代码块
}
}




你可能感兴趣的:(实用小技巧备份)