最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tracking the mouse relative to starting drag position HTML5, Ember - Stack Overflow

programmeradmin1浏览0评论

Starting off of this fiddle I'd like to output the coordinates of the draggable object, as it's dragged, relative to its starting position, which is always {0,0}. I know I can log the beginning and end drag events like:

DragNDrop.Draggable = Ember.Mixin.create({
    attributeBindings: 'draggable',
    draggable: 'true',
    dragStart: function(event) {
        var dataTransfer = event.originalEvent.dataTransfer;
        dataTransfer.setData('Text', this.get('elementId'));
        console.log('begin coordinates: (0, 0)');
    },
    dragEnd: function(event) {
        console.log('end coordinates');  
    }
});

So I have two problems:

1.How do I get the mouse coordinates?

2.How do I continue to output the mouse coordinates until the dragEnd function is called?

Edit:

For 1. I can now get the mouse coordinates using:

DragNDrop.Draggable = Ember.Mixin.create({
    attributeBindings: 'draggable',
    draggable: 'true',
    dragStart: function(event) {
        var dataTransfer = event.originalEvent.dataTransfer;
        dataTransfer.setData('Text', this.get('elementId'));
        console.log('begin coordinates: ', event.originalEvent.pageX, event.originalEvent.pageY);
    },
    dragEnd: function(event) {
        console.log('end coordinates: ', event.originalEvent.pageX, event.originalEvent.pageY);  
    }
});

But I still am not sure how to continually output the current mouse coordinates, rather than only one when the drag starts and once when the drag ends.

Starting off of this fiddle I'd like to output the coordinates of the draggable object, as it's dragged, relative to its starting position, which is always {0,0}. I know I can log the beginning and end drag events like:

DragNDrop.Draggable = Ember.Mixin.create({
    attributeBindings: 'draggable',
    draggable: 'true',
    dragStart: function(event) {
        var dataTransfer = event.originalEvent.dataTransfer;
        dataTransfer.setData('Text', this.get('elementId'));
        console.log('begin coordinates: (0, 0)');
    },
    dragEnd: function(event) {
        console.log('end coordinates');  
    }
});

So I have two problems:

1.How do I get the mouse coordinates?

2.How do I continue to output the mouse coordinates until the dragEnd function is called?

Edit:

For 1. I can now get the mouse coordinates using:

DragNDrop.Draggable = Ember.Mixin.create({
    attributeBindings: 'draggable',
    draggable: 'true',
    dragStart: function(event) {
        var dataTransfer = event.originalEvent.dataTransfer;
        dataTransfer.setData('Text', this.get('elementId'));
        console.log('begin coordinates: ', event.originalEvent.pageX, event.originalEvent.pageY);
    },
    dragEnd: function(event) {
        console.log('end coordinates: ', event.originalEvent.pageX, event.originalEvent.pageY);  
    }
});

But I still am not sure how to continually output the current mouse coordinates, rather than only one when the drag starts and once when the drag ends.

Share Improve this question edited Oct 17, 2013 at 20:04 JuJoDi asked Oct 17, 2013 at 19:49 JuJoDiJuJoDi 15k23 gold badges84 silver badges128 bronze badges 2
  • 1 event.pageX and evet.pageY will give the mouse position. drag event will continue to output the mouse coordinates – Harsha Venkataramu Commented Oct 17, 2013 at 19:51
  • When I do console.log('begin coordinates: ', event.pageX, event.pageY); I get "begin coordinates: undefined, undefined" – JuJoDi Commented Oct 17, 2013 at 19:56
Add a ment  | 

2 Answers 2

Reset to default 5

Try this

DragNDrop.Draggable = Ember.Mixin.create({
    attributeBindings: 'draggable',
    draggable: 'true',
    dragStart: function(event) {
        var dataTransfer = event.originalEvent.dataTransfer;
        dataTransfer.setData('Text', this.get('elementId'));
        console.log('begin coordinates: ', event.originalEvent.pageX, event.originalEvent.pageY);
        },
        // track the drag
        drag: function(event) {
            console.log('tracking coordinates: ', event.originalEvent.pageX,  event.originalEvent.pageY); 
        },
        dragEnd: function(event) {
            console.log('end coordinates: ', event.originalEvent.pageX, event.originalEvent.pageY);  
        }
});

This code shows the coordinates of the mouse on drag

HTML

<div id= 'draggable'></div>
<input type = 'text' value = 'test' id='myInput'/>

JQUERY

$(document).ready(function(e){
    $('#draggable').draggable();
    $('#draggable').on('drag' , function(event){
        $('#myInput').val(event.pageX + ',' + event.pageY);
    })
})

CSS

#draggable
{
    background-color:#EEE;
    width:200px;
    height:200px;
}

JSFIDDLE LINK

发布评论

评论列表(0)

  1. 暂无评论