Triangle-shaped image mapping and interactive hover effects

I am attempting to create a simple hover effect using two triangles. One triangle should start below the other, and upon hovering, it should appear above the first triangle.

The problem I am encountering is that the surrounding boxes are obstructing the effect. The left triangle, with a temporary z-index above the other, overlaps unnecessarily due to its square perimeter and wasted space.

Hovering over triangle 2 only works in certain areas because of this issue. I have tried using image maps and coordinate mapping to achieve my goal, but I could not maintain a triangular shape while getting the desired functionality. It always defaults to a square.

Is there a way to accomplish this task? I am looking for a method to hover over a specific shape, as CSS/HTML seems limited to squares. Are there any jQuery solutions or alternatives?

Thank you.

Answer №1

If you're not entirely sure what direction to take, I can share a technique I once used to solve a similar issue on a previous project.

To address this, consider placing all images within a shared staging container like a div with a position: relative property to establish the stacking and positioning context for the images. Then, overlay the entire div with a transparent 1x1 GIF or PNG image as the last child in the markup, with dimensions set to cover the whole container. By applying an image map to this blank image, specifically using a "poly" shape area, you can define a free-form point-to-point region for interactive purposes. Link your mouseover/mouseout JavaScript actions to this image map area.

For example (actual CSS positions and map coordinates will need to be tailored to your specific needs):

The HTML

<div class="imagePanel">
    <img src="http://www.mysite.com/images/triangle1.png" alt="" class="triangle1" />
    <img src="http://www.mysite.com/images/triangle2.png" alt="" class="triangle2" />
    <img src="http://www.mysite.com/images/blank.gif" alt="" class="imageMap" usemap="#mapOver"/>
    <map id="mapOver" name="mapOver">
        <area id="areaTriangle" shape="poly" coords="25,240,108,100,190,240" href="#" />
    </map>
</div>

The CSS

div.imagePanel
{
    position: relative;
    width: 300px;
    height: 300px;
}

div.imagePanel > img
{
    position: absolute;
}

img.triangle1
{
    left: 25px;
    top: 100px;
}

img.triangle2
{
    left: 75px;
    top: 75px;
    display: none;
}

img.imageMap
{
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
}

The jQuery

$(function() {
    $("#areaTriangle").hover(function() {
        $("img.triangle2").show();
    }, function() {
        $("img.triangle2").hide();
    });
});

Check out a live demonstration on this fiddle.

Answer №2

If you're looking to achieve a certain effect with an image map, ImageMapster, a jquery image map plugin from Outsharked, could be the solution you need. This plugin allows you to easily create interactive image maps by using a second image for highlight effects and defining specific areas to show after certain events.

For a similar example of what you might be trying to do, check out this demonstration (hover over the middle circle). You could make your desired area invisible until a user hovers over it, just like in this example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with displaying errors in the delete confirmation box within Bootstrap 4

I am working on implementing a Bootstrap 4 modal confirmation before sending a delete request using JSON. Here is the code I have: $(document).ready(function() { $('.launchConfirm').on('click', function (e) { $('#confi ...

What exactly is the significance of "hash" in terms of Internet Explorer style

After downloading a sample CSS file, I noticed the following entry: #text-align: right; The comment beside the entry mentioned that this expression is specifically for justifying it in IE, while text-align: right; works for Safari and Chrome. My questi ...

Attempting to make Navbar vanish as the page size decreases

I am a beginner in html and css and my instructor wants us to create a resume using bootstrap. Bootstrap is designed to provide interactive design that adjusts seamlessly across different devices. I am currently working on aligning elements and I would lik ...

Siblings are equipped with the advanced selector feature to

I'm struggling to understand this setup I have: <div class="container"> for n = 0 to ... <a href="some url">Link n</a> endfor for each link in ".container" <div class="poptip"></div> ...

Javascript textfield button function malfunctioning

Here is the code I have created: HTML: <form method="post" id="myemailform" name="myemailform" action="form-to-email.php"> <div id="form_container"> <label class="descriptio ...

Exploring the Power of JQuery AJAX Requests and Analyzing Google Trends

Currently, as a first-year Computer Science university student, I have taken on the task of creating a website that utilizes Google Trends data. My goal is to use only jQuery / JavaScript for this project. I am attempting to retrieve the JSON data provide ...

Guide to creating a vertical handler that can be resized

Did you know that you can resize tables in http://www.jsfiddle.net? I'm curious about how to resize just the "Vertical Handler". Could someone share the source code with me? If possible, please provide an example on http://www.jsfiddle.net. ...

When the button is clicked, verify the existence of the file, then display a message and undo

I encountered a situation where I needed to verify if a user tries to input data with the same date and same region again, they should receive a prompt alert. I attempted to achieve this using code in the backend, as shown below:- protected void btnSave ...

Tips for aligning a cluster of floating buttons at the center in Vuetify:

This is the code I am currently working with: <v-container height="0"> <v-row align="center" justify="center"> <v-hover v-slot:default="{ hover }" v-for="(option, index) in options" ...

Image showcasing the background

I'm facing an issue with adding a background image to my project Even after trying to add it globally, the background image is not showing up on the Next.js index page. import { Inter } from 'next/font/google' const inter = Inter({ subsets: ...

The importance of CSS style prioritization

I can't seem to figure out CSS declaration priority. I have an external CSS file with a rule and some inline CSS declarations that are supposed to override it. According to my understanding, inline styles should take precedence over external CSS rules ...

What is the best way to assign the active class to a current anchor tag without including an href attribute?

I need help adding an active class to the current <a> tag that doesn't have a href. Below is the list of languages available: <ol> <li> <a data-value="en">English</a> </li> <li> ...

Proper alignment of multiple elements with Bootstrap

I am currently incorporating Bootstrap panels into my project, and I have a design mockup that looks like this: The Profile text and progress bar need to be aligned to the left, while the collapse icon should be aligned to the right. Here is the code sni ...

Ways to align the `<ol>` number at the top vertically

I'm having trouble aligning my <ol> numbers vertically at the top of the list. I've created a sample on CodePen. ol { list-style-type: decimal-leading-zero; font-size: 11px; } a { font-size: 80px; tex ...

The CSS selector seems to be malfunctioning

I am facing an issue with calling HTML elements from CSS using CSS selectors. It's strange that the class selectors work fine, but I'm unable to select the elements directly from CSS. Therefore, I had to assign them classes. What I aim to achiev ...

Ways to stop a background image from repeating?

<div style="background-image: url('https://i.ibb.co/v1B855w/bg15.png'); color: #000000; background-color: #000000; background-attachment: fixed; text-align: center;"><img src="https://i.ibb.co/L6zQY0S/name6.png" /> ...

Navigation buttons that move the user either up or down the page

I am a beginner in programming and I wanted to create two buttons for my webpage. One button should appear when the page is scrolled more than 300px and take the user to the top, while the other button should be displayed immediately and scroll the user to ...

Retrieve data from a database using Ajax and PHP's `.load()` method

I am facing a dilemma with updating a value from a database ($learner->idnumber). The scenario involves a form that posts to process.php upon submission (which then edits the database). The challenge is updating the PHP database value $learner->idn ...

What is the reason for being unable to remove the event listener from a sibling element?

function show1() { console.log("ok1"); document.getElementById("a2").removeEventListener("click", delegate); } function show2() { console.log("ok2"); } function show3() { console.log("ok3"); } function delegate(event) { var flag = event.target ...

Prevent HTML Characters from Rendering in Preformatted Text Tag

After setting up a syntax highlighter, I found out that for it to function properly, the tags need to be indicated as &lt; and &gt;. My task now is to switch all <'s with &lt; and >'s with &gt;, but only within the PRE tag. ...