【发布时间】:2015-08-28 20:05:40
【问题描述】:
Hitbox Overlay IIFE 代码
//CSS Hitbox Solution 08-26-2015
//StackOverflow - https://stackoverflow.com/questions/32233084/show-an-element-without-hitbox-does-not-take-mouse-touch-input
//Detect MouseOver https://stackoverflow.com/questions/1273566/how-do-i-check-if-the-mouse-is-over-an-element-in-jquery
//Source: https://stackoverflow.com/questions/3942776/using-jquery-to-find-an-element-at-a-particular-position
//https://css-tricks.com/snippets/jquery/get-x-y-mouse-coordinates/
(function($) {
$.mlp = {
x: 0,
y: 0
}; // Mouse Last Position
function documentHandler() {
var $current = this === document ? $(this) : $(this).contents();
$current.mousemove(function(e) {
jQuery.mlp = {
x: e.pageX,
y: e.pageY
};
});
$current.find("iframe").load(documentHandler);
}
$(documentHandler);
$.fn.ismouseover = function(overThis) {
var result = false;
this.eq(0).each(function() {
var $current = $(this).is("iframe") ? $(this).contents().find("body") : $(this);
var offset = $current.offset();
result = offset.left <= $.mlp.x && offset.left + $current.outerWidth() > $.mlp.x && offset.top <= $.mlp.y && offset.top + $current.outerHeight() > $.mlp.y;
});
return result;
};
})(jQuery);
$('.notification-box').on("click", function() {
$("button").each(function(i) {
var iteratedButton = $('button:eq(' + i + ')');
var buttonID = iteratedButton.attr("id");
if (iteratedButton.ismouseover()) {
iteratedButton.toggleClass(buttonID);
}
});
});
Example 01: Overlay Example for context
Example 02: Concept for auto generating content - 源自this stackoverflow 问题。
有一种方法可以在覆盖它们的覆盖层下放置多个对象。然后,如果用户在预定点单击,则有一种方法可以使指针与所述覆盖层下方的元素交互。我的问题是,请有人编写代码,将<map> 标签的概念与检测用户单击的参考点是否是该图像的 IIFE 结合起来,然后就好像它被单击了一样。
如果这没有意义,简单地说,我正在寻找一种偏离手动设置<area> 坐标或必须使用工具(非常深刻)如http://www.image-maps.com/ 的过程。相反,我们会让指针完成所有工作。
我们有以下高实用性 + 高度兼容的方法:.offset()、.position()、elementFromPoint() 以及使用基本 CSS 将元素置于掩码后面的能力。
所以我们可以结合上面的 IIFE Overlay hitbox 方法 + ???? = 利润(再见映射坐标,通过<map>)。
我只是不知道 ????是。我确实知道无论解决方案是什么,我都希望它适用于所有浏览器(包括 IE 5)。
最后,该过程在设计、设置和实施方面应该是相当自动化的。
无论是谁创建的,请将其命名为 autoMapperJs(因为它不仅限于图像)。
更新:
???? 的核心功能组件正如@Alex 在 cmets 中所指出的那样,已经实现了。 CreateJs 会在指针悬停在图像的非透明区域上时发出通知。这是强大的,应该是创建的工具的标准。它似乎也使用了.mousemove() 和z-index。请继续发表评论,总的来说,我觉得可以找到解决方案。
【问题讨论】:
-
(包括 IE 5)。 :-o
-
IE5 兼容性意味着它非常难.. :(
-
IE 5 或许可以在以后构建。至少要实现这个概念,这样我们就有了一个可以构建和改进的工作模板。
-
createjs.com/easeljs 有一个不错的功能。您可以将图像放在画布上,当您实际将鼠标悬停在非透明像素上时,指针会发出通知,如下所示:createjs.com/demos/easeljs/draganddrop。我非常怀疑 IE 5 甚至是一个互联网浏览器。
-
您可以考虑使用 SVG 而不是 Canvas 以获得更好的元素基础操作和事件处理。顺便说一句,SVG 和 Canvas 在 IE8 及更低版本中都没有原生支持。
标签: javascript jquery html css imagemap