我正在尝试检测元素上的开始/拖放事件。有时这些事件似乎没有被触发。源代码在JSFiddle上。尝试在橙色条中按住鼠标并拖动以创建间隔(蓝色)。在 Chrome 中,尝试执行此操作 3 次。第三次,我几乎总是注意到 mouseup 事件没有被触发,这使得效果很奇怪。即使没有按下鼠标,拖拽事件仍然会触发
$ ->
isMouseDown = false
isDragging = false
$draggedInterval = undefined
$test = $("#test")
$test
# code related to attaching handlers to trigger custom events
.on "mousedown", ->
isMouseDown = true
.on "mouseup", (evt) ->
isMouseDown = false
if isDragging
$test.trigger("x-stopDrag", evt)
isDragging = false
else
$test.trigger("x-click", evt)
.on "mousemove", (evt) ->
console.log isMouseDown, isDragging
if isMouseDown && not isDragging
$test.trigger("x-startDrag", evt)
isDragging = true
if isDragging
$test.trigger("x-drag", evt)
.on "mouseleave", (evt) ->
isMouseDown = false
if isDragging
isDragging = false
$test.trigger("x-cancelDrag", evt)
# handlers for custom events
.on "x-startDrag", (x, evt) ->
console.log("started dragging")
$draggedInterval = $("<div>", {
class: "interval"
css:
left: evt.clientX
}).appendTo($test)
.on "x-stopDrag", (x, evt) ->
console.log("stopped dragging")
$draggedInterval = undefined
.on "x-cancelDrag", ->
console.log("canceled dragging")
$draggedInterval.remove()
$draggedInterval = undefined
.on "x-click", (x, evt) ->
console.log("click")
.on "x-drag", (x, evt) ->
console.log("drag")
$draggedInterval.css("width", evt.clientX - $draggedInterval.position().left)
似乎可以在 Firefox 和 IE10 中使用