Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Size of Sortable Element on drag JQuery UI

Here's a small fiddle for my issue:

http://jsfiddle.net/Yc9WY/52/

Essentially, one box of items is much larger than the other. So, dragging and dropping with sortable is a challenge.

First, I changed the size of the element on start

$(function() {
    $( "#sortable1, #sortable2" ).sortable({
       connectWith: ".connectedSortable",
       start: function(event, ui){
           $(ui.item).width($('#sortable2 li').width());
       }               
    }).disableSelection();
});​

But, I wasn't sure how to move the resized element to the cursor of the mouse. Basically, if I click any distance away from the left handside of an element in the left list, it becomes hard to drop the element onto the smaller list.

I was trying to move the element to the mouse position using:

$(ui.item).offset({top: event.pageY, left: event.pageX});

But, it didn't seem to work.

Further clarification:

(List 1) (List 2) ----------------------------------------- ---

Moving element from list 1 to list 2 is challenging because of their different sizes. So, I resized elements in list 1 to list 2 size. Now, if you click

-------------------------------------x--- ---

The element looks like

---_____________________x__ ---

where x indicated the mouse position. Essentially, I want to move the element to the mouse position to make the dragging to list 2 easier

Are there any suggestions? Thanks!

Edit: Sorry, I don't have enough rep to post an image, but...

[____________________] (before click)

[__]-------------------- x (click with x marking the location of the click)

I essentially want to move the now smaller element to the same position as the mouse.

Thanks again!

like image 460
user1464055 Avatar asked Jan 16 '23 09:01

user1464055


2 Answers

Add tolerance:"pointer", in the option of the sortable element

here'S the fiddle : http://jsfiddle.net/Yc9WY/55/

EDIT :

ADD cursorAt: { top:0, left: 0 }, in the option of the sortable element

It will position the item under your pointer

here'S the fiddle : http://jsfiddle.net/Yc9WY/57/

like image 163
GregM Avatar answered Jan 18 '23 23:01

GregM


I found this solution that works perfectly:

$("ul.sortable").sortable({
    placeholder: "highlight",
    start: function(event, ui) {
       // Resize elements
       $(this).sortable('refreshPositions');
    }
});

Source: http://forum.jquery.com/topic/reducing-the-size-of-elements-before-sorting-them-using-sortable

like image 20
German Rojas Avatar answered Jan 18 '23 22:01

German Rojas