13

I have this code that changes the opacity of the div on hover.

$("#navigationcontainer").fadeTo("slow",0.6); 

$("#navigationcontainer").hover(function(){ $("#navigationcontainer").fadeTo("slow",
    1.0); // This sets the opacity to 100% on hover },function(){ 

$("#navigationcontainer").fadeTo("slow",
    0.6); // This sets the opacity back to 60% on mouseout });

I want to have a delay before setting the div back to 0.6 opacity how would i do this

Péter Török
  • 114,404
  • 31
  • 268
  • 329
user272899
  • 871
  • 5
  • 16
  • 22

3 Answers3

52

With jQuery 1.4, you have a method called delay, which takes an integer representing ms you want to delay

$("#navigationcontainer").delay(500).fadeTo("slow", 0.6);

Half a second delay

peirix
  • 36,512
  • 23
  • 96
  • 126
2

use set timeout with a callback to the functionality you want and a delay that you want.

$("#navigationcontainer").fadeTo("slow",0.6); 

$("#navigationcontainer").hover(function(){ $("#navigationcontainer").fadeTo("slow",
    1.0); // This sets the opacity to 100% on hover },function(){ 


var delay = 1000;
setTimeout(function() 
    { 
        $("#navigationcontainer").fadeTo("slow",
            0.6); // This sets the opacity back to 60% on mouseout });

    },
    delay
) 
MedicineMan
  • 15,008
  • 32
  • 101
  • 146
0

How about

$("#hover_me").hover(function() {
                $("#target_div").fadeTo("slow", 1.0);
           }, function() {
                $("#target_div").delay(800).fadeTo("slow", 0.6);
           }); 
odavy
  • 485
  • 1
  • 4
  • 10