i want the draggable object to revert back to its original position when i click a button. i used 'destroy' option but it doesnt seem to work. it disables the dragging but doesnt revert back to original position.
can someone help?
EDIT PART:
$('#Zoom').toggle(function() {
$("#draggable").draggable({});},
function() {
$("#draggable").draggable('destroy');});
this is a toggle button that i am using to enable dragging. the destroy option only disables dragging and does not revert the object back to the original position.
also this is the button i want to make the object come back to its original position:
nextImg.addEventListener('click', function() {img.setAttribute("src", "img"+(++imgnum)+".jpg");}, false);
jQueryUI provides draggable() method to make any DOM element draggable. Once the element is draggable, you can move that element by clicking on it with the mouse and dragging it anywhere within the viewport.
Limit draggable area using 'containment' option It is simple. All you have to do is, add an option called containment to the draggable() method. The containment option has a value parent.
This can be done by clicking on the draggable object by mouse and drop it on the specified target. The value of this option specifies that which draggable objects can be dropped on the specified target. The default value of this option is *.
How about this?
<script type="text/javascript">
jQuery(document).ready(function() {
$("#draggable").data("Left", $("#draggable").position().left)
.data("Top", $("#draggable").position().top);
$("#draggable").draggable();
$("#nextImg").bind('click', function() {
$("#draggable").animate(
{ "left": $("#draggable").data("Left"),
"top": $("#draggable").data("Top")
}, "slow");
$("#draggable").attr("src", "img"+(++imgnum)+".jpg");
});
});
</script>
I've made some assumptions, such as the nextImg object having an id of "nextImg" so that I can bind the click event using jQuery (one less step in the javascript event binding process?) I'm also assuming that you don't really want to destroy the drag functionality.
Revert executes when dragging has stopped (with optional delay), not on click.
$(document).ready(function() {
var originalTop = $('#draggable').position().top;
var originalLeft = $('#draggable').position().left;
$('#Zoom').toggle(
function() {
$("#draggable").draggable({});},
function() {
$("#draggable").draggable('destroy');
$('#draggable').position().top = originalTop;
$('#draggable').position().left= originalLeft;
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With