简体   繁体   English

html,svg,css如何使鼠标事件不触发透明颜色

[英]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

Edit 编辑

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上获得一些不错的结果,例如

Image map by alpha channel 按Alpha通道的图像图

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.

相关问题 SVG + CSS-如何包含在HTML中以及比例和颜色? - SVG + CSS - how to include in HTML and scale & color? 如何使用 JavaScript 在 Firefox 中触发鼠标滚轮事件? - How to fire mouse wheel event in Firefox with JavaScript? 如何使用CSS在HTML中设置外部SVG颜色? - How to set a External SVG color in HTML using CSS? 如何在不加动鼠标的情况下加载页面时触发鼠标事件? - How to fire mouse event when page loads without moving the mouse? 如何使内联 svg 在 html/css 中可点击以在 javascript 中使用 onclick? - How to make an inline svg clickable in html/css to use onclick in javascript? 如何将(鼠标)事件添加到SVG的XMLDocument - How to add a (mouse)event to a XMLDocument of a SVG 如何在 JS 或 CSS 中使用 svg 作为鼠标光标? - How use an svg as a mouse cursor in JS or CSS? 如何让SVG元素鼠标事件通过另一个元素冒泡? - How do you make an SVG element mouse event bubble up through another element? 如何使MUI 中的默认颜色主题透明? - How to make the default color theme in MUI transparent? 当我将鼠标放在按钮上时,如何检测按钮的颜色是否因为 CSS 或 JavaScript 事件而改变? - How can I detect if the color of a button changes because CSS or JavaScript event when I put mouse on button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM