HSL随机颜色将其应用于函数

时间:2014-03-23 20:59:21

标签: javascript jquery html css

我对jquery很新,并且我试图让它为每次闪烁的单词生成的颜色总是不同的颜色,但无法弄清楚如何这样做。请查看下面的代码或查看jsfiddle。

http://jsfiddle.net/schan01/2d8Rr/

<!doctype html>
<html>
<head>
    <style>

        html{
            height:100%;
            overflow:hidden;
        }

        p{
            font-family: "Helvetica";
            font-size: 10px;
            float:left;
            margin:10px;

        }


        .box {
            width: 10px;
            height: 10px;    
            float:left;
            margin:10px;
        }



        .hovered{
            color: blue;

            -webkit-animation-name: blinker;
            -webkit-animation-duration: 1s;
            -webkit-animation-timing-function: linear;
            -webkit-animation-iteration-count: infinite;

            -moz-animation-name: blinker;
            -moz-animation-duration: 1s;
            -moz-animation-timing-function: linear;
            -moz-animation-iteration-count: infinite;

            animation-name: blinker;
            animation-duration: 1s;
            animation-timing-function: linear;
            animation-iteration-count: infinite;

        }

        @-moz-keyframes blinker {  
        0% { opacity: 1.0; }
        50% { opacity: 0.0; }
        100% { opacity: 1.0; }
        }

        @-webkit-keyframes blinker {  
        0% { opacity: 1.0; }
        50% { opacity: 0.0; }
        100% { opacity: 1.0; }
        }

        @keyframes blinker {  
        0% { opacity: 1.0; }
        50% { opacity: 0.0; }
        100% { opacity: 1.0; }
        }




    </style>

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>

        var g = 0;


        $(function() {

            for(var i=0; i<1000; i++){
                            $("body").append("<p>blink</p>");
                        }

            $("p").hover(function() {
                //hovered on
                $(this).addClass("hovered")({
                    "color":"hsl(" + g + ",100%,50%)"
                });

        });

    });

    </script>
</head>

<body>

</body>

</html>

感谢给予的任何帮助或建议。感谢。

1 个答案:

答案 0 :(得分:0)

.addClass()

之后,您错过了.css()的来电
$(this).addClass("hovered").css({
    "color": "hsl(" + g + ",100%,50%)"
});

fiddle