Swap image in div upon hovering over a list of links in another div

I'm in the process of designing a webpage that features a header and footer fixed to the top and bottom of the browser window, with adaptable content in between. To structure the inner content, I've opted for a flexbox wrapper containing a list of links on the left (within its own div) and an adjacent div on the right showcasing images.

Although I have successfully implemented this layout, my next goal is to dynamically change the image displayed in the right div based on the last link hovered over on the left side. This functionality is similar to what you'll find on (try clicking "clients" at the bottom left corner and then hover over the list of links). While it seems like Javascript/jQuery would be the solution, my attempts thus far have not yielded the desired outcome.

This is a basic outline of how my page components are structured:

HTML

<div class="wrapper">
 <header class="header">Header</header>
 <div class="workWrapper">
  <div class="workText">
   <ul class="workList">
    <li id="ex1"><a href="#">Example 1</a></li>
    <li id="ex2"><a href="#">Example 2</a></li>
    <li id="ex3"><a href="#">Example 3</a></li>
    <li id="ex4"><a href="#">Example 4</a></li>
    <li id="ex5"><a href="#">Example 5</a></li>
    <li id="ex6"><a href="#">Example 6</a></li>
   </ul>
  </div>
  <div class="workImages">
   <img src="#">
  </div>
 </div>
 <footer class="footer">Footer</footer>
</div>

CSS

.workList {
list-style-type: none;
margin: 0;
padding: 0;
}

.wrapper {
display: flex;
min-height: 100%;
flex-direction: column;
}

.workWrapper {
width: 100%;
display: flex;
flex: 1;
align-items: center;
}

.workText {
float: left;
width: 30%;
}

.workImages {
float: right;
width: 70%;
margin-right: 100px;
background-color: green;

img {max-width: 100%;}

https://jsfiddle.net/symjcfsk/

Answer №1

There are various methods to achieve this. One way is to utilize the data-* attributes of a hovered link to store image URLs:

$(".workList a").hover(function() {
  var imageUrl = $(this).data("img");
  $(".workImages img").attr("src", imageUrl);
});
  .workList {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.wrapper {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  min-height: 100%;
  flex-direction: column;
}

.workWrapper {
  width: 100%;
  display: flex;
  flex: 1;
  align-items: center;
  margin: 0;
  padding: 0;
}

.workText {
  float: left;
  width: 30%;
}

.workImages {
  float: right;
  width: 70%;
  margin-right: 100px;
  background-color: green;
  img {
    max-width: 100%;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <header class="header">Header</header>
  <div class="workWrapper">
    <div class="workText">
      <ul class="workList">
        <li><a href="#" data-img="https://cdn.sstatic.net/Sites/stackoverflow/img/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1a0b1b1ada4ecb5aeb4a2a9eca8a2aeaf81f3efb1afa6">[email protected]</a>?v=73d79a89bded">Example 1</a></li>
        <li><a href="#" data-img="https://www.socialtalent.co/wp-content/uploads/blog-content/so-logo.png">Example 2</a></li>
      </ul>
    </div>
    <div class="workImages">
      <img src="#">
    </div>
  </div>
  <footer class="footer">Footer</footer>
</div>

Answer №2

If you're looking for an alternative way to achieve a similar effect, try utilizing the "mouseenter" and "mouseout" events. Here's an example of how you can do it:

$(".workList").on("mouseenter",function(event){
     var list_elem = event.target;
     // Add your code here to change the background image as needed.
});

To revert back to the default image when the mouse leaves:

$(".workList").on("mouseout",function(event){
    // Revert back to the default image.
});

A further enhancement could involve keeping track of the last image used for the background to prevent unnecessary image changes when hovering over the same link again.

Answer №3

When it comes to achieving a specific design functionality, pure html and css may not be sufficient on their own. In such cases, importing javascript is necessary. Below is a demo showcasing this concept in action:

var mouseover = function (index) {
  document.getElementById('workImages').className = 'workImages bgcolor' + index;
}
.workList {
  list-style-type: none;
  width: inherit;
  margin: 0;
  padding: 0;
 }
 .workList& >li {
    padding: 10px 5px;
 }
 .workList  a {
  width: inherit;
  display: block;
 }

.wrapper {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  min-height: 100%;
  flex-direction: column;
}

.workWrapper {
  width: 100%;
  height: 230px;
  display: flex;
  flex: 1;
  align-items: center;
  margin: 0;
  padding: 0;
  position: relative;
}

.workText {
  position: absolute;
  width: inherit;
}

.workImages {
  width: 100%;
  margin-right: 100px;
  height:inherit;
  background-color: #E1BEE7;
  transition-duration: 0.5s;
 }
.workImages.bgcolor1 {
  background-color: #CE93D8;
}
.workImages.bgcolor2 {
  background-color: #CE93D8;
}
.workImages.bgcolor3 {
  background-color: #BA68C8;
}
.workImages.bgcolor4 {
  background-color: #AB47BC;
}
.workImages.bgcolor5 {
  background-color: #9C27B0;
}
.workImages.bgcolor6 {
  background-color: #8E24AA;
}
<div class="wrapper">
  <header class="header">Header</header>
  <div class="workWrapper">
    <div class="workText">
      <ul class="workList">
        <li id="ex1"><a href="#" onmouseover="mouseover(1)">Example 1</a></li>
        <li id="ex2"><a href="#" onmouseover="mouseover(2)">Example 2</a></li>
        <li id="ex3"><a href="#" onmouseover="mouseover(3)">Example 3</a></li>
        <li id="ex4"><a href="#" onmouseover="mouseover(4)">Example 4</a></li>
        <li id="ex5"><a href="#" onmouseover="mouseover(5)">Example 5</a></li>
        <li id="ex6"><a href="#" onmouseover="mouseover(6)">Example 6</a></li>
      </ul>
    </div>
    <div class="workImages" id="workImages">
      <!-- use bgcolor instead of img for display-->
      <!--<img src="#">-->
    </div>
  </div>
  <footer class="footer">Footer</footer>
</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

Using jQuery to apply a class based on JSON data

This file contains JSON data with details about seat information. var jsonData = { "who": "RSNO", "what": "An American Festival", "when": "2013-02-08 19:30", "where": "User Hall - Main Auditorium", "seats": ["0000000000000000001111111 ...

Create a new array of objects by extracting specific properties from an existing array of objects

Consider the input array below: const initialArray = [{name: 'john', age: 12, height: 178, likes: 'music'}, {name: 'mike', age: 22, height: 181, likes: 'sport'}, {name: &ap ...

The PHP JSON response is equipped with HTML headers

I'm facing an unusual issue with my PHP code. I'm trying to build a PHP page that sends JSON data back in response to a Jquery AJAX call. However, despite setting the content type as application/json, the response always contains the HTML header. ...

Ways to extract the directive's value specific to my scenario

I'm currently working on developing a directive for my Angular app, and I need to find a way to pass a value to my controller. Here's what I have so far: controllers.directive('checkBox', function() { return { restrict: &a ...

Displaying a certain div when clicked within a loop using Vue.js and Laravel

I am currently facing an issue with displaying a hidden div upon click. The problem arises when using a loop to dynamically generate all the divs. Whenever I click the button, it shows all the divs instead of just one specific div on each click. I attempte ...

A guide on how to efficiently retrieve all images stored in a specific directory

I attempted to showcase all the images from a single directory using jQuery, but unfortunately it is not functioning as expected. My folder setup includes an 'images' folder and a 'js' folder. I followed this question on Stack Overflow ...

Guide for displaying and hiding an image using a button or link in Next.js

I'm a beginner with React and Next.js, and I have the following code snippet: import Link from 'next/link'; import Image from 'next/image'; async function getPokedex() { const response = await fetch(`http://localhost:3000/api/p ...

Switching from a vertical to horizontal tab layout in Angular 4 Material 2 using MD-Gridlist

I'm currently trying to modify the tabbing functionality within an MD-Gridlist so that it tabs horizontally instead of vertically. I've experimented with tab indexes but haven't had any success. My goal is to enable horizontal tabbing throug ...

Is there a way to automatically change the value of one input box to its negative counterpart when either of the two input boxes have been filled in?

Consider two input boxes: box1 box2 If a user enters a number in one of the input boxes, we want the value of the other input box to automatically change to the opposite sign of that number. For example: User enters 3 in box1. The value of box2 shoul ...

Getting the ThreeJs OrbitControl import version directly from the CDN

Using threejs from CDN and requiring OrbitControl as well, I encountered an issue with importing both Three and OrbitControl using the latest version 0.148.0: import * as THREE from 'https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__ ...

Performing a multitude of if statements simultaneously consistently results in an undefined outcome after the sixth iteration

Oops! I can't believe I forgot my original question! Every time I run the sixth if statement in my code, I encounter a typeError Undefined. Interestingly, when I switch the positions of the fifth and sixth statements, the if(!data.body.main) condition ...

After inserting the Telerik component, the code <% ... %> is throwing an error

I have a webpage with ASPX that utilizes JavaScript and ASP components without issues. However, after adding a Telerik combobox, I encountered an error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %& ...

Executing a JQuery function from varying environments

While this question may seem basic, I am having trouble understanding the behavior described. I have written some JavaScript code and I am puzzled why the second call to foo does not work. You can find the code in this JSFiddle link. $.fn.foo = function( ...

Why isn't the button border displaying the color I assigned to it?

After setting a border color to my button, it's not displaying the color when I click on it. Instead, it's showing a different color. How can I resolve this issue? I need some assistance with setting the background of my button to match the imag ...

Retrieve information without causing a complete page reload

Scenario: A customer buys an airline ticket from American Airlines on my website. After a processing time of one hour, I want to display a navigation tab without requiring the user to refresh the page. Inquiry: Is there a method to automatically trigger a ...

Here is how you can pass two callback functions to React.cloneElement:

Recently, I encountered an issue where one of the callbacks passed to a child component using React.cloneElement was always present while the other was undefined. Specifically, activeRow was consistently available but deactivateRow remained undefined. I ...

Trouble getting SVG line to display within Bootstrap 4 flex divs

I am attempting to establish a connection between the first two circular div elements using an SVG line. While inspecting, I can visualize the line but it remains hidden on the page. I tried adjusting the z-index without success. However, increasing the wi ...

What's a quick way in Javascript to add a string to all elements in an array?

I'm working with an array = ["a", "b", "c"]; What I need to do is concatenate a string, let's say "Hello", to each value in this array. The desired output should look like this: ["Hello_a", "Hello_b", "Hello_c"] Is there a quicker way in java ...

Generate an array containing the dates of the past 30 days using Moment.js

Currently, I am utilizing momentjs in my project and aiming to generate an array that comprises of the last 30 days. My approach involves creating a counter and subsequently iterating backwards, generating a new moment instance for each day. However, I a ...

Is Webkit's Overflow Scrolling feature hiding the divs on your website?

I recently implemented a design for my website where the content is contained within an absolute positioned div that takes up the entire screen. However, I noticed that the scrolling on this div felt clunky and not as smooth as I would like it to be. After ...