javascript - 将对象与 jQuery 中的另一个对象拉起来

标签 javascript jquery jquery-collision

我正在尝试使用 jQuery 1.9.1jQuery-collision 制作动画,我想将一个对象与它所站立的对象一起拉起来。站立平台会上升,它会带走站在上面的物体。但到目前为止,我无法做到这一点。这是我的代码,

$(document).ready(function () {
    heroDrop = setInterval(function () {
        var currentTop = parseInt($('#boy').css('top'));
        $('#boy').css('top', currentTop + 5);
    }, 50);

    barMove = setInterval(function () {
        var currentTop = parseInt($('.platbars').css('top'));
        $('.platbars').css('top', currentTop - 2);
    }, 100);

    setInterval(function () {
        action_process()
    }, 10);
});

function action_process() {
    $('#boy').each(function () {
        var fallstop = $(this).collision(".platbars");
        if (fallstop.length != 0) {
            clearInterval(heroDrop);
            heroDrop = null;
        } else {
            if (heroDrop !== null) return;
            heroDrop = setInterval(function () {
                var currentTop = parseInt($('#boy').css('top'));
                $('#boy').css('top', currentTop + 5);
            }, 50);
        }
    });
}

JSFiddle Demo Here

如何让物体随平台上升?非常需要这个帮助!谢谢。

最佳答案

我对你的代码做了一些调整:

var heroDrop;
var heroMove;
var barMove;
var coll;

$(document).ready(function () {
    heroDrop = setInterval(function () {
        var currentTop = parseInt($('#boy').css('top'));
        $('#boy').css('top', currentTop + 5);
    }, 50);

    barMove = setInterval(function () {
        var currentTop = parseInt($('.platbars').css('top'));
        $('.platbars').css('top', currentTop - 2);
    }, 100);

    coll = setInterval(function () {
        action_process()
    }, 10);
});

function action_process() {
    $('#boy').each(function () {
        var fallstop = $(this).collision(".platbars");
        if (fallstop.length != 0) {
            clearInterval(heroDrop);
            clearInterval(coll);
            heroDrop = setInterval(function () {
                var currentTop = parseInt($('#boy').css('top'));
                $('#boy').css('top', currentTop - 2);
            }, 100);
        }
    });
}

关于javascript - 将对象与 jQuery 中的另一个对象拉起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30695449/

相关文章:

javascript - jquery为每个按键事件生成不同的id

javascript - 如何为浏览器音频播放器添加静音/取消静音按钮?

javascript - ExtJS 6 : Should I use the config object?

javascript - 谷歌网站。 JS 与电子表格的连接

javascript - JS中通过onclick访问对象外的方法

javascript - Android 喜欢状态栏吗?

javascript - jQuery .click() 只工作一次

jQuery 选择器链