我用的是jquery draggable,想用if else语句判断点击按钮时弹出不同的弹窗,但是不行,怎么办?
$(".btn-popup").on("click", () => {
if ($(".year2").mouseout()) {
$(".popup-1").show();
} else if ($(".year58").mouseout()) {
$(".popup-2").show();
}
});
mouseout()
函数不返回布尔值,因此您的 if 语句不会按预期工作。其次,您需要使用 mouseenter
事件而不是 mouseout
来检测鼠标何时进入年元素。最后,您需要使用可拖动事件来检测用户何时将可拖动元素放到年份元素上。
试试这个。
$(".btn-popup").on("click", () => {
if ($(".year2").hasClass("dropped")) {
$(".popup-1").show();
} else if ($(".year58").hasClass("dropped")) {
$(".popup-2").show();
}
});
$(".draggable").draggable({
revert: "invalid",
stop: function(event, ui) {
const year = $(this).data("year");
const dropped = $(`#year${year}`).hasClass("dropped");
if (!dropped) {
$(this).css({ top: 0, left: 0 });
}
}
});
$(".year").droppable({
accept: ".draggable",
drop: function(event, ui) {
const year = $(this).data("year");
$(`#year${year}`).addClass("dropped");
},
out: function(event, ui) {
const year = $(this).data("year");
$(`#year${year}`).removeClass("dropped");
}
});