[英]html, svg, css how to make mouse event not fire on transparent color
Is it possible to make a mouse event not fire on transparent color of an image? 是否可以使鼠标事件不触发图像的透明颜色?
I have a big background image and several small images which are shaped in different shapes (for example the shape of a ball). 我有一个大的背景图像和几个小图像,它们的形状不同(例如,球的形状)。 I want to catch mouse events on these small images. 我想在这些小图像上捕捉鼠标事件。 The problem is that events fire on the whole rect of an image. 问题在于事件在图像的整个区域上触发。 I can use image map on the background image but this is not a good idea because the small images will be changing under mouse (sprites). 我可以在背景图像上使用图像映射,但这不是一个好主意,因为小图像将在鼠标(精灵)下变化。
You can check a number of things, either the fill of an svg shape, path or rect or also the opacity
property depending on how you have made the element transparent. 您可以检查许多事情,包括svg形状,路径或矩形的填充,也可以检查opacity
属性,具体取决于您使元素透明的方式。
For instance: 例如:
$('svg rect').mouseenter(function(){
if($(this).attr('opacity') != 0){
$('body').append('hovered element with colour');
}
});
Here is an example: 这是一个例子:
http://codepen.io/EightArmsHQ/pen/zxLbXO http://codepen.io/EightArmsHQ/pen/zxLbXO
With more complex images like 具有更复杂的图像,例如
http://mobilite.mobi/wp-content/uploads/white-rabbit-wallpapers-9. http://mobilite.mobi/wp-content/uploads/white-rabbit-wallpapers-9。
You have two options. 您有两个选择。 The (to me, a graphic designer turned web designer) the simpler way would be to create an SVG hit map) (对我来说,由图形设计师转变为网页设计师),更简单的方法是创建SVG匹配图)
But a solution that would be far more thorough would be to use HTML5 / JavaScript canvas element which can have all of this stuff included, but will be more complex to initially code. 但是更彻底的解决方案是使用HTML5 / JavaScript canvas元素,该元素可以包含所有这些内容,但是对于初始代码而言将更为复杂。
If you use the words canvas
alpha
hit detection
I'm sure you'll be able to google some great results, such as 如果您使用“ canvas
alpha
hit detection
”一词,我相信您可以在Google上获得一些不错的结果,例如
what you can do is add classes for those small small shapes. 您可以做的是为这些小小的形状添加类。 for example 例如
<div class="big_back" id="big_back">
<div class="small_shapes" id="shape1">
</div>
<div class="small_shapes" id="shape2">
</div>
<div class="small_shapes" id="shape3">
</div>
</div>
and in jquery you can call the method as follow 在jquery中,您可以按以下方式调用方法
$(document).ready(function() {
$(".small_shapes").mouseenter(function() {
alert("I enteres shapes");
});
}); }); If this is not what you are looking let me know. 如果这不是您要查找的内容,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.