draggable() - 防止在窗口外拖动元素

时间:2011-05-12 10:08:09

标签: jquery

如何阻止用户将元素拖到窗口外?

elm.css({
        top : $(window).scrollTop() + ($(window).height() - elm.height()) / 2+'px',
        left : ($(window).width() - elm.width()) / 2+'px'
    })
    .draggable({
        handle : tr_top
    });

1 个答案:

答案 0 :(得分:15)

您可以使用containment选项:

elm.draggable({
    handle: tr_top,
    containment: "window"
});
相关问题