When hovering over one item, it causes a hover effect on an item in a SEPARATE container

My goal is to create a unique interaction between links and images, where hovering over a link will transform the corresponding image below it, and vice versa. I have been searching for solutions but have only found methods that work when all items are within the same container. Here is an overview of my current setup:

HTML:

<ul class="links">
  <li><a href="https://...1">LINK 1</a></li> 
  <li><a href="https://...2">LINK 2</a></li>
  <li><a href="https://...3">LINK 3</a></li>
</ul>
<div class="images">
  <a href="https://...1">
    <img src="1.png">
  </a> 
  <a href="https://...2">
    <img  src="2.png">
  </a>
  <a href="https://...3">
    <img class="ssoverview" src="3.png">
  </a>
</div>

CSS:

a:hover {
    cursor: pointer;
}

.images a img {
    width: 33.33%;
    float: left;
    transition: transform .2s;
}

.images a img:hover {
    transition: transform .2s;
    transform: scale(1.4);
}


Answer №1

In my opinion, achieving this solely with CSS may not be possible; however, since you have also included the tag, here is a JavaScript solution:

You can implement this functionality using the mouseover and mouseout events, matching elements based on their href attribute. Here is the code logic explained:

// Remove any existing highlight
function removeHighlight() {
    document.querySelectorAll(".highlight").forEach(element => {
        element.classList.remove("highlight");
    });
}

// Add a highlight to the hovered element (or its parent LI, if available) as well as any
// matching element with the same `href` attribute (or its parent LI, if available)
function handler(event) {
    const link = event.target.closest("a");
    if (link && this.contains(link)) {
        removeHighlight();
        // Note: Using `getAttribute` is crucial here instead of the reflected
        // `href` property to ensure it returns the attribute value, not the fully-resolved link
        document.querySelectorAll(`[href='${link.getAttribute("href")}']`).forEach(element => {
            const highlightedElement = element.parentElement.tagName === "LI" ? element.parentElement : element;
            highlightedElement.classList.add("highlight");
            console.log("Added to " + highlightedElement.tagName);
        });
    }
}

// Implement event delegation to monitor mouseover and mouseout actions on designated containers
const linksContainer = document.querySelector(".links");
linksContainer.addEventListener("mouseover", handler);
linksContainer.addEventListener("mouseout", removeHighlight);
const imagesContainer = document.querySelector(".images");
imagesContainer.addEventListener("mouseover", handler);
imagesContainer.addEventListener("mouseout", removeHighlight);
.highlight {
    border: 1px solid red;
}
<ul class="links">
  <li><a href="https://...1">LINK 1</a></li> 
  <li><a href="https://...2">LINK 2</a></li>
  <li><a href="https://...3">LINK 3</a></li>
</ul>
<div class="images">
  <a href="https://...1">
    <img src="1.png">
  </a> 
  <a href="https://...2">
    <img  src="2.png">
  </a>
  <a href="https://...3">
    <img class="ssoverview" src="3.png">
  </a>
</div>

Answer №2

My expertise lies in the initial part of this code snippet, where I utilize hover to scale the corresponding image when a link is hovered over. This implementation requires the use of jQuery.

$(document).ready(function() {
  $('.links li a').hover(
    function() {
      var setRelation = $(this).data('relation');
      var aImg = $('.images a').find('#' + setRelation);
      aImg.css('z-index', '1');
      aImg.css('transform', 'scale(1.4)');
    },
    function() {
      var setRelation = $(this).data('relation');
      var aImg = $('.images a').find('#' + setRelation);
      aImg.css('z-index', 'unset');
      aImg.css('transform', 'scale(1.0)');
    });
});
a:hover {
  cursor: pointer;
}

.images {
  display: flex;
}

.images a {
  flex: 1;
  position: relative;
}

.images a img {
  width: 100%;
  transition: transform .2s;
  position: absolute;
  top: 0;
  left: 0;
}

.images a img:hover {
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<ul class="links">
  <li><a data-relation="link1" href="https://...1">LINK 1</a></li>
  <li><a data-relation="link2" href="https://...2">LINK 2</a></li>
  <li><a data-relation="link3" href="https://...3">LINK 3</a></li>
</ul>

<div class="images">
  <a href="https://...1">
    <img id="link1" src="https://picsum.photos/id/11/500">
  </a>
  <a href="https://...2">
    <img id="link2" src="https://picsum.photos/id/12/500">
  </a>
  <a href="https://...3">
    <img id="link3" class="ssoverview" src="https://picsum.photos/id/13/500">
  </a>
</div>

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

Exploring the usage of arrays within Angular 4 components. Identifying and addressing overlooked input

I'm struggling with array declaration and string interpolation in Angular 4 using TypeScript. When I define the following classes: export class MyArrayProperty { property1: string; property2: string; } export class MyComponent { @Input() object: ...

How to retrieve the dimensions of an image for a specified CSS class with Selenium or PIL in Python

I am interested in using Python to determine the size of specific images. Is it possible to automate this process so that I can identify only images with certain classes? I am unfamiliar with PIL. Could I define the CSS class/name/id after specifying the U ...

preserving the current state even after refreshing the page

Currently, I am teaching myself React. When I click on the favorites button, which is represented by a heart symbol, it changes color. However, upon refreshing the page, the color change disappears. To address this issue, I came across the following helpfu ...

Variety of perspectives on Magento products

Is there a way to configure Magento 1.7 to display view.phtml for bundled products and view.phtml for simple products, or the other way around? I am looking to customize the views for different product types - what is the best approach to achieve this? ...

Choosing a root element in a hierarchy without affecting the chosen style of a child

I am working on a MUI TreeView component that includes Categories as parents and Articles as children. Whenever I select a child item, it gets styled with a "selected" status. However, when I click on a parent item, the previously selected child loses its ...

What is the reason that setting the margin of 0 on the body does not eliminate the margin on an h1 element?

I believe the body element should apply styles to all elements contained within it. In this scenario, I am attempting to give my h1 element a margin of 0. body { font: 15px/1.5 Arial, Helvetica, sans-serif; padding: 0; margin: 0; background ...

What is the method for incorporating a button on an HTML webpage that triggers a server-side script to execute?

Currently, I have the following: HTML <form action="test.php" method="get"><input type="submit" value="Run me now!"> php <?php shell_exec('sh test.sh'); ?> sh #!/bin/bash sudo systemc ...

The validation of radio input signals results in an error being returned

For a while now, I've been trying to solve the issue with radio button validation in my current project. Surprisingly, it works fine when there are no other functions in the form besides the radio buttons themselves. I suspect that the problem lies wi ...

Issue with form action failing to properly refresh

I'm encountering an issue with form submission. I have an HTML form, but the action doesn't seem to be working properly. Could you take a look at it and see if there's anything I'm missing? <form action="add.php" method="post" autoc ...

When the Image Icon is clicked, the icon itself should become clickable and trigger the opening of a popup Dialogue Box for uploading a new image

When I click on the image icon, it should be clickable and a popup dialogue box will open to upload a new image. Check out this sample image for reference. Any help on this would be greatly appreciated. Thanks in advance. <div class="d-flex align-ite ...

Unveiling the Magic: Enhancing Raphaeljs with Interactive Click Events on a Delicious Slice of the

I'm having trouble responding to a click event on each slice of a Raphael Pie Chart. I've tried implementing the code below, but it doesn't seem to be working. The code is just two lines, commented as "My Code", in the example from the offic ...

Issues with the Dropdown Menu

I'm currently working on a dropdown menu and I've encountered two issues. When I hover over the links in the main navigation bar (such as 'about' and 'connect'), only the actual words are clickable, not the entire area tha ...

What could be causing my insertRow function to inject empty cells into my table?

I am facing an issue with adding the results of a fetch request into a table as a new row. Strangely, when I click the button to trigger this function for the first time, it adds an empty row instead of the desired data. However, on subsequent clicks, the ...

Creating a HTML element that functions as a text input using a div

I am encountering an issue with using a div as text input where the cursor flashes at the beginning of the string on a second attempt to edit the field. However, during the initial attempt, it does allow me to type from left to right. Another problem I am ...

The page refreshes when the anchor element is clicked

Currently, I am working on enhancing a web application that is built on the foundation of traditional aspx (asp.net) web forms with elements of Angular 6 incorporated into it. My current assignment involves resolving a bug that triggers a page refresh whe ...

Is it feasible to utilize the .fadeTo() function in jQuery multiple times?

I need some help with writing a script that will display a warning message in a div tag every time a specific button is pressed. I chose to use the fadeTo method because my div tag is within a table and I want to avoid it collapsing. However, I'm noti ...

Clicking on the images will display full page versions

Apologies for not making an attempt just yet, but I'm struggling to locate what I need. Here's the menu structure in question: <div class="overlay-navigation"> <nav role="navigation"> <ul class="test"> & ...

error occurred while trying to upload a file

I am encountering an issue while trying to upload a file using PHP. Here is the HTML code snippet: <form enctype="multipart/form-data" id="form" action="action.php"> <input type="file" name="file"/><br/><br/> <input type ...

Troubleshooting: Jquery UI Draggable - Cursor losing track of draggable element

I want to create a draggable popup with Jquery UI functionality, but I'm facing an issue where the cursor does not stay aligned with the popup element when dragged. When dragging the popup element to the left, the mouse cursor goes beyond the div elem ...

Scroll effortlessly with wrapping divs

Hey there! I've been experimenting with the Smooth Div Scroll plugin which you can find on their website here: The implementation is pretty simple. I'm using the touch example, but with a twist - instead of images, I am using Divs to scroll thro ...