Multiple hover highlighting techniques

Recently, I came up with an interesting concept for providing user feedback. My idea is to have a menu where hovering over it highlights an image that corresponds to the menu item, or vice versa - hovering over an image will highlight the corresponding menu item.

I believe this functionality can be achieved using jQuery, but I'm curious if it's possible to do it purely with CSS. If you have any examples or code that could help me implement my idea, I would greatly appreciate it!

Thank you in advance.

Answer №1

When working with jQuery, imagine you have a menu and some images:

<ul>
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
</ul>

...

<img src="/img1.jpg" />
<img src="/img2.jpg" />

To connect the two, consider adding a class or rel reference:

<ul>
<li><a href="#" rel="img1">item 1</a></li>
<li><a href="#" rel="img2">item 2</a></li>
</ul>

...

<img src="/img1.jpg" id="img1" />
<img src="/img2.jpg" id="img2" />

You can then use jQuery to add a class on rollover:

$(function(){
    $('li a').bind('mouseenter mouseleave',function(e){
        $(this).toggleClass('highlight');
        $('#'+$(this).attr('rel')).toggleClass('highlight');
    });
    $('img[id^=img]').bind('mouseenter mouseleave',function(e){
        $(this).toggleClass('highlight');
        $('a[rel='+$(this).attr('id')+']').toggleClass('highlight');
    });
});

You can use any identifier like .text() or .atrr('src') The highlight class would define your rollover styles, which can be different for the menu and images by using li .hightlight and img .highlight or unique classes.

Answer №2

To achieve the desired effect without using Javascript, you can utilize absolute positioning on the image. Simply nest the img within the element (preferably an anchor tag to ensure compatibility with IE6), position it absolutely (assign IDs to different menu items for unique positioning) and apply a hover style to the anchor. This hover action will apply to both the absolutely positioned img and the encompassing a element.

Below is a straightforward example:

<!DOCTYPE html>
<html>
<head>
<style>
#container {
    margin: 10px auto;
    padding: 0;
    position: relative;
    width: 960px;
}
#menu {
    list-style: none;
    margin: 0px;
    padding: 100px 0px; /* simulate header area */
}
#menu a {
    border: 1px solid #fff;
    float: left;
}
#menu a:hover,
#menu a:hover img {
    border: 1px solid #f00;
}
#menu img {
    border: 1px solid #fff;
    position: absolute;
    top: 0px;
}
#test1 img {
    left: 0px;
}
#test2 img {
    left: 40px;
}
.clear {
    clear: left;
}
</style>
</head>
<body>
<div id="container">
    <ul id="menu">
        <li><a href="#" id="test1">here is some menu text <img src="http://www.gravatar.com/avatar/9565672b231ef0e90d5a625699f2eafc?s=32&d=identicon&r=PG" /></a></li>
        <li><a href="#" id="test2">here is some more menu text <img src="http://www.gravatar.com/avatar/9565672b231ef0e90d5a625699f2eafc?s=32&d=identicon&r=PG" /></a></li>
    </ul>
    <div class="clear"></div>
</div>
</body>
</html>

Answer №3

If the image you want to emphasize is a sibling or offspring of the element you are hovering over, it can be achieved quite easily.

Siblings

<div>

<img class="thumbnail" src="path/to/thumb.png" />
<img class="fullSize" src="path/to/Full.png" />

</div>

.thumbnail:hover + .fullsize {
display: block;
}

Descendants

<ul>
<li><img class="thumbnail" src="path/to/thumb.png" />
        <ul>
                <li class="fullsize"><img src="path/to/full.png" /></li>
                <li class="desc">Description text for above image</li>
        </ul>
</li>
</ul>

.thumbnail .fullsize,
.thumbnail .desc {
        display: block;
}

Unfortunately, the reverse (hovering over the image to show the menu) cannot be accomplished with CSS due to the one-way cascade nature. However, it can be implemented easily using JavaScript, particularly jQuery:

$(document).ready(
    function(){
        $('.fullsize.').hover(
            function() {
                $(this).closest('.thumbnail').show();
            }
        );
    }
);

I am intrigued as to the reason behind wanting this feature. It appears that you wish to hover over either 'thumbnail' or 'fullsize' to reveal the other. This suggests that one or both of these elements will be hidden at any given time. In such cases, how does the user know they exist for interaction?

See Demo at JS Bin (based on siblings).


Here's an alternate method for creating a relationship between the two anywhere on the page:

Demo: at JS Bin.

HTML:

$(document).ready(
  function(){
    $('.fullsize, .thumbnail').mouseover(
      function() {
        var currId = $('.fullsize').index();
        if ($(this).hasClass('thumbnail')) {
          var toShow = '.fullsize';
        }
        else {
          var toShow = '.thumbnail';
        }

        $(toShow).eq(currId).addClass('show');
      }
    );
    $('.fullsize, .thumbnail').mouseout(
      function() {
        var currId = $('.fullsize').index();
        if ($(this).hasClass('thumbnail')) {
          var toShow = '.fullsize';
        }
        else {
          var toShow = '.thumbnail';
        }

        $(toShow).eq(currId).removeClass('show');
      }
    );
  }
);

This method differs slightly from @BenWells' suggestion in that it does not require an explicit relationship, but it does rely on the thumbnails being in the same order as their corresponding fullsize elements (or vice versa) since it is based on their index positions.

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

Adding and subtracting quantities inside a loop

My current issue involves a +/- quantity feature that is functioning correctly, but when integrated into a loop, the quantity amounts duplicate for each input field when the +/- buttons are clicked - please refer to the attached screenshot below. Screensh ...

Is it necessary to have height:0 and width:0 when display:none is already applied?

Is it necessary to include height:0 and width:0 when applying display:none to a class? I've noticed these properties being used in certain instances, could they potentially resolve any issues with compatibility in older browsers? ...

Save an HTML5 canvas element as a picture using JavaScript and specify the file extension

Is there a way to save an Html5 canvas element as an image file using Javascript? I've tried using the CanvasToImage library, but it doesn't seem to work for this purpose. You can view my current code on JsFiddle. <div id="canvas_container" ...

Utilizing jQuery to toggle the visibility of a container and then showing/hiding a sub-container nested within it

I have a unique challenge with nested links within a div. My goal is to hide the div containing the clicked link and reveal another div instead. Further, if a link is clicked within that newly revealed div, I want to switch to yet another div. Here is how ...

Purge all previous page visits within a specified domain upon user logout using JavaScript

On my website, abc.xyz.com, an individual named A logs in from the login page and is directed to the home page. Then, when user A clicks on the profile button, they are taken to a page displaying their information. User A logs out, prompting the site to ...

Instructions for converting a readonly text field into an editable one using JavaScript

I'm trying to make it so that when the button (with id name_button) is clicked, the text field (with id name_text_field) becomes enabled. However, my current code doesn't seem to be working. Here's a snippet of my HTML: <input type="tex ...

Gulp does not generate any files

Hey there, I'm brand new to using node.js and coding in general. I recently attempted to convert SCSS to CSS using gulp. My gulpfile.js seems to be correct, but when I try running "gulp styles" in the node.js command prompt, I receive the following ou ...

Wrapping classes with jQuery

Hey there! I have a list of li's with varying dynamic classes. Each li has a .connected class and two sets of "numberclasses." <ul> <li class="connected 34 22"></li> <li class="connected 54 11"></li&g ...

Obtain the unique identifier for every row in a table using jQuery

Apologies for not including any code, but I am seeking guidance on how to use an each() statement to display the ID of each TR element. $( document ).ready(function() { /* Will display each TR's ID from #theTable */ }); <script src="https:// ...

Issues with aligning div elements centrally

I have a dilemma with aligning two divs on my webpage. I am trying to position the second div in such a way that it is centered between the first div and the end of the page, like this: | | | | | | | | | | | | | | div1 | ...

What is causing me to receive a Request Method of "GET" instead of "POST"?

I recently implemented the dropzone js library for uploading images and have a query regarding it. Is the POST method more suitable than GET when uploading an image? If yes, how can I rectify this? $(document).ready(function() { var myDropzone = new Dr ...

Tips for keeping notification icons in the upper right corner of the box?

I am facing an issue with absolute positioning where the notification icons I've positioned to the top-right corner of a box are moving away when the screen size changes. Desired appearance of the image Actual appearance when screen size is adjusted ...

Indent the initial line of a new paragraph

CSS <p><br />1.2 text text text text text texttext text texttext text texttext text texttext text texttext text texttext text texttext text texttext text texttext text texttext text text</p> <p><br />1.3 text text text text ...

Switching the arrow direction in Bootstrap 4 menu toggles

I'm a beginner with Bootstrap 4. My navigation menu includes a dropdown item with the standard caret pointing down, added automatically by Bootstrap. Now, I want to make the caret point up after opening the dropdown menu for the nav item. Despite ext ...

The ticking clock between the beginning and final moments

I am using jQuery and trying to create a countdown timer between a start time and end time. While I have successfully calculated the difference between the two times, I am unsure how to count down the time stored in the difference variable in my code: v ...

Attach a child element to the bottom of its parent div when the parent div reaches the bottom

I'm looking to make a child div stick to the bottom when the parent div reaches the bottom of the browser window. Note: This behavior should only occur when the parent div is pushed down, not when the page is scrolled down. For instance, in my demon ...

Enhance your Windows Media Player settings

I inserted a Windows Media Player on my webpage like this: <object id="mPlayer" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" type="application/x-oleobject"> <embed name="mPlayer" pluginspage="http://www.microsoft.com/Windows/Medi ...

a guide on utilizing knockout js to bind json data within a datalist

Below is the code I have written for my web services: public class SympsService : System.Web.Services.WebService { [WebMethod] public symps GetSymptoms(string organ_name) { symps Symptoms = new symps(); string CS = Configurati ...

What could be causing the empty space on the right side of my container?

Currently, I'm tackling a project using only HTML, CSS, and Bootstrap. Things were going smoothly until I stumbled upon an unexpected gap on the right side of my container. I'm puzzled as to why this is happening and how I can go about rectifying ...

Listening for a click event on a newly added element using JQuery

I am relatively new to using JQuery and I have encountered an issue with adding a click event listener for a dynamically created button. When the user clicks on the first button, my function successfully adds a second button to a div. However, I am facing ...