As you hover over various text, the central image will dynamically transform alongside each selection

Currently working on a website related to property planning for a customer. They have requested that when their customers hover over text, the central image should change. Is there a CSS-only method to achieve this?

<div>
<img src="http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg" class="imgcenter">
<table class="plantable">
    <tbody>
        <tr>
            <td style="color:#469785;"><a href="">Type <b>E</b></a></td>
            <td style="color:#9BB8A0;"><a href="">Type <b>F</b></a></td>
            <td style="color:#9DB77F;"><a href="">Type <b>G</b></a></td>
            <td style="color:#9FA278;"><a href="">Type <b>G1</b></a></td>
            <td style="color:#C9AE77;"><a href="">Type <b>H</b></a></td>
        </tr>
    </tbody>
</table>
</div>

The requirement is that when hovering over Type E, the central image changes to Image E, and when hovering over Type G, it changes to Image G. How can this be achieved?

I came across a solution that may work:

http://fiddle.jshell.net/tbz9nL4g/

<style>
    .hover_image {position:relative;}
    .hover_image .img1{position:absolute; display:none; z-index:99;}
    .hover_image:hover .img1{display:block;}
    .hover_image .img2{position:absolute; display:block; z-index:99;}
    .hover_image:hover .img2{display:none;}
</style>

<div>
    <a href="#" class="hover_image"> link text 
        <span class="img1"><img src="http://www.imagingshop.com/images/sharptone/hdr-2.jpg" /></span>
        <span class="img2"><img src="http://www.gettyimages.co.uk/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg" />
        </span>
    </a>
</div>

However, there are issues with this code, as it does not work when adding additional text. More details can be found here.

Answer №1

Utilize the data attribute to store data-img in a tag and implement it on hover

//js
$('a').hover(function(e){
    if(e.type==='mouseenter' || e.type==='mouseover'){
      $('.imgcenter').prop({'src':$(this).data('img')});
    }else if(e.type==='mouseleave'){
      $('.imgcenter').prop({'src':$('.imgcenter').data('img')});
    }
})
img{width:300px;height:300px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<img data-img="http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg" src="http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg" class="imgcenter" />
<table class="plantable" border=1>
    <tbody>
        <tr>
            <td style="color:#469785;"><a data-img="http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg" href="">Type <b>E</b></a></td>
            <td style="color:#9BB8A0;"><a data-img="http://www.rodalesorganiclife.com/sites/rodalesorganiclife.com/files/images/animal_bird_300.jpg" href="">Type <b>F</b></a></td>
            <td style="color:#9DB77F;"><a data-img="https://sanaakosirickylee.files.wordpress.com/2012/05/chirping-birds.jpg" href="">Type <b>G</b></a></td>
            <td style="color:#9FA278;"><a data-img="http://img1.cookinglight.timeinc.net/sites/default/files/image/2005/09/0509p113-bird-m.jpg" href="">Type <b>G1</b></a></td>
            <td style="color:#C9AE77;"><a data-img="http://orig10.deviantart.net/6a53/f/2012/074/3/0/profile_picture_by_birds_love_us_all-d4sv1v2.jpg" href="">Type <b>H</b></a></td>
        </tr>
    </tbody>
</table>
</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

Different width, same height - images arranged side by side in a responsive layout

The problem lies in the images: https://i.sstatic.net/rydNO.png Here is an example of desktop resolution: https://i.sstatic.net/uv6KT.png I need to use default size pictures (800x450px) on the server. This means I have to resize and crop them with CSS to ...

Trouble with the query waypoints extension in a simple demonstration

Can anyone help me figure out why the basic example from the waypoints plugin isn't working for me? Here's a link to the jsfiddle I created: http://jsfiddle.net/ZA8bd/2/ CSS .block1 { margin-top:30px; width: 400px; background: red; ...

Using CSS to showcase div elements with varying dimensions

How can I arrange divs with varying heights to be positioned close together, regardless of their height, and with a specific margin similar to buildings? I am aiming for a layout like the one on this website . I tried using float:left but it only float ...

"Exploring the technique of extracting substrings in JavaScript while excluding any symbols

I am working on a JavaScript web network project and I need to use the substring method. However, I want to exclude the colon symbol from the substring because it represents a hexadecimal IP address. I do not want to include colons as part of the result. H ...

Media query failing to target HTML form element

Is it possible to dynamically adjust the font size of form labels and input fields based on browser width? I've heard that fonts can't automatically 'scale down' when the browser width decreases, requiring media queries instead. I atte ...

What are the advantages of using classes versus ids for managing multiple li elements in example 20 with knockout and jQuery? Is one option more efficient and easier to maintain

<ul class="sellerDetails sellerdetailsData " data-bind="attr: { id: 'sellerdetailsData-' + $index()}"> <li><span class="buyerprocess-sprite seller-name"></span> <p class="leftfloat"> <span data-bind=" ...

The HTML/CSS navigation bar is displayed on top of a fullscreen video player in the web

I've scoured the internet but I'm still unable to find a solution to this issue. While watching videos in fullscreen mode, the html navigation appears over the video in IE, Chrome and Firefox, but not in Safari on both Mac and PC platforms. The w ...

Adjustable div element and percentage-based height are not functioning correctly

I am facing a minor issue while attempting to create a responsive div with an image background. The div does not match the size of the image: 540px-290px When I resize the browser, the two divs do not resize together. In each div, there is a background ...

Guide to centering a list on a webpage using Bootstrap 3

Looking to create a centered list using the latest version of Bootstrap 3. Any tips? The desired list should be positioned in the middle of the page. ___________________________________________ | | | ...

I successfully managed to ensure that the splash screen is only displayed upon the initial page load. However, I am now encountering an issue where it fails to load again after I close the page. Is there any possible solution

After successfully implementing a splash screen that only plays once on page load, I encountered an issue. When I close the tab and revisit the page, the splash screen no longer plays. Is there a way to fix this problem? Perhaps by setting a time limit for ...

Trouble with CSS submenus: Are your lists not appearing as they should? Potential style override

I am encountering an issue with my dropdown mega menu. I have two sets of list items that should be displayed vertically side by side with bullet points in the dropdown, but only one list is appearing despite coding for both. Additionally, the list elemen ...

How can I ensure that this footer is consistently centered across all screen resolutions?

I've managed to get my CSS looking just the way I want it on my 1366x768 laptop screen, with the element centered at the bottom of the page. However, when I switch to a larger screen, the footer moves to a different position higher up on the page. An ...

The horizontal display list is experiencing issues when using the display inline property

I'm having trouble getting my list to display horizontally. I've tried using display: inline, as well as float: left; and position:center, but can't seem to get it centered properly. JavaScript Array.prototype.map.call(document.querySelect ...

Initial space in model variable not being displayed

Hey there, I am working with a model variable called "name" that is linked to a span element like this: <input type="text" ng-model="name"> <span ng-bind="name"></span> My goal is to display the text entered in the input field without r ...

html and css code to create a linebreak ↵

I received a JSON response from the server, and within this data is a "return line" in the text. {text: "I appear in the first line ↵ and I appear in the second line"} However, when I try to display this on an HTML page, the "return line" does not show ...

Assistance is required for establishing a connection between php and js. The process begins with executing a sql query to extract the necessary data, which is then encoded into JSON format

I have encountered an issue with a project I am working on solo, involving a sidecart in a PHP file with external links to SQL, CSS, and JS. The problem arose when attempting to insert necessary data into JS using JSON encoding. <?php session_start(); ...

Is it possible to invoke fancybox using a class selector?

I'm having trouble using fancybox on an element with this code: $(this).parent().parent().parent().find('.comments').fancybox(); Is it not feasible? Could it be because it's a class and not an id? I have multiple rows of items, so as ...

Designing divs of the same dimensions with a wrap-around effect

I'm currently working on developing a menu system that incorporates images and text above and below each image. The content is dynamic, so I need the menu layout to display with equal spacing between each image, creating a grid-like pattern both horiz ...

Create an input box with a designated class

When I click the button in my HTML and JavaScript code below, I am able to dynamically add a text field and radio button. This functionality is working properly. However, I also want the newly generated text field to have the same font size and appearance ...

Out of the blue div, could you lend your assistance?

I have a code that displays 5 random images with corresponding text. My challenge is that I want to separate the text into another div so that I can add another function to it, while still keeping the images random with their corresponding text. <scr ...