Heim  >  Artikel  >  Web-Frontend  >  JS实现判断碰撞的方法_javascript技巧

JS实现判断碰撞的方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:14:251095Durchsuche

本文实例讲述了JS实现判断碰撞的方法。分享给大家供大家参考。具体如下:

JS判断碰撞方法:

复制代码 代码如下:
/** 判断是否碰撞
 * @param obj 原对象
 * @param dobj 目标对象
 */ 
function impact(obj, dobj) { 
    var o = { 
        x: getDefaultStyle(obj, 'left'), 
        y: getDefaultStyle(obj, 'top'), 
        w: getDefaultStyle(obj, 'width'), 
        h: getDefaultStyle(obj, 'height') 
    } 
 
    var d = { 
        x: getDefaultStyle(dobj, 'left'), 
        y: getDefaultStyle(dobj, 'top'), 
        w: getDefaultStyle(dobj, 'width'), 
        h: getDefaultStyle(dobj, 'height') 
    } 
 
    var px, py; 
 
    px = o.x     py = o.y  
    // 判断点是否都在两个对象中 
    if (px >= o.x && px = o.y && py = d.x && px = d.y && py         return true; 
    } else { 
        return false; 
    } 

 
/** 获取对象属性
 * @param obj       对象
 * @param attribute 属性
 */ 
function getDefaultStyle(obj, attribute) { 
    return parseInt(obj.currentStyle ? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj, false)[attribute]); 
}

示例如下:

复制代码 代码如下:
 
 
  
  demo  
   
  
  
 
 
   
 
   
 
 
 
  
  

希望本文所述对大家的javascript程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn