How to show random color on hover in CSS?

前端 未结 5 1583
清歌不尽
清歌不尽 2021-01-17 16:00

I have this CSS code that only displays one color (blue) when there\'s a mouse hover.

.MenuBox {
    transition: all 1.0s ease;
    -moz-border-radius:30px;
         


        
5条回答
  •  情书的邮戳
    2021-01-17 16:53

    I believe you will need jQuery to do this,

    JS

    function startAnimation(o) {
        var hue = 'rgb('
            + (Math.floor(Math.random() * 256)) + ','
            + (Math.floor(Math.random() * 256)) + ','
            + (Math.floor(Math.random() * 256)) + ')';
        $(o.currentTarget).animate( { color: hue }, 500, function() {
            startAnimation(o);
        });
    }
    
    $(document).ready(function() {
        $('a').hover(
            startAnimation,
            function() {
                $(this).stop().animate( { color: '#000' }, 500);
            }
        );
    });
    

提交回复
热议问题