随机淡入和淡出

时间:2014-01-04 01:39:35

标签: javascript jquery html css testing

我需要下面的每个闪光灯随机淡入并淡出,但并非一下子全部淡出。 1-2一次是好的。此外,我不希望“包”图像褪色。由于悬停等功能,我不希望闪光灯淡入/淡出。我也需要他们所有人在5秒后消失。请帮助,我已经尝试了一切,无法弄清楚..我是Web开发和javascript的新手。

这是我发现的小提琴,我希望它与此类似,我尝试编辑代码。我现在很困惑。

http:// jsfiddle(dot)net / maniator / rcts4 / - 小提琴(我无法上传超过2个链接,请在http://之后取出空格并用“。”替换“(点)”。查看小提琴。

http://pastebin.com/DkDjU0qS - HTML http://pastebin.com/Zr1vjafn - CSS

1 个答案:

答案 0 :(得分:1)

<强> HTML

<html>
    <head>
        <link rel="stylesheet" href="style.css" />
    </head>

    <body>
        <!-- Pack -->
        <img id="pack" src="http://cloud.attackofthefanboy.com/wp-content/uploads/2013/07/fifa14pack.png">

        <!-- Sparkles-->
    <div id="sparkles">

        <img id="sparkle1" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle2" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle3" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle4" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle5" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle6" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle7" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle8" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">
        </div>
        <script>

        // Define a random integer function
        function random(n) {
            return Math.floor(Math.random() * n);
        }

        // Define some variables, hide all images and show just one of them.
        var transition_time = 500;
        var waiting_time = 500;
        var images = $('div#sparkles img');
        var n = images.length;
        var current = random(n);

        images.hide();
        images.eq(current).show();

        // Periodically, we fadeOut the current image and fadeIn a random one
        var interval_id = setInterval(function () {
             images.eq(current).fadeOut(transition_time, function () {
                 current = random(n);
                 images.eq(current).fadeIn(transition_time);
             });
        }, 2 * transition_time + waiting_time);

       </script>
    </body>
</html>

<强> CSS

 html, body {
        width: 100%;
        height: 100%;

        background-color: white;
    }

    #pack {
        position: absolute;
        top: 50%;
        left: 50%;
        height: 300px;
        width: 200px;
        margin-top: -100px;
        margin-left: -100px;
    }

    #sparkle1 {
        position: absolute;
        top: -8%;
        left: 50%;
        height: 140px;
        width: 150px;
    }

    #sparkle2 {
        position: absolute;
        top: 90%;
        left: 57%;
        height: 180px;
        width: 155px;
    }

    #sparkle3 {
        position: absolute;
        top: 94%;
        left: 29%;
        height: 140px;
        width: 115px;
    }

    #sparkle4 {
        position: absolute;
        top: 25%;
        left: 86%;
        height: 150px;
        width: 123px;
    }

    #sparkle5 {
        position: absolute;
        top: 20%;
        right: 83%;
        height: 145px;
        width: 118px;
    }

    #sparkle6 {
        position: absolute;
        top: 79%;
        right: 82%;
        height: 150px;
        width: 120px;
    }

    #sparkle7 {
        position: absolute;
        top: 52%;
        right: 85%;
        height: 150px;
        width: 130px;
    }

    #sparkle8 {
        position: absolute;
        top: 50%;
        left: 85%;
        height: 180px;
        width: 160px;
    }

    #sparkles {
        position: absolute;
        top: 50%;
        left: 50%;

        width: 500px;
        height: 500px;
        margin-top: -250px;
        margin-left: -250px;
    }

以下是更新后的JSFiddle

希望这有帮助。