也许你可以使用四叉树之类的方式,将页面分割以便加快循环速度?
退一步,认识到问题非常小,努力越大,使用的答案就会越复杂。
现在你需要做的是为高亮创建4个元素。它们将形成一个空方块,因此鼠标事件可以自由触发。这类似于我制作的overlay example。
不同之处在于,你只需要四个元素(没有调整大小标记),而且4个框的大小和位置略有不同(模仿红色边框)。然后你可以在事件处理程序中使用event.target
,因为默认情况下它获取真正的最顶层元素。
另一种方法是隐藏额外元素,获取elementFromPoint
,计算然后放回去。
它们比光还快,我可以告诉你。甚至爱因斯坦也会同意的 :)
1.) elementFromPoint overlay/borders - [Demo1] FF 需要 v3.0+
var box = $("<div class='outer' />").css({
display: "none", position: "absolute",
zIndex: 65000, background:"rgba(255, 0, 0, .3)"
}).appendTo("body");
var mouseX, mouseY, target, lastTarget;
window.requestAnimationFrame(function frame() {
window.requestAnimationFrame(frame);
if (target && target.className === "outer") {
box.hide();
target = document.elementFromPoint(mouseX, mouseY);
}
box.show();
if (target === lastTarget) return;
lastTarget = target;
var $target = $(target);
var offset = $target.offset();
box.css({
width: $target.outerWidth() - 1,
height: $target.outerHeight() - 1,
left: offset.left,
top: offset.top
});
});
$("body").mousemove(function (e) {
mouseX = e.clientX;
mouseY = e.clientY;
target = e.target;
});
2.) mouseover边框 - [演示2]
var box = new Overlay();
$("body").mouseover(function(e){
var el = $(e.target);
var offset = el.offset();
box.render(el.outerWidth(), el.outerHeight(), offset.left, offset.top);
});
function Overlay(width, height, left, top) {
this.width = this.height = this.left = this.top = 0;
var outer = $("<div class='outer' />").appendTo("body");
var topbox = $("<div />").css("height", 1).appendTo(outer);
var bottombox = $("<div />").css("height", 1).appendTo(outer);
var leftbox = $("<div />").css("width", 1).appendTo(outer);
var rightbox = $("<div />").css("width", 1).appendTo(outer);
outer.mouseover(function(){
outer.hide();
});
this.resize = function resize(width, height, left, top) {
if (width != null)
this.width = width;
if (height != null)
this.height = height;
if (left != null)
this.left = left;
if (top != null)
this.top = top;
};
this.show = function show() {
outer.show();
};
this.hide = function hide() {
outer.hide();
};
this.render = function render(width, height, left, top) {
this.resize(width, height, left, top);
topbox.css({
top: this.top,
left: this.left,
width: this.width
});
bottombox.css({
top: this.top + this.height - 1,
left: this.left,
width: this.width
});
leftbox.css({
top: this.top,
left: this.left,
height: this.height
});
rightbox.css({
top: this.top,
left: this.left + this.width - 1,
height: this.height
});
this.show();
};
}
e.currentTarget
иҖҢдёҚз”ЁеҫӘзҺҜжҹҘжүҫе…ғзҙ еҗ—пјҹ - GuffacurrentTarget
将始终是$div
,因为我正在将其移动到要突出显示的元素上方,但然后它会夺取所有鼠标事件。 - mpen