如何让Apple Javascript Split Flap Counter在纯CSS上运行

时间:2011-01-17 01:06:52

标签: javascript html css

如何在不使用胶片图像的情况下复制苹果的分瓣计数器?我想创建一些类似的显示单词和其他特殊字符。

可能使用CSS过渡或css变换?

链接到苹果网站:http://www.apple.com/itunes/10-billion-app-countdown/

感谢。

2 个答案:

答案 0 :(得分:1)

我使用DIV,setInterval,setTimeout和CSS3(webkit-transform,animation和keyframes)实现了这一点。

这是粗略的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>untitled</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.19.1" />
    <style>
        @-webkit-keyframes spin {
          0% { -webkit-transform-origin: 100% 100%; -webkit-transform: rotateX(0deg) skew(0deg, 0deg); }
          100%   { -webkit-transform-origin: 100% 100%; -webkit-transform: rotateX(90deg) skew(5deg, 0deg); }
        }
        @-webkit-keyframes spin2 {
          0% { -webkit-transform-origin: 0% 0%; -webkit-transform: rotateX(90deg) skew(-5deg, 0deg); }
          100%   { -webkit-transform-origin: 0% 0%; -webkit-transform: rotateX(0deg) skew(0deg, 0deg); }
        }
        .box span { font-family: arial; font-weight:bold; font-size: 72px; display:inline-block; }
        .scale { -webkit-animation: spin 0.15s infinite linear; }
        .scale2 { -webkit-animation: spin2 0.15s infinite linear; }
        .dv { background-color: #FFF; border:1px solid #333; display:block; height:45px; width:80px; overflow:hidden; text-align: center; line-height:100%; }
        .dv > div > span { width:100%; vertical-align:middle }
        .dv > div { height:200%; }
        .down > div { position:relative; top:-100%; }
        .spn { position:absolute; }
        .spn.top { z-index:20 }
        .spn.top > .dv { z-index:15 }
        .spn.down { z-index:10 }
        .spn.down > .dv { z-index:5 }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
    <script>
    $(function() {
    var ctr, loop;
    var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
    var chars = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
    var numbers = "0123456789";
    var duration = 150;

    function switchToNext(loopthis, end, box) {
        box.removeClass('normal').addClass('scale');
        box.text(loopthis.charAt(ctr));
        ctr++;
        if(ctr == end) clearInterval(loop);
        if(ctr > loopthis.length -1) ctr = 0;
        }

    function loopThrough(a, b) {
        var tmpStart, tmpEnd, stype, etype, loopthis;
        stype = letters;
        tmpStart = stype.indexOf(a);
        if(tmpStart < 0) { stype = numbers; tmpStart = numbers.indexOf(a); }
        if(tmpStart < 0) { stype = chars; tmpStart = chars.indexOf(a); }
        etype = letters;
        tmpEnd = etype.indexOf(b);
        if(tmpEnd < 0) { etype = numbers; tmpEnd = numbers.indexOf(b);}
        if(tmpEnd < 0) { etype = chars; tmpEnd = chars.indexOf(b);}
        if(stype !== etype) { loopthis = stype + etype; tmpEnd = tmpEnd + stype.length } 
            else { loopthis = stype; }
        ctr = tmpStart;
        box = $("div.box").children("span#1").find("span");
        tmpbox = $("div.box").children("span#2").find("span");
        boxa = $("div.box").children("span#1").children("div.up").find("span");
        boxb = $("div.box").children("span#1").children("div.down").find("span");
        tmpboxa = $("div.box").children("span#2").children("div.up").find("span");
        tmpboxb = $("div.box").children("span#2").children("div.down").find("span");
        var delay;
        loop = setInterval(function() {
            end = tmpEnd + 1;
            boxa.parent().parent().addClass('scale');
            delay = setTimeout(function() { boxb.parent().parent().addClass('scale2'); }, duration);
            if(ctr+1 < end) { setTimeout( function(){ tmpboxa.text(loopthis.charAt(ctr));}, duration/2); }
            boxa.text(loopthis.charAt(ctr));
            boxb.text(loopthis.charAt(ctr));
            if(ctr == tmpStart) tmpboxb.text(loopthis.charAt(ctr)); else setTimeout(function() { tmpboxb.text(loopthis.charAt(ctr-1)); }, (duration/2));
            ctr++;
            if(ctr == end) { setTimeout(function() { boxb.parent().parent().removeClass('scale2'); }, duration); clearInterval(loop); boxa.parent().parent().removeClass('scale'); clearTimeout(delay); }
            if(ctr > loopthis.length -1) ctr = 0;
            }, duration);
            //box.removeClass('scale').addClass('normal'); 
        }
    var str = new String($("div.box").text());
    loopThrough("A","9");

    });
    </script>
</head>

<body>
    <div class='box'>
    <span id="1" class='spn top'>
        <div class='dv up'><div><span></span></div></div>
        <div class='dv down'><div><span></span></div></div>
    </span>
    <span id="2" class='spn down'>
        <div class='dv up'><div><span></span></div></div>
        <div class='dv down'><div><span></span></div></div>
    </span>
    </div>
</body>

</html>

答案 1 :(得分:1)

虽然不是纯CSS,但有人建立了nice script来完全复制Apple网站的功能。