jQuery to change (with fade animation) background image of div on hover

前端 未结 6 1449
南笙
南笙 2021-01-12 16:40

I am trying to change the background image of a div on hover with jQuery. This is what I came up so far, however, it\'s not working:

html

         


        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 17:12

    I saw someone saying you can't do it with jQuery, well here is my example and it works. $(".bannerImages img") is calling my image directly, so we can change its attribute using $(this). Once that's done you can call $(this) and change its attr, and also add an animation.

    $(".bannerImages img").animate({opacity: 0}, 'slow', function() {
         $(this)
             .attr({'src': 'images/mainSlider/ps1.jpg'})
             .animate({opacity: 1});
    });
    

提交回复
热议问题