jQuery UI how to set draggable containment option on parent's parent

混江龙づ霸主 提交于 2019-12-01 03:09:19

As per the documentation, the containment option can also be a jquery selector or an actual element. So you can do this:

$('.selector').draggable({
    containment: $('.selector').parent().parent()
});

Or even better yet:

$('.selector').each(function(){
    $(this).draggable({
        containment: $(this).parent().parent()
    });
});

According to the docs, you do not have to pass in a string for the containment property. You can pass in any of:

Selector, Element, String, Array

So, just select the parent's parent with jQuery (i.e. $( ".selector" ).parent().parent()), and pass in instead of 'parent'.

LABASTIE François

This works fine as well:

$('.selector').draggable({
    containment: "#parent",
    scroll: false
});

If i use $(this).parent() my element go over the parent element.

$('.selector').draggable({
  containment: $(this).parent()
});

But with parent it works fine.

$('.selector').draggable({
  containment: "parent"
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!