javascript - 多次单击元素后未触发鼠标事件

标签 javascript google-chrome events coffeescript eventtrigger

我正在尝试检测元素上的开始/放下拖动事件。有时这些事件似乎不会被触发。来源在 JSFiddle 。尝试在橙色栏中按住鼠标并拖动以创建间隔(蓝色)。在 Chrome 中,尝试执行此操作 3 次。第三次,我几乎总是注意到,没有触发 mouseup 事件,这使得效果很奇怪。即使没有按下鼠标,拖动事件仍然会被触发

Video on Screenr

$ ->
    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 中工作

最佳答案

问题是您需要使用 user-select 属性并将其设置为 none,这样 div 就无法被选中并且鼠标操作正常事件(这是通常对复杂的 UI 元素执行的操作)。

http://www.w3.org/TR/2000/WD-css3-userint-20000216#user-select :

none: None of the element's content can be selected. This is a very important value of user-select for user interface elements in particular. This value is one of the aspects of how a button behaves. The user cannot select any of the content inside the button. If for example, a user uses a pointing device to click on an element with user-select: none, what happens when the pointing device button is "down" is addressed by the user-input property, whereas when that pointing device button is "released", this property ensures that no selection of the contents of the element may remain. Another way to explain this is that user-select: none is what gives a button its "push-button-springy" feel. The value of 'none' is also useful for static text labels in a user interface which are not meant to be selected. For example, in the header of an email message window, the portion that says "Name:" cannot be selected, whereas the content following it can be. Such a static text label would also have user-input: none.

我看到这个错误是因为 not-allowed 游标:not-allowed cursor

这是修正后的 JS fiddle :http://jsfiddle.net/r8Phv/7/

User-select 尚未在浏览器中实现,但 vendor 前缀适用于 Firefox2+、Chrome 6+、Opera 15 和 IE 10。

以下是用户选择的支持表:http://caniuse.com/user-select-none

关于javascript - 多次单击元素后未触发鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17587653/

相关文章:

javascript - 在行分组级别 ag-grid 启用单元格字段的编辑

javascript - jQuery关闭菜单自动关闭

asp.net-mvc - ASP.NET MVC 4.0 Chrome 缓存

javascript - 点击目标

javascript - 如何在用户释放范围 slider 时触发 "change"事件,即使值未更改

angular - 如何在 Angular 2 中将事件从深层嵌套的子级传递给父级?

javascript - 刷新时重定向到不同的 URL

c++ - 如何使用已安装的 firefox 或 chrome 在 C++ 应用程序中呈现 html

css - 为什么当设置盒子内部阴影时,chrome 会忽略边框半径?

javascript - 在 window.onerror 中捕获错误?