gpt4 book ai didi

javascript - 可拖动 - 如何限制可拖动元素越过屏幕底部?

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:53 25 4
gpt4 key购买 nike

我有一个只能通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com