toggle image on hover using Jquery

后端 未结 2 1911
孤城傲影
孤城傲影 2021-01-23 19:57

First of all please dont suggest me to do this using css.

I need to toggle between two images on-hover using Jquery. I need that two images (two toggling images

2条回答
  •  难免孤独
    2021-01-23 20:37

    $(function(){
       var imgs = [
           new Image(),
           new Image()
       ];
    
       imgs[0].src = 'http://www.typeofnan.com/pic1.jpg';
       imgs[1].src = 'http://www.typeofnan.com/pic2.jpg';
    
       $('#hoverelement').hover(function(){
          $(this).html(imgs[0]);
       }, function(){
          $(this).html(imgs[1]);
       });
    });​
    

    That should work, even if I'm not sure if that is what you need. You can add an onload event to both images to makes sure, they are really loaded before allowing hovering.

提交回复
热议问题