I'm dragging a div using drag API but e.originalEvent.pageX
and e.originalEvent.pageY
always return 0.
.container
div can only be dragged using the red square, that's why it can't have draggable attribute from the beginning.
This is my code:
Html:
<div class="container">
<div class="dragger"></div>
</div>
<label></label>
JS:
$(function(){
$('.container').on('drag', function(e){
$('label').text("pageX:" + e.originalEvent.pageX + " pageY:" + e.originalEvent.pageY);
console.log("pageX:" + e.originalEvent.pageX + " pageY:" + e.originalEvent.pageY);
}).on('dragstart', function(e){
e.originalEvent.dataTransfer.setData('text', 'junk');
});
$('.dragger').on('mouseover', function(){
$(this).parent().attr('draggable', 'true');
}).on('mouseout', function(){
$(this).parent().removeAttr('draggable');
});
});
CSS
.container {
position:relative;
background: #ccc;
width:200px;
height:200px;
}
.dragger {
position:absolute;
top:-8px;
right:-8px;
background: red;
width:16px;
height:16px;
}
And this is the fiddle http://jsfiddle.net/gypLrxvq/
EDIT
I've found a workaround to get that coordinates:
var pageX = 0;
var pageY = 0;
$(document).on("dragover",function(e) {
pageX = e.originalEvent.pageX;
pageY = e.originalEvent.pageY;
});