Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opacity on a div hover

So far I've got this code

http://jsfiddle.net/Nq79H/1/

but I want to fadeout the image in order to leave only the text visible. Do I need to change the javascript or write a new css div?

$('.text').hide().removeClass('text').addClass('text-js');

$('.thumb').hover(function(){
    $(this).find('.text-js').fadeToggle();
});
like image 311
Hristian Ivanov Avatar asked Mar 20 '13 13:03

Hristian Ivanov


People also ask

How do you change the opacity of hovering?

To make the caption opaque (and easier to read) when the mouse hovers over it, you can use the :hover pseudo-class to change opacity to 1 on hover. Open the HTML page in a browser and hover your mouse over the caption to see it change.

How do you make a hover effect a div?

You'll need a container div and at least one foreground div to cover the background (could be just an image). Then you'll want to target the parent on hover and change the foreground child. I used transform instead of animating a position property because it's more performant.

How to give opacity to color in CSS?

Transparency using RGBA In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

How to make a div background transparent?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).


2 Answers

...but I want to fadeout the image in order to leave only the text visible.

Simply add .fadeToggle() to the img element as well:

$('img', this).fadeToggle();

JSFiddle example.

like image 180
James Donnelly Avatar answered Oct 19 '22 02:10

James Donnelly


Here is the CSS3 transition solution:

jsFiddle

CSS

.thumb .text {
    display: block;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #999;
    background: rgba(0, 0, 0, 0.3);
    text-align: center;
    -webkit-transition:opacity .5s ease;
    -moz-transition:opacity .5s ease;
    transition:opacity .5s ease;
    opacity:0;
}
.thumb:hover .text {
    opacity:1;
}

.thumb img {
    opacity:1;
    -webkit-transition:opacity .5s ease;
    -moz-transition:opacity .5s ease;
    transition:opacity .5s ease;
}
.thumb:hover img {
    opacity:0;
}

Support

The support for CSS3 transitions is pretty decent now, the latest versions of all the major browsers (Safari, Chrome, Opera, Firefox) all support transitions. IE on the other hand only supports it from version 10. Transitions are nice though in that they don't crash and burn when something doesn't support it. The opacity of the element will still change, there will just be no transition.

References

  • Caniuse.com transitions
like image 28
Daniel Imms Avatar answered Oct 19 '22 01:10

Daniel Imms