Jquery 淡出延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2508773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:39:54  来源:igfitidea点击:

Jquery delay on fadeout

jquerydelayfadeout

提问by user272899

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

我有这段代码可以在悬停时更改 div 的不透明度。

$("#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

我想在将 div 设置回 0.6 不透明度之前延迟一下我该怎么做

回答by peirix

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

在 jQuery 1.4 中,您有一个名为 的方法delay,它采用一个整数来表示您想要延迟的毫秒数

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

Half a second delay

半秒延迟

回答by MedicineMan

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

使用 set timeout 和对您想要的功能的回调和您想要的延迟。

$("#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
) 

回答by odavy

How about

怎么样

$("#hover_me").hover(function() {
                $("#target_div").fadeTo("slow", 1.0);
           }, function() {
                $("#target_div").delay(800).fadeTo("slow", 0.6);
           });