简体   繁体   中英

jquery draggable custom containment

I want keep object inside parent while dragging. Sample fiddle is jsFiddle

$("#child").draggable({cursor:'move',axis: "x"});

axis is working fine but also child left and right should keep in parent. You should only drag in y axis and parent width.

Thanks

Use the containment option on the draggable element. Example Code :

$("#child").draggable({
    cursor:'move',
    axis: "x",
    containment: "parent"
});

Alternatively you can set this to be the ID of an element (ie containment:'#parent' )

There was a problem with your child element being constrained in your JSFiddle because it was bigger than the Parent Element. In my example fiddle below I have made the child element width:200px; so that it fits within the parent div (so it is constrained correctly).

Example JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM