我正在尝试跟踪整个屏幕的拖入/离开事件,在Chrome / Safari中一切正常,这要归功于来自https://dev59.com/wWkv5IYBdhLWcg3w71H7#10310815 的draghover插件,如下所示:
$.fn.draghover = function(options) {
return this.each(function() {
var collection = $(),
self = $(this);
self.on('dragenter', function(e) {
if (collection.size() === 0) {
self.trigger('draghoverstart');
}
collection = collection.add(e.target);
});
self.on('dragleave drop', function(e) {
// timeout is needed because Firefox 3.6 fires the dragleave event on
// the previous element before firing dragenter on the next one
setTimeout( function() {
collection = collection.not(e.target);
if (collection.size() === 0) {
self.trigger('draghoverend');
}
}, 1);
});
});
};
function setText(text) {
$('p.target').text(text);
}
$(document).ready(function() {
$(window).draghover().on({
'draghoverstart': function() {
setText('enter');
},
'draghoverend': function() {
setText('leave');
}
});
});
然而,当我拖动文本项时,火狐浏览器仍然给我带来问题,这里有一个演示的小工具:http://jsfiddle.net/tusRy/6/。这是火狐的bug还是可以用JS解决?或者有没有更稳健的方法来执行所有这些操作?
谢谢!更新:更新了小工具http://jsfiddle.net/tusRy/6/以减少一些杂乱。为了解释小工具的预期行为:
- 将文件拖入窗口,p.target应为"ENTER",变成黄色。 - 将文件拖出窗口,p.target应为"LEAVE",变成红色。 - 在窗口中放置文件,p.target应为"LEAVE",变成红色。
在Firefox中,当您将文件拖动到文本上方时,会触发"LEAVE"事件。