25540

Jquery fadeOut on hover

I need a bit of help with jquery to get a fadeOut effect happening, here is my code:

http://jsfiddle.net/PPpnT/25/

When you hover over the image the image needs to fade out and show the red underneath, when you mouse off the image should fade back. I think the code may need a stop function - just having a hard time writing it.

All suggestions welcome!

Answer1:

In your specific jsFiddle, use this:

$('#show').hover(function() { $(this).stop(true).fadeTo("slow", 0); }, function() { $(this).stop(true).fadeTo("slow", 1); });​

You can see it work here: http://jsfiddle.net/jfriend00/BJwn7/.

They key here is you can't use .fadeOut() and .fadeIn() because when they are done, they set display: none which causes the element to be hidden and that messes up the .hover() function. So, instead just fade the opacity to 0 (but the hover stays in effect) and then when the hover leaves, fade back to opacity of 1.

I also had to remove the opacity CSS for the red block from your jsFiddle because you want it to be visible when you fade out the image so you can just leave it visible behind the image.

If you only wanted the effect in browser that support CSS3 transitions (no version of IE yet), then you could also do this without jQuery/javascript and just use CSS3 transitions. But for something as simple as this, when you already have jQuery, it's pretty easy to just use jQuery fades and get cross browser support.

Answer2:

$('#show').hover( function () { $(this).fadeOut("slow"); }, function () { $(this).fadeIn("slow"); } );

Answer3:

You started with CSS transitions, so here's an example that uses this:

    <li>http://jsfiddle.net/PPpnT/33/</li> </ul>

    I use Firefox, so the example was updated for Firefox only, but the others should be symmetrical.

    Read more about this here:

Recommend

  • IE7 and “jaggies” around layered PNGs (with jQuery)
  • How can I add some Enum values to a combobox
  • Always use first of default colors instead of black in ggplot2 [duplicate]
  • How do you fade in *partially* in jQuery?
  • Don't queue fadeTo(), fadeIn()/fadeOut()
  • Throwing a New Exception Best Practice [closed]
  • Pop up box to appear once per site visit
  • Fading in large images in Chrome
  • How to change slowly background attributes in jquery?
  • How to lookup value with multiple criteria in excel 2007 and newer
  • Reloading table causes flickering
  • div fade-in when window is scrolled a certain distance from the top
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Replace last two characters in column
  • insert radio value multiple data codeigniter in database to one row?
  • Universal Image Loader reuse images
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Asynchronous UI Testing in Xcode With Swift
  • Why value captured by reference in lambda is broken? [duplicate]
  • Menu Color Fade on Hover with Jquery
  • ActionScript 2 vs ActionScript 3 performance
  • Weird JavaScript statement, what does it mean?
  • Adding custom controls to a full screen movie
  • To display the title for the current loaction in map in iphone
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Traverse Array and Display in markup
  • Comma separated Values
  • How to set the response of a form post action to a iframe source?
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Why joiner is not used after Sequence generator or Update statergy
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • reshape alternating columns in less time and using less memory
  • UserPrincipal.Current returns apppool on IIS
  • How to load view controller without button in storyboard?