如何在更改图像属性时使用jquery获得平滑效果?

时间:2014-07-14 05:07:01

标签: javascript jquery html css jquery-animate

我编写了以下代码来更改鼠标悬停时的图像源。它改变了图像src,但效果是如此不稳定。 OnMouseOver它突然改变了图像。

请告诉我如何减慢动画效果。

    <script>

    $(document).ready(function(){


        $("#image").hover(function(){           
            $(this).attr("src","images/pic2.png")
                },  function(){                 
                    $(this).attr("src","images/pic1.jpg")   
            });
        });


    </script>

</head>

<body>  
    <img id="image" src="images/pic1.jpg">  
</body>

修改

http://jsfiddle.net/MKuvn/

1 个答案:

答案 0 :(得分:5)

您可以在jQuery中使用fadeOutfadeIn效果:

 $(document).ready(function(){

     $("#image").hover(function(){           
         $(this).fadeOut(1000,function(){
           $(this).attr("src","images/pic2.png");
           $(this).fadeIn(1000);
         });
       },  function(){                 
           $(this).fadeOut(1000, function(){
             $(this).attr("src","images/pic2.png");
             $(this).fadeIn(1000);
           });                  
       });
 });

<强> Demo

Fade IN / Out API