I tried to disable draggable to the item when dropped on a div. So once draggable item is dropped on droppable div, it will be unable to be dragged anywhere else.
This is my code so far:
$(".div1").droppable({
drop: function (event, ui) {
ui.draggable({disabled: true});
});
I tried disabled: true but this didn't work. Can anyone help me out here?
you can add the class name 'disabled' then in the property of your class set a none.
.disabled{
pointer-events: none;
}
Related
I'm writing an app that let's you drag elements from a column on the right, and drop them on a column on the left, and once you drop them the dragged element (a clone of it) is appended to the element where it's dropped. You can also drag and drop a class that floats the element (basically you can float the elements).
The problem is that when you drop two elements on the same container, that is: you make two siblings, and then float one of them, every new element you add to the floated element is also added to the overlapping sibling.
Check this fiddle: https://jsfiddle.net/5265cg7a/11/
$(function() {
function droppingFunc(event, ui) {
if (ui.helper.hasClass("add-elem")) {
event.stopPropagation();
$(event.target)
.append($(ui.draggable.clone().addClass("testing someHeight someMargin clearFix"))
.clone()
.html("")
.droppable({
drop: droppingFunc,
greedy: true,
tolerance: "pointer"
})
);
} else if (ui.helper.hasClass("add-class")) {
var classToAdd = ui.helper.attr("title");
$(this)
.addClass(classToAdd);
}
}
$(".drag-elem").draggable({
helper: "clone"
});
$(".drop-elem").droppable({
drop: droppingFunc,
greedy: true,
tolerance: "pointer"
});
});
Do the follwing and you´ll se the bug:
Add two siblings by dragging two times the "Add an element" button from the right column and dropping it on the left area.
Float one of them by dragging the "Float an element" button and dropping it in one of the previously created elements.
Add a new child element to the floated sibling. You'll see that a new child element is simultaneously added to the overlapping sibling.
Obviously what i want is that the dropped element only gets added to the element where its being dropped and not to any siblings.
I look forward to all your answers and i thank you in advance for all the help!!
I was tried to drag the div (class = scale_roll) tag by dragging the another div (class =scale_cap).
When the div scale_cap is dragging it's related scale_roll div tag also move.
But my problem is scale_cap dragging cann't stop when the scale_roll div tag was reached 100%.
I want to don't drag the div tag after the visible area. or i want to drag only the 100%.
Js Fiddle
First of all you fully read the Draggable. jQuery have the option for add the handle to the div.
So you don't work hard for add the handle to the drggable element.
create the handle <div> inside the draggable div. Then add and use this
$( ".scale_roll" ).draggable({
axis: "x",
containment: 'parent',
handle:'.scale_cap',
});
Working fiddle
I'm trying to create drag and drop activity where left side have some images and we have to drop that images into right inside the targeted div. can somebody help me or guide me on this i am new in html5 and jquery?
also if the draggable item is not match with that particular targeted div then it comes back to its original place
logic is like that
img drag
{
if(img == targeted div){
img place inside div
}
else{
it reverts to its original position
}
and visa versaaa
You need jquery UI draggable and droppable for this. You can refer them here: http://api.jqueryui.com/droppable & http://api.jqueryui.com/draggable .
I created a fiddle for it. So the images having class "yes" will only be dropped in the div, rest of them will not be dropped.
Here is the sample code:
$('.drag img').draggable({
revert: 'invalid',
});
$('.drop div').droppable({
accept: '.yes'
});
Jsfiddle link: http://jsfiddle.net/p2taQ/
I have a small problem which i can't seem to solve myself.
Look at this fiddle:JSfiddle
This is a basic example of the problem I have.
I have a large div which is a droppable area. Inside this droppable area are multiple other droppable areas.
The inner droppable area should walk trough its code when the element is dropped. Instead the code from the outer div seems to run.
Am i doing something wrong? The area around the divs should stay this way because elements can be placed here (not officially dropped).
I hope my question is clear enough, but I think the fiddler speaks for itself.
P.S. - resizing in this example isn't functioning but is functioning in my development environment.
Rusty and Mark,
Thank you for your replies.
I'm sorry for the confusing resizer. I just removed that from the code.
New Fiddler
Just to clarify things. The box div is a container which has multiple images in it. I am trying to achieve the following:
http://postimage.org/image/qwhtik04f/
The grey dotted boxes are the dropbox2 div from my example.
The space around those drop boxes are dropbox div.
The space with the board is the only place where images may be dropped without anything happening.
The dragged images can snap back to the dropbox2 divs.
If the images are dragged onto the dropbox div, the images should revert.
Setting the greedy: true option on the inner droppable will prevent the event from happening on the outer droppable:
jQuery('#dropbox2').droppable({
greedy: true,
drop: function(event, ui) {
// ...
}
});
Your code has this for the outer <div>:
$("#dropbox").droppable({
drop: function(event, ui) {
ui.draggable.draggable( 'option', 'revert', true );
}
});
This says to set the revert option to true when you drag into the outer <div>. However, when you drop in the smaller <div>, the option is still set to true. All you need to do is change the revert value on your draggable element after a successful drop in your inner <div>:
$("#dropbox2").droppable({
drop: function(event, ui) {
ui.draggable.position( { of: $(this), my: 'center', at: 'center' } );
// Add this line
ui.draggable.draggable( 'option', 'revert', false );
}
});
Update:
Mark pointed out that my solution doesn't stop the propagation of the event to the parent container. As his answer shows, you need to add greedy: true in your initial options. The jQuery documentation says:
If true, will prevent event propagation on nested droppables.
That sounds like what you're looking for. You still need to change the revert property on your draggable, since greedy is only set on your droppables and won't affect your draggable reactions.
i want to change the background of the draggable object after being dropped in the droppable division. how can i pass the current object being dropped?
assume there is #drag div and #drop div. after moving #drag into #drop div. I want to change the color of #drag div to black.
$("#drop").droppable({
drop: function(){
$(this).css('background','black');
},
});
i have no idea how to pass the dragged element into the droppable object after dragging and dropping it into the #drop div. I assumed 'this' would work. but it seems that it points to the #drop div
Try this:
...
drop: function(event, ui) {
// here ui.draggable is the object that was dragged
ui.draggable.css('background','black');
}
...