Google LOGO的CSS Sprite

css-sprite

为纪念美国现代舞舞蹈家玛莎·葛兰姆诞辰,Google Doodle推出了一款极其炫酷的LOGO,整个LOGO使用CSS Sprite技术,利用一个初始图片和一张画满各个动作的拼接图片,实现了动画,而非传统的GIF动画图像。

效果

原文查看效果

CSS Sprite简介

CSS Sprites(css精灵),是一种网页图片应用处理方式。它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢慢显示出来了。
限制:不高于200KB的单张图片

整合处理图片的目的是减少请求次数,如果每一张图片都向服务器发送请求,即影响服务器负载也影响页面性能。

CSS Sprite原理

CSS Sprites的原理就是把网页中用到的小图标、图片类集中整合到一张图片中。
再利用CSS中的背景属性

  • background-image
  • background-repeat
  • background-position

当需要显示指定图片时,定位到图片相应位置即可

Google粘土动画

实现步骤:
1、准备素材,设计动画的每一帧图片并整合成一张图片
2、监听点击事件执行动画
3、根据延迟时间设置图片背景位置以显示下一帧图片

素材

HTML

<div>
    <div style="display:flex;align-items:flex-end;">
        <div style="width:135px;height:156px;background:url(/images/css-sprite/blockheads.png) 0 0;" id="blockheads"></div>
        <div style="width:65px;height:102px;background:url(/images/css-sprite/prickle.png) 0 0;" id="prickle"></div>
        <div style="width:67px;height:144px;background:url(/images/css-sprite/goo.png) 0 0;" id="goo"></div>
        <div style="width:98px;height:156px;background:url(/images/css-sprite/gumby.png) 0 0;" id="gumby"></div>
        <div style="width:75px;height:108px;background:url(/images/css-sprite/pokey.png) 0 0;" id="pokey"></div>
    </div>
</div>

JS

<script>
    class Sprite {
        constructor(opt) {
            this.element = document.getElementById(opt.element);
            this.url = opt.url;
            this.delay = 1000 / (opt.speed || 20);
            this.defaultBackground = this.element.style.background;
            this.defalutUrl = this.element.style.backgroundImage;
            this.step = 0;
            this.width = this.element.clientWidth;
            this.height = this.element.clientHeight;
            let img = new Image();
            img.onload = () => {
                this.cols = parseInt(img.width / this.width);
                this.rows = parseInt(img.height / this.height);
                this.frames = this.cols * this.rows;
                this.element.onclick = () => {
                    this.play();
                };
            };

            img.src = this.url;
        }

        play() {
            if (this.playing) {
                return;
            }
            this.playing = true;
            this.next();
        }

        next() {
            this.step++;
            if (this.step > this.frames) {
                this.stop();
                return;
            }
            let x = this.step % this.cols;
            let y = parseInt(this.step / this.rows);
            if (this.step % this.rows > 0) {
                y++;
            }
            var position = "-" + (x - 1) * this.width + "px -" + (y - 1) * this.height + "px";
            this.element.style.background = "url(" + this.url + ") " + position;
            setTimeout(() => {
                this.next();
            }, this.delay);
        }

        stop() {
            this.step = 0;
            this.playing = false;
            this.element.style.background = this.defaultBackground;
        }
    }

    window.addEventListener('load', () => {
        new Sprite({
            element: 'blockheads',
            url:'/images/css-sprite/blockheads.png'
        });
        new Sprite({
            element: 'prickle',
            url:'/images/css-sprite/prickle.png'
        });
        new Sprite({
            element: 'goo',
            url:'/images/css-sprite/goo.png'
        });
        new Sprite({
            element: 'gumby',
            url:'/images/css-sprite/gumby.png'
        });
        new Sprite({
            element: 'pokey',
            url:'/images/css-sprite/pokey.png'
        });
    });
</script>

Google舞蹈者

实现步骤:
1、准备素材,收集舞蹈者每一个动作集成到一张图片
2、初始化每一个动作的位置
3、定时动态创建div元素设置背景位置和元素位置

素材

HTML

<div id="graham-logo" style="position:relative;height:150px;"></div>

JS

(function () {
    window.google={};
        if (!google.doodle) google.doodle = {};
        var d = [
                [307, 48, 88, 89],
                [307, 48, 89, 89],
                [307, 48, 91, 89],
                [305, 49, 93, 89],
                [305, 50, 93, 88],
                [305, 50, 93, 88],
                [306, 52, 92, 86],
                [305, 53, 93, 84],
                [305, 54, 94, 83],
                [306, 54, 93, 83],
                [307, 54, 92, 83],
                [307, 54, 92, 83],
                [308, 54, 90, 83],
                [308, 54, 90, 83],
                [306, 53, 91, 84],
                [306, 53, 91, 84],
                [308, 53, 90, 84],
                [308, 53, 90, 84],
                [305, 53, 92, 84],
                [305, 52, 92, 85],
                [306, 52, 91, 85],
                [308, 51, 88, 87, 1],
                [308, 50, 88, 88],
                [308, 49, 88, 88],
                [307, 49, 89, 88],
                [307, 50, 89, 87],
                [308, 51, 89, 86],
                [307, 54, 90, 83],
                [307, 57, 90, 80],
                [306, 58, 92, 79],
                [306, 58, 92, 79],
                [305, 60, 92, 77],
                [302, 61, 95, 76],
                [302, 63, 95, 74],
                [302, 51, 96, 86],
                [302, 66, 98, 71],
                [304, 67, 96, 69],
                [301, 63, 96, 74],
                [301, 58, 93, 79],
                [291, 52, 94, 85],
                [288, 50, 71, 88],
                [285, 43, 76, 95],
                [285, 37, 70, 101],
                [281, 29, 55, 109],
                [278, 20, 58, 119],
                [278, 20, 55, 119, 1],
                [277, 12, 121, 127],
                [271, 2, 122, 138],
                [267, 1, 126, 139],
                [264, 0, 136, 140],
                [260, 0, 141, 140],
                [255, 0, 148, 140],
                [252, 0, 151, 140],
                [249, 2, 121, 138],
                [247, 3, 123, 137],
                [246, 3, 123, 137],
                [246, 2, 124, 137],
                [258, 2, 112, 137],
                [263, 2, 106, 137],
                [263, 2, 106, 137],
                [262, 2, 103, 137],
                [260, 2, 104, 136],
                [260, 2, 104, 137, 1],
                [268, 2, 98, 137],
                [267, 2, 99, 137],
                [266, 2, 97, 137],
                [266, 3, 96, 136],
                [264, 3, 99, 136],
                [263, 3, 100, 136],
                [261, 3, 100, 136],
                [259, 2, 138, 137],
                [254, 2, 126, 137],
                [247, 2, 101, 136],
                [240, 2, 108, 136],
                [238, 1, 110, 137],
                [230, 1, 118, 138],
                [220, 15, 128, 124],
                [211, 18, 137, 121],
                [205, 43, 102, 96],
                [202, 45, 104, 93],
                [200, 38, 97, 101],
                [198, 38, 104, 101, 1],
                [197, 39, 107, 100],
                [197, 39, 112, 100],
                [213, 39, 94, 110],
                [212, 40, 95, 111],
                [211, 41, 97, 111],
                [209, 42, 99, 112],
                [209, 43, 98, 112],
                [213, 43, 87, 112],
                [213, 42, 83, 113],
                [211, 40, 86, 109],
                [211, 38, 86, 103],
                [211, 37, 88, 112],
                [211, 20, 186, 131],
                [213, 27, 167, 122],
                [212, 44, 87, 105],
                [210, 44, 88, 98],
                [195, 44, 106, 98],
                [189, 44, 110, 98],
                [182, 46, 117, 99],
                [173, 44, 118, 96, 1],
                [161, 43, 130, 99],
                [154, 42, 137, 97],
                [153, 42, 137, 97],
                [153, 42, 137, 97],
                [152, 41, 137, 98],
                [151, 41, 137, 97],
                [149, 41, 145, 97],
                [148, 25, 144, 114],
                [148, 13, 144, 126],
                [141, 12, 153, 127],
                [115, 11, 173, 128],
                [108, 7, 180, 133],
                [108, 4, 180, 136],
                [108, 3, 176, 137, 1],
                [108, 1, 161, 139],
                [105, 1, 235, 138],
                [103, 1, 295, 148],
                [103, 0, 277, 149],
                [108, 0, 234, 137],
                [101, 0, 232, 137],
                [99, 0, 135, 139],
                [95, 0, 244, 139],
                [81, 0, 152, 139],
                [69, 0, 164, 139, 1],
                [66, 0, 169, 139],
                [65, 0, 170, 139],
                [63, 0, 168, 138],
                [61, 0, 159, 138],
                [35, 0, 304, 139],
                [19, 0, 189, 140],
                [18, 11, 138, 129],
                [18, 11, 137, 129],
                [18, 11, 137, 128],
                [18, 6, 135, 133],
                [7, 4, 146, 136],
                [6, 4, 147, 136],
                [3, 4, 150, 136, 1],
                [3, 5, 150, 135],
                [3, 8, 150, 132],
                [4, 6, 394, 145],
                [12, 6, 388, 145],
                [11, 8, 389, 144],
                [11, 8, 387, 144],
                [11, 8, 387, 143, 1],
                [10, 8, 113, 131],
                [11, 8, 111, 131],
                [10, 9, 112, 130],
                [12, 9, 116, 130],
                [12, 9, 111, 130],
                [12, 9, 111, 130],
                [12, 9, 110, 131],
                [12, 34, 113, 106],
                [13, 35, 110, 104]
            ],
            e = d.length,
            f, g, h, i, j = -1,
            c = document.getElementById("graham-logo"),
            l = function () {
                var a = d[f];
                if (c && a[0]) {
                    var b = document.createElement("div");
                    b.id = "graham-logo" + f;
                    b.style.position = "absolute";
                    b.style.left = a[0] + "px";
                    b.style.top = a[1] + "px";
                    b.style.width = a[2] + "px";
                    b.style.height = a[3] + "px";
                    b.style.background = "url(/images/css-sprite/graham-sprite.png) no-repeat " + -g + "px " + -h + "px";
                    a[3] > i && (i = a[3]);
                    a[4] ? (g = 0, h += i, i = 0) : g += a[2];
                    c.appendChild(b);
                    ++f;
                    f < e && (j = window.setTimeout(l, 83))
                }
            },
            m = function () {
                google.doodle.a = !1;
                i = h = g = f = 0;
                j != -1 && (window.clearTimeout(j), j = -1);
                c.innerHTML = "";
                j = window.setTimeout(l, 83)
            };
            c.addEventListener ? c.addEventListener("click", m, !1) : c.attachEvent("onclick", m);
        if (!google.doodle.a) {
            google.doodle.a = !0;
            var n = document.createElement("img");
            n.addEventListener ? n.addEventListener("load", m, !1) : n.attachEvent("onload", m);
            n.src = "/images/css-sprite/graham-sprite.png"
        }
})();

总结

希望大家可以通过本文的Google动画实现了解到一些CSS Sprite技术,CSS Sprite是前端优化的一部分,合理的利用好可以提高网页的性能,也可以丰富网页内容。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • [css sprite原理优缺点及使用] CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方...
    LabRaDor2079阅读 1,505评论 0 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 这是一首歌的歌词,写的真好!越长大越孤单,已经是大龄青年的我越来越不想长大!可是我还是悄无声息地长到了27岁,...
    石小心2016阅读 214评论 0 1
  • 《永不抵達的列車》是趙涵漠的成名作。說起來,趙涵漠不是中青冰點第一個寫特稿的記者,自始至終,這個欄目的稿件都延續了...
    Elaine_Liu阅读 276评论 0 0
  • 三位建筑师RCR建筑事务所,在他们近30年的职业生涯中,通过作品说明了“对场地的一种坚强隐忍的叙述性”。通过协调运...
    孟yueling阅读 1,039评论 1 4