jQuery animate backgroundColor

前端 未结 17 2100
醉梦人生
醉梦人生 2020-11-21 07:11

I am trying to animate a change in backgroundColor using jQuery on mouseover.

I have checked some example and I seem to have it right, it works with other properties

17条回答
  •  野性不改
    2020-11-21 07:51

    I used a combination of CSS transitions with JQuery for the desired effect; obviously browsers which don't support CSS transitions will not animate but its a lightweight option which works well for most browsers and for my requirements is acceptable degradation.

    Jquery to change the background color:

       $('.mylinkholder a').hover(
            function () {
                $(this).css({ backgroundColor: '#f0f0f0' }); 
            },
            function () {
                $(this).css({ backgroundColor: '#fff' });
            }
        );
    

    CSS using transition to fade background-color change

       .mylinkholder a
       {
       transition: background-color .5s ease-in-out;
       -moz-transition: background-color .5s ease-in-out;
       -webkit-transition: background-color .5s ease-in-out; 
      -o-transition: background-color .5s ease-in-out; 
       }
    

提交回复
热议问题