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

前端 未结 6 1450
南笙
南笙 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:19

    DEMO

    $('.logo').hover(
    
        function () {
            $(this).animate({
                opacity: 0
            }, 'fast', function () {
                $(this)
                    .css({
                        'background-image': 'url(http://placehold.it/300x100/ffffff/000000.png&text=second)'
                    })
                    .animate({
                        opacity: 1
                    });
            });
        },
    
        function () {
            $(this).animate({
                opacity: 0
            }, 'fast', function () {
                $(this)
                    .css({
                        'background-image': 'url(http://placehold.it/300x100/ffffff/000000.png&text=first)'
                    })
                    .animate({
                        opacity: 1
                    });
            });
    });
    

提交回复
热议问题