简体   繁体   English

在 jQuery 中停止悬停后,如何保持悬停 3 秒?

[英]How do you keep hover in effect for 3 seconds after you stop hovering over it in jQuery?

What this code does is when you hover over an item in a list, it changes to red, and displays another list below.这段代码的作用是,当您将鼠标悬停在列表中的某个项目上时,它会变为红色,并在下方显示另一个列表。 If you are no longer hovering over any elements, the goal is to have the latest thing you hovered over stay there for 3 seconds before it's cleared.如果您不再悬停在任何元素上,则目标是让您悬停在其上的最新事物在清除之前停留 3 秒。

$('#stockList li').hover(
            function () {
                    $(this).css({ color: 'red' }); //mouseover
                    if ($(this).text() == symbol) {

                        $('#stockInfo').append('<div><ol><li>' + "Company = " + company + '</li><br/><li>' + "Market = " + market + '</li><br/><li>' + "Sector = " + sector + '</li><br/><li>' + "Price = " + price + '</li><br/><li>' + "Year Range = " + low + " " + high + '</li><br/><li>' + "Dividend = " + amount + " " + yieldx + " " + frequency + '</li></ol><br/></div>');
                    }
                },

            function () {
                $(this).css({ color: 'navy' }); // mouseout
                $('#stockInfo').empty();
            }
        );

Just add delay to it:只需添加delay

$(this).delay(3000).css({ color: 'navy' }); // mouseout
$('#stockInfo').empty();

Remember that javascript time works with miliseconds, so 3 seconds is 3000 miliseconds in javascript.请记住,javascript 时间与毫秒一起工作,因此 3 秒在 javascript 中是 3000 毫秒。

See the jQuery docs on delay here .请在此处查看延迟的 jQuery 文档。

Hover effect remains their until you take mouse cursor off the element悬停效果保持不变,直到您将鼠标光标从元素上移开

jsFiddle js小提琴

You can do something like this :你可以这样做:

var timeout;
    $('#stockList li').hover(
           function () {
              clearTimeout(timeout);// this will take care if user re-hover over the element
              $(this).css({ color: 'red' }); //mouseover
                  if ($(this).text() == symbol) {        
                     $('#stockInfo').append('<div><ol><li>' + "Company = " + company + '</li><br/><li>' + "Market = " + market + '</li><br/><li>' + "Sector = " + sector + '</li><br/><li>' + "Price = " + price + '</li><br/><li>' + "Year Range = " + low + " " + high + '</li><br/><li>' + "Dividend = " + amount + " " + yieldx + " " + frequency + '</li></ol><br/></div>');
                        }
                    },

                function () {
               timeout=  setTimeout(function(){
                    $('#stockList li').css({ color: 'navy' }); // mouseout
                    $('#stockInfo').empty();
                  },3000);
                }
            );
$('#stockList li').hover(
            function () {
                    $(this).css({ color: 'red' }); //mouseover
                    if ($(this).text() == symbol) {

                        $('#stockInfo').append('<div><ol><li>' + "Company = " + company + '</li><br/><li>' + "Market = " + market + '</li><br/><li>' + "Sector = " + sector + '</li><br/><li>' + "Price = " + price + '</li><br/><li>' + "Year Range = " + low + " " + high + '</li><br/><li>' + "Dividend = " + amount + " " + yieldx + " " + frequency + '</li></ol><br/></div>');
                    }
                },

            function () {
                setTimeOut(function(){
                    $(this).css({ color: 'navy' }); // mouseout
                    $('#stockInfo').children('div').remove();
                },3000); // Disappear after 3000 ms or 3 seconds
            }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM