我正在尝试使用 jQuery 1.9.1
和 jQuery-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);
}
});
}
如何让物体随平台上升?非常需要这个帮助!谢谢。
最佳答案
我对你的代码做了一些调整:
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/