2013-03-07 71 views
0

我有一段文字,當我長按鼠標按鍵(700毫秒)時,我將激活文本編輯器。在此期間(按下鼠標時),我必須檢查鼠標位置是否已移動。問題是,我只有一個事件,鼠標按下事件。查找鼠標在mousedown期間是否發生了變化

如何知道鼠標是否已移動? 我試圖採取新的事件,但我是jquery的初學者,所以我無法實現我想要的。

這是我得到事件的功能。

onTaskItemMouseDown: function (event) { 

     // We only check the left click 
     if (event.button !== 0) { return true; } 

     var that = this, 
      initialX = event.pageX, 
      initialY = event.pageY; 

     // Set timeout 
     console.log("x=" + initialX); 
     console.log("y=" + initialY); 
     this.pressTimer = window.setTimeout(function() { 


      clearTimeout(that.pressTimer); 
      that.pressTimer = 0; 
      that.onEditTask(event, that.$(event.currentTarget).closest(".task").find(".dropdown-container").data("task-id")); 
     }, MindomoUtils.longClickDuration); 

     return true; 
    }, 
+1

http://api.jquery.com/mousemove/ – Blazemonger 2013-03-07 14:06:22

+0

我看了,但我沒明白。你能幫我一點嗎?我告訴過你,我是這個初學者的 – 2013-03-07 14:07:05

+0

首先複製那個頁面上的例子,並與他們一起修改,看看會發生什麼。 – Blazemonger 2013-03-07 14:08:05

回答

1

您可能正在尋找mousemove事件。

我可以看到你已經使用jQuery,所以這裏是你的一個例子。

HTML輸出

<ul class="output"></ul> 

jQuery的

$(document).on('mousedown', onMouseDown) 
$(document).on('mousemove', onMouseMove) 
$(document).on('mouseup', onMouseUp) 

var mouseIsDown = false 

function onMouseDown(event) { 
    // set boolean true 
    mouseIsDown = true 
    $('.output').append($('<li>').text('Mouse down - x: ' + event.pageX + ' y: ' + event.pageY)) 
} 

function onMouseMove(event) { 
    if(mouseIsDown) { 
     $('.output').append($('<li>').text('Mouse moving - x: ' + event.pageX + ' y: ' + event.pageY)) 
    } 
} 

function onMouseUp(event) { 
    // set boolean false again 
    mouseIsDown = false 
    $('.output').append($('<li>').text('Mouse up - x: ' + event.pageX + ' y: ' + event.pageY)) 
} 

Here you can play with it yourself.

相關問題