简体   繁体   English

检测两个div冲突的最有效方法

[英]Most efficient way to detect collision of two divs

I'm using Jquery Collision to detect two objects overlapping each other. 我正在使用jQuery Collision来检测彼此重叠的两个对象。 Here is a JSFiddle of the problem. 这是问题的JSFiddle (apologies for including jquery collision script in HTML, couldn't find other way) (对于在HTML中包含jquery collision脚本的道歉,找不到其他方法)

Click anywhere in the gray container to move the green div over the white div. 单击灰色容器中的任意位置,将绿色div移到白色div上。

HTML Structure: HTML结构:

<div class="container">
    <div class="test"></div>
    <div class="menu"></div>
</div>

JS: JS:

$(document).ready(function () {
    var hit_list;
    $(".container").click(function () {

        $(".menu").stop().animate({
            left: "+=100px"
        }, 300, function () {
            $(".menu").animate({
                left: "0"
            }, 800);
        });
        //Test for collision
        hit_list = $(".menu").collision(".test");
        if (hit_list.length != 0) {
            alert("welcome Earthling!");
        }
    });
});

The problem with my method is that, it doesn't detect collision every time. 我的方法的问题在于,它不会每次都检测到冲突。 Even though it passes over the white division fine, the alert isn't displayed everytime. 即使它超过了白色分割罚款, alert也不会每次显示。

Am I going wrong anywhere in checking for collision? 检查碰撞时,我在哪里出错? Is there a better/more efficient method to detect collisions during animation ? 有没有更好/更有效的方法来检测animation过程中的碰撞?

jQuery animate has a step callback ( https://api.jquery.com/animate/ ), it gets executed after each step of the animation. jQuery animate有一个步骤回调( https://api.jquery.com/animate/ ),它在动画的每一步之后执行。

Use it like this: 像这样使用它:

$(document).ready(function () {
    var hit_list;
    $(".container").click(function () {

        $(".menu").stop().animate({
            left: "+=100px"
        }, {
           duration: 300,
           complete: function () {
               $(".menu").animate({
                   left: "0"
               }, 800);
           },
           step: function(){
               //Test for collision
               hit_list = $(".menu").collision(".test");
               if (hit_list.length != 0) {
                   alert("welcome Earthling!");
               }
           }
         });
     });
 });

Try this http://jsfiddle.net/aamir/y7PEp/6/ 试试这个http://jsfiddle.net/aamir/y7PEp/6/

$(document).ready(function () {
    var hit_list;
    var hits=0;
    $(".container").click(function () {
        var $this = $(this);
        function checkCollision() {
           //Test for collision
            hit_list = $(".menu").collision(".test");
            if (hit_list.length != 0) {
                hits++;
                $(".menu").html(hits+ ' hits');
            } 
        }

        $(".menu").stop().animate({
            left: "100px"
        }, 300, function () {
            checkCollision();
            $(".menu").animate({
                left: "0"
            }, 800);
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM