Draggable revert if outside this div and inside of other draggables (using both invalid and valid re
2013-08-29 18:08
676 查看
On ui draggables ( http://jqueryui.com/demos/droppable/#revert ) is it possible to revert a div if its inside one div and if not inside another one? for example like this $( "#draggable" ).draggable({ revert: "valid", revert:"invalid" }); but that wouldn't work because of obvious reasons.. can I condition that though?.. for when its inside this droppable and not inside of that droppable? jquery jquery-ui drag-and-drop draggable jquery-ui-draggable
| |||||||||||
|
1 Answer
activeoldestvotesup vote21down voteaccepted | Your thinking was correct, you have to make the small boxes greedy droppables and handle the dropevent on them. The tricky part is to cancel the drag operation. By default, your draggables should start as revert:'invalid'. You don't have to do anything if they are dragged inside the big box, which in my example uses tolerance:'fit', so the small boxes must be completely inside to be accepted. I have made the small boxes greedy droppables with tolerance:'touch', so if the dragged small box touches another small box, it will call the draghandler on it. To cancel the drag operation from a drag handler, you can do a workaround of setting the dragged item to revert:true, which forces it to revert even though it was dropped on an accepting droppable. To make sure you can drag that small box again, on its drag stop event you have to reset revert:'invalid'. The stopevent will fire on every successful drop and if it's reverting, it will fireafter reverting has completed. You can try out a live demo here: http://jsfiddle.net/htWV3/1/ HTML: <div class="drop"> <div class="drag"></div> <div class="drag"></div> <div class="drag"></div> <div class="drag"></div> <div class="drag"></div> </div> CSS: .drop { display:inline-block; width:300px; height:200px; border:1px solid silver; background-color:whitesmoke; padding:10px; } .drag { display:inline-block; width:30px; height:30px; border:1px solid silver; background-color:white; } Javascript: $('.drop').droppable({ tolerance: 'fit' }); $('.drag').draggable({ revert: 'invalid', stop: function(){ $(this).draggable('option','revert','invalid'); } }); $('.drag').droppable({ greedy: true, tolerance: 'touch', drop: function(event,ui){ ui.draggable.draggable('option','revert',true); } });
|
相关文章推荐
- Installation failed with message...It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.
- It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing ___Error Installing APK
- proxy: No protocol handler was valid for the URL /. If you are using a DSO version of mod_proxy, make sure the proxy sub
- Invalid configuation file. File "**********" was created by a VMware product with more feature than this version of VMware Workstation and cannot be
- Treating HTML like XML using HtmlAgilityPack, and doing it inside of an XSLT too [转载]
- iOS:Class Foo is implemented in both MyApp and OtherApp. One of the two will be used. Which one is u
- Can I get rid of the "This page contains both secure and nonsecure items" warning?
- Treating HTML like XML using HtmlAgilityPack, and doing it inside of an XSLT too [转载]
- Treating HTML like XML using HtmlAgilityPack, and doing it inside of an XSLT too [转载]
- 异常crrently using minified code outside of NODE_ENV === 'production'. This means that 。。。
- failed to sync branch You might need to open a shell and debug the state of this repo
- [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe
- 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit
- Count the number of alphabets, spaces, digits and other characters in one line character
- Evaluate the Costs and Benefits of Using DBMS_REPAIR(2)【每日一译】--2012-10-27
- 【转载】Step by Step Creation of Crystal Report using its Features Group, Graph, Cross-Tab and Sub Report
- windows下python 安装igraph库报错 Cannot find the C core of igraph on this system using pkg-config.的解决
- Invalid component element. Instead of passing a string like 'div', pass React.createElement('div')
- My Fluid Simulation (SPH) Sample (3) – Optical Effects using GLSL, and Integration of Physical Model
- 1.4 Dynamically change the look of an application by using view states,transitions and effects