我有一个只能通过 Y 轴拖动的元素。我希望它能够越过屏幕顶部(它可以),但禁止它越过屏幕底部。我在 jQuery Draggable API 中阅读了有关包含的内容,但没有找到我所问问题的答案,或者至少无法理解如何将其应用到我的案例中。到目前为止,这是我的代码:
JavaScript:
$(document).ready(function() {
$("#elem").draggable({
axis: "y"
});
});
最佳答案
一定有更好的方法但是;
$(document).ready(function() {
$("#elem").draggable({
axis: "y",
drag: function(event, ui) {
if($(this).offset().top + $(this).outerHeight() > $(window).height()) {
$(this).offset({"top": $(window).height() - $(this).outerHeight()});
event.preventDefault();
}
}
});
});
关于javascript - 可拖动 - 如何限制可拖动元素越过屏幕底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13662141/