Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:38:29+00:00 2026-05-31T07:38:29+00:00

I would like to catch the mouse event for an element that is positioned

  • 0

I would like to catch the mouse event for an element that is positioned under an other element.

Here is a example of what I have : http://jsfiddle.net/KVLkp/13/

Now what I want is that blue square has yellow borders when mouse is over red square.

The important things are :

  • they are images. So there is no way to embed one element in the
    other.
  • this is an easy example, but in reality there are many img. So the
    search of hovered elements by coordinate calculations is not my
    solution (I’ve already found this answer)
  • There is no link between the two images except that the mouse is over them all

Thank you for your help

Edit:
I’ve written a more complete example of my general case :
– img order in DOM is not fixed and can be changed on live
– top and bottom image has various size and position

Thanks to all of you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:38:31+00:00Added an answer on May 31, 2026 at 7:38 am

    You could try something along the lines of the following:

    <style>
    img.hover {
        border-color : yellow;
    }​
    </style>
    <script>
    $('img').mouseenter(function () {
        $(this).addClass("hover");
    }).mouseleave(function (e) {
        var next = e.relatedTarget;
        if (next.tagName.toUpperCase()!="IMG"
                     || $(next).hasClass("hover"))
           $(this).removeClass("hover");
    });
    </script>
    

    Rather than setting the colour of the border directly I’ve defined a class called “hover” that sets the colour, so that I can easily test whether a given element has the border without hard-coding colours everywhere, and then I’ve made use of the event.relatedTarget property to see where the mouse is going when it leaves.

    The “hover” class is only removed if the element the mouse is going to is not an image or is an image that already has the hover class. Seems to work pretty well in this demo: http://jsfiddle.net/KVLkp/18/, but it won’t work if two images are side-by-side with no gap between them or if the image on top isn’t completely “contained” (in a coordinate sense) by the img it overlaps – I decided to go ahead and post it anyway because I hope that seeing event.relatedTarget in the demo will give you enough of a start to come up with something to fit your real situation.

    (Note: “hover” is a bad name for a class given CSS already has the :hover pseudo-class, but by the time I remembered I couldn’t be bothered changing it in the fiddle and here.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to catch the event when the innerHTML of an element changes
I would like to catch the click/change selection event on a listbox that has
I have a base class, and I would like to catch all exceptions of
I have a Try Catch in my code. I would like to know if
I would like to catch an exception an tell if it was me that
I would like to catch and log MySQL warnings in Python. For example, MySQL
I have sentences like : a a a I would like to catch all
I would like to catch all dev tags and their respective content, through php
I am currently playing around with chrome extensions. I would like to catch google
I would like to test some exception handling logic in the empty catch block

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.