jQuery .delay() doesn't work

后端 未结 8 1102
暗喜
暗喜 2020-12-19 18:26

I\'ve the following JavaScript snippet:

$(\"#dashboard\").addClass(\"standby\").delay(3000).removeClass(\"standby\");
$(\".active\").removeClass(\"active\");         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 18:53

    Delay won't work the way you expect it to on these line:

    $("#image1").delay(9000).attr("src", "image/image1.jpg");
    $("#image1").delay(9000).attr("src", "image/image2.jpg");
    

    It will run the attribute change immediately. Why? Because the attribute change isn't part of the "animation". Delay can only be used with with animating functions.

    If you only need two images, it might be easiest for you to have two images stacked together, and fade them in and out as needed.

    If you want to expand this to many images, try using the more robust ".animate" function to fade in and out. "Animate" can be given a callback function that will be called when complete.

提交回复
热议问题