Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger hover effect over another element when hovering an element

Tags:

jquery

I am having issues with this: I want to force a hover function over the image when infact hovering over the h3 element

html:

<p><a><img class="size-full wp-image-1236 alignleft" alt="phone-icon-red" src="..." width="50" height="50" /></a></p>
<h3>Contact Us</h3>

CSS:

img:hover{
    margin-left: 10px;
}

js:

$('h3').hover(function(e){
                e.preventDefault();
                $(this).prev('p').find('img').trigger('mouseover');
            });

See my fiddle

like image 922
David Van Staden Avatar asked Apr 04 '13 10:04

David Van Staden


People also ask

How can you affect other elements when one element is hovered?

Approach: This task can be accomplished by adding one element inside the other element & accordingly declaring the required CSS properties for the parent-child elements, so that whenever hovering outside the element then automatically the property of the inner element will change.

How do I hover another div in CSS?

That's using the general sibling combinator ( ~ ). If #b is a descendant of #a , you can simply use #a:hover #b . ALTERNATIVE: You can use pure CSS to do this by positioning the second element before the first. The first div is first in markup, but positioned to the right or below the second.

Does Z index affect hover?

If an element is transparent and the element with z-index:-1; is 'under' it. This stops the hover effects. Z-index can you see as elevations in a building, and you watching it from birdseye. You can't reach the basement if there is a floor above it, even if its build from glass.


1 Answers

I have workaround for your issue.

Add img class same as hover pseudo class:

img:hover, img.hover{
    margin-left: 10px;
}

Bind mouseover and mouseout events to h3 element:

$('h3').on({
    'mouseover': function() {
        $(this).prev('p').find('img').addClass('hover');
    },
    'mouseout': function() {
        $(this).prev('p').find('img').removeClass('hover');
    }
});

fiddle

like image 73
Dmonix Avatar answered Oct 11 '22 21:10

Dmonix