I have a container with plates (grass, water, etc.), and in some of them I have objects (border sprites, trees and general objects). Due to the nature of sprites, all elements are 64x64, and tiles are only 32x32.
My problem is that I want to trigger a hover event on tiles, but sometimes they are obscured by elements of other elements.
The image below shows the problem. A thick green area is a tile in which it really gets stuck when I want to put a tile on a tree.
problem http://upload.cip.nu/pfile.php?file_id=2327
CSS:
#map_canvas .tile{ height: 32px; position: absolute; width: 32px; } #map_canvas .tile .item{ background-position: bottom right; background-repeat: no-repeat; bottom: 0; display: inline-block; height: 64px; margin: 0; padding: 0; position: absolute; right: 0; }
HTML simplified:
<div class="tile" style="background-image: url(grass.png);"></div> <div class="tile" style="background-image: url(grass.png);"></div> <div class="tile" style="background-image: url(grass.png);"> <div class="item" style="background-image(top-left-border.png);"></div> </div>
Here's a live demo http://sleavely.com/tiles/
I donβt even know how to formulate this question, but here goes:
Is it possible to trigger an event only for the parent element (.tile), so that the overflowing child elements (.item) do not obfuscate which plate I really pointed?
EDIT: thanks to @Brilliand I implemented the following
function figureOutTile(e){ var $source = jQuery(e.srcElement); if($source.hasClass("item")){ var $parent = $source.parent(); var posx = $parent.attr("col"); var posy = $parent.attr("row"); if(e.offsetY <= 32){ if(e.offsetX <= 32){ return jQuery(".tile[col="+ (posx-1) +"][row="+ (posy-1) +"]"); }else{ return jQuery(".tile[col="+ posx +"][row="+ (posy-1) +"]"); } }else{ if(e.offsetX <= 32){ return jQuery(".tile[col="+ (posx-1) +"][row="+ posy +"]"); }else{ return $parent; } } }else{ return $source; } } jQuery("#map_viewport").on({ mouseenter: function(e) { var $target = figureOutTile(e); $target.addClass('hovered'); }, mouseleave: function() { jQuery(".tile.hovered").removeClass('hovered'); } }, '.tile');β
jquery css
Sleavely
source share