0

在 jQuery 终端中,我有绑定鼠标向下/向上/移动的代码,以检测用户是否使用单击而不是拖动。

代码如下所示:

            (function() {
                var count = 0;
                var isDragging = false;
                var $target;
                var name = 'click_' + self.id();
                self.mousedown(function(e) {
                    console.log('down');
                    isDragging = false;
                    $target = $(e.target);
                    if (e.originalEvent.button === 2) {
                        return;
                    }
                    self.oneTime(1, function() {
                        $(window).one('mousemove.terminal_' + self.id(), function() {
                            console.log('move');
                            isDragging = true;
                            count = 0;
                        });
                    });
                }).mouseup(function() {
                    console.log('up');
                    var wasDragging = isDragging;
                    isDragging = false;
                    $(window).off('mousemove.terminal_' + self.id());
                    console.log(wasDragging);
                    if (!wasDragging) {
                        if (++count === 1) {
                            if (!self.enabled() && !frozen) {
                                self.focus();
                                command_line.enable();
                                count = 0;
                            } else {
                                self.oneTime(settings.clickTimeout, name, function() {
                                    if ($target.is('.terminal') ||
                                        $target.is('.terminal-wrapper')) {
                                        var len = self.get_command().length;
                                        self.set_position(len);
                                    } else if ($target.closest('.prompt').length) {
                                        self.set_position(0);
                                    }
                                    count = 0;
                                });
                            }
                        } else {
                            self.stopTime(name);
                            count = 0;
                        }
                    }
                }).dblclick(function() {
                    count = 0;
                    self.stopTime(name);
                });
            })();

(代码也处理双击,不记得为什么)。

但是在 Chrome/MacOS 上存在问题(我正在 VirtualBox 中对其进行测试,但也有人报告了这个问题)即使没有鼠标移动也没有拖动,mousemove 也会被触发。

在鼠标实际移动后绑定时,似乎会触发 mousemove 事件(在 MacOS/Chrome 上)。

是否有更好的解决方案来检测在 MacOS 上以及在其他 OS/浏览器上工作的点击而不是拖动?

4

1 回答 1

0

我想我已经通过使用此跨浏览器功能将 mousemove 替换为检查是否选择了任何文本来解决问题

var get_selected_text = (function() {
    if (window.getSelection || document.getSelection) {
        return function() {
            var selection = (window.getSelection || document.getSelection)();
            if (selection.text) {
                return selection.text;
            } else {
                return selection.toString();
            }
        };
    } else if (document.selection && document.selection.type != "Control") {
        return function() {
            return document.selection.createRange().text;
        };
    }
    return function() {
        return '';
    };
})();
于 2017-09-04T07:45:49.410 回答