I have a triangle with a javascript function that moves that image.
The issue is that the element has a square shape so click and hover state are triggered ouside the triangle (see the red part in following image) :
How can I prevent hover and click outside the triangle shape and allow click/hover state only inside the triangle shape?
To prevent hover and click outside the CSS triangle you can use transforms to make the the triangle.
This technique is described here : CSS Triangles with transform rotate
The point is to use a wrapper with hidden overflow and rotate the clickable/hoverable element so that it actualy has a triangular shape and prevent the click event or hover state outside the triangle.
Demo: Click and hover on a CSS triangle
Another approach would be to use an SVG with a clickable triangle :
You should be able to just use an image map. Just create one poly that covers the triangle by setting the coords to
(w/2, 0), (w, h), (0, h)
wherew
andh
are width and height. (Assuming an equilateral triangle like in your example. Otherwise just find the points with an image editor.)Demo: http://jsfiddle.net/QXv2c/
This should work: (based on Brian Nickel's answer to another question)
Working example: http://jsfiddle.net/FCf9d/
This solution assumes there are no transparent pixels inside the triangle.
This example uses the jsfiddle logo instead of a triangle, because you can't use remote images. The jsfiddle-logo works because it's on the jsFiddle-domain but any other random image doesn't work.
But that shouldn't be a problem once you implement the code on your own website.
I took the liberty of adding the move-function to the fiddle for you so you could see everything in action. If you like my script and would like to add boundaries to the move area, check out another fiddle of mine which has those build in: http://jsfiddle.net/SN8Ys/
Taken from CSS-tricks. This is a neat solution however I'm not sure about the clicking part. Maybe you can use it to mask over what you have.
HTML:
CSS:
Source: http://css-tricks.com/snippets/css/css-triangle/