Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Opacity with jQuery

I have 9 items on a grid, I want all items to have 0.5 opacity on every item and only when hovered over should the div/item and everything inside have 1.0 opacicty.

Here is the JS

$('.gallery-single').css({ opacity: 0.5 });

$('.gallery-single a').mouseover(function(){
    $('.gallery-single-title', this).css('display', 'block');
        $('.gallery-single', this).css({ opacity: 1 });
});
$('.gallery-single a').mouseout(function(){
    $('.gallery-single-title', this).css('display', 'none');
        $('.gallery-single', this).css({ opacity: 0.5 });
}); 

HTML

<div class="gallery-single">
<a href="#" title="">
<div class="gallery-single-title hide">Some text goes here</div>
<div class="gallery-single-img"><img src="http://img.youtube.com/vi/code/0.jpg" width="300" height="200" /></div>
</a>
</div>

All items are at opacity 0.5 when loaded but opacities are not changed when focused. What am I doing wrong here?

like image 395
Ian Avatar asked Jun 08 '11 05:06

Ian


People also ask

How to change opacity in jQuery?

The fadeTo() is an inbuilt method in jQuery which is used to change the opacity of the selected element. Here selector is the selected element. speed: It specifies the speed of the fading effect. opacity: It specifies to fade and this value should must be in between 0.0 and 1.0.

What does fadeTo () do?

fadeTo() method animates the opacity of the matched elements. It is similar to the . fadeIn() method but that method unhides the element and always fades to 100% opacity. Durations are given in milliseconds; higher values indicate slower animations, not faster ones.

Can you fade an element to a desired value of opacity using jQuery?

The jQuery fadeTo() method allows fading to a given opacity (value between 0 and 1). Syntax: $(selector).fadeTo(speed,opacity,callback); The required speed parameter specifies the duration of the effect.

Which jQuery method is used to show selected elements by adjusting height?

jQuery height() Method The height() method sets or returns the height of the selected elements. When this method is used to return height, it returns the height of the FIRST matched element. When this method is used to set height, it sets the height of ALL matched elements.


2 Answers

Try this:

$('.gallery-single a').hover(function(){
    $(this).closest('.gallery-single-title').css('display', 'block');
        $(this).closest('.gallery-single').css({ opacity: 1 });
},
function(){
    $(this).closest('.gallery-single-title').css('display', 'none');
        $(this).closest('.gallery-single').css({ opacity: 0.5 });
}); 

Working example.

like image 146
Michael Robinson Avatar answered Sep 30 '22 07:09

Michael Robinson


The problem is that .gallery-single is an ancestor of the anchor (i.e. it's outside the anchor). The $(selector, this) format looks for the selector within this. Instead, use .closest():

$(this).closest('.gallery-single').css(...);

Sidenote: jQuery gives this warning about mouseover (also applies to mouseout):

This event type can cause many headaches due to event bubbling. For instance, when the mouse pointer moves over the Inner element in this example, a mouseover event will be sent to that, then trickle up to Outer. This can trigger our bound mouseover handler at inopportune times. See the discussion for .mouseenter() for a useful alternative.

You should use mouseenter (and mouseleave) instead (or the hover() function which conveniently combines the two).

like image 38
David Tang Avatar answered Sep 30 '22 06:09

David Tang