Perform function in attr() callback?

前端 未结 2 1413
甜味超标
甜味超标 2021-01-06 10:24

Not sure if I am doing this correctly or not.

Here is my .js:

var currentIMG;
$( \'.leftMenuProductButton\' ).hover(function () {

              


        
相关标签:
2条回答
  • 2021-01-06 10:39

    You can do this with the load event like this:

    $("#swapImg").css("opacity", 0).attr("src", productImages[swapIMG])
                 .one("load",function(){ //fires (only once) when loaded
                    $(this).fadeIn("slow");
                 }).each(function(){
                    if(this.complete) //trigger load if cached in certain browsers
                      $(this).trigger("load");
                 });
    

    The fade will start after the image load event fires.

    0 讨论(0)
  • 2021-01-06 10:46
    $("#swapImg").css("opacity", 0).attr("src", productImages[swapIMG])
             .on("load",function(){ //fires each time when image loaded
                $(this).fadeIn("slow");
             });
    

    This will also properly.

    0 讨论(0)
提交回复
热议问题