This is a migrated thread and some comments may be shown as answers.

Centering draggable to mouse pointer

6 Answers 794 Views
Drag and Drop
This is a migrated thread and some comments may be shown as answers.
Joao
Top achievements
Rank 1
Joao asked on 24 Oct 2011, 09:42 PM
Hello,
How do I center the draggable to the mouse pointer?

6 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 25 Oct 2011, 07:52 AM
Hi Joao,

If you use the following demo as a starting point...

http://demos.kendoui.com/web/dragdrop/index.html

...you need to add negative top and left margin to the draggable element, for example:

$("#draggable").kendoDraggable({
    hint: function() {
        return $("#draggable").clone().css({marginLeft: "-40px", marginTop: "-40px"});
    }
});

40px is half of the draggable element's width and height. You can also calculate this on the fly, if you don't want to hard-code it, or you don't know it beforehand.

All the best,
Dimo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
John Deary
Top achievements
Rank 1
answered on 20 Jan 2012, 07:05 PM
I tried this, but it seems to prevent the drop target from getting the drop event,

I determined that this was because you are dropping onto your hint now, if it is under the mouse.

Thoughts?
0
Raymond
Top achievements
Rank 1
answered on 22 Jan 2012, 11:44 PM
I can confirm that when using negative margins to get the item centered, the droptarget area is not functional anymore.
0
Dimo
Telerik team
answered on 23 Jan 2012, 08:52 AM
Hello,

That's right. After some additional testing, it turned out this scenario cannot be supported, as the required mouse events are not fired.

Regards,
Dimo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Tim
Top achievements
Rank 1
answered on 25 Mar 2012, 06:04 AM
I found success with this...

$('#foo').kendoDraggable({
  cursorOffset: {
    top: -($('#foo').height() / 2),
    left: -($('#foo').width() / 2)
  },     
  ...
})

$('body').kendoDropTarget({
  drop: function (e) {
    top_offset = -($('#foo').height() / 2),
    left_offset = -($('#foo').width() / 2)
    $('#foo').offset({
      top: e.clientY + top_offset,
      left: e.clientX + left_offset,
    })
  }      
})
0
Petyo
Telerik team
answered on 26 Mar 2012, 07:45 AM
Hello,

I just wanted to mention that positioning the hint below the mouse is supported (and is, in fact, the default behavior) since Q1 2012. Check this demo for details.
Kind regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Drag and Drop
Asked by
Joao
Top achievements
Rank 1
Answers by
Dimo
Telerik team
John Deary
Top achievements
Rank 1
Raymond
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Petyo
Telerik team
Share this question
or