Delay CSS function in Jquery?

后端 未结 8 1233
自闭症患者
自闭症患者 2021-02-13 11:12

How can I delay a CSS change in jquery? Here is my code:

$(\"document\").ready(function() {
    $(\".pressimage img\").mouseenter(function() {
        $jq(this)         


        
8条回答
  •  一生所求
    2021-02-13 11:33

    You can do this with setTimeout();

    $("document").ready(function() {
        var timer;
        $(".pressimage img").mouseenter(function() {
            $jq(this).css('z-index','1000');
            clearTimeout(timer);
        });
        $(".pressimage img").mouseleave(1000,function() {
             timer = setTimeout(function(){
                $jq(this).css('z-index','1');
             }, 500);
        });
    });
    

    The code in setTimeout(code, delay) executes after delay milliseconds. You might have problems with unintended changes if you were to move the mouse about too quickly, so I have cleared the timeout action on mouseenter().

提交回复
热议问题