CSS雪碧图制作emoji表情包

效果图:
CSS雪碧图制作emoji表情包_第1张图片

原理:利用雪碧图,来实现emoji表情包,减少图片请求数。
表情包转换为文字 & 文字转换为表情包 维护两个对象来实现。

缺点:表情包大小由雪碧图中表情包大小控制,不能更改。

htm代码


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>emotitle>
    <link rel="stylesheet" href="emoji.css">
    <style>
        ul,li {
            padding: 0;
            margin: 0;
            list-style: none;
        }
        #emo_container {
            width: 280px;
            display: flex;
            flex-wrap: wrap;
        }
        #emo_container li {
            width: 70px;
            height: 70px;
            margin-bottom: 10px;
            cursor: pointer;
            position: relative;
        }
        #emo_container li span {
            display: none;
            position: absolute;
            width: 100%;
            bottom: -10px;
            font-size: 12px;
            text-align: center;
        }
        #emo_container li:hover span {
            display: block;
        }
        #emo_input {
            margin-top: 50px;
        }
        .emo-span {
            display: inline-block;
            width: 70px;
            height: 70px;
            vertical-align: middle;
        }
        .show {
            min-height: 200px;
            margin-top: 100px;
            border: 1px solid;
        }
    style>
head>
<body>
    <ul id="emo_container">ul>
    <input type="text" id="emo_input"/>
    <button id="send_msg">sendbutton>
    <div class="show">div>
body>
<script src="jquery-2.1.4.js">script>
<script>
    $(function(){
        let li_html = "";
        let input = "";
        let input_dom = $("#emo_input")[0];
        let emotions = {
            "1":"[微笑]",
            "2":"[撇嘴]",
            "3":"[色]",
            "4":"[发呆]",
            "5":"[得意]",
            "6":"[流泪]",
            "7":"[害羞]",
            "8":"[闭嘴]"
        };
        let emo_index = {
            "[微笑]":"1",
            "[撇嘴]":"2",
            "[色]":"3",
            "[发呆]":"4",
            "[得意]":"5",
            "[流泪]":"6",
            "[害羞]":"7",
            "[闭嘴]":"8"
        };
        //渲染表情包
        Object.keys(emotions).map(function (value,index) {
            li_html += `
  • class="bg-${value}" data-emo="${value}"> <span>${emotions[value]}span> li>`; }); $("#emo_container").html(li_html); //预览输入的文字 $("#emo_container").on("click","li",function () { let emo = $(this).data("emo"); input = $("#emo_input").val(); insert(emotions[emo]); //确保插入表情后光标在末尾 input_dom.focus(); }); //界面展示输入的内容 $("#send_msg").click(function () { input = $("#emo_input").val(); let emo_input = renderEmo(input); $(".show").append(`<div>${emo_input}div>`); $("#emo_input").val(""); }); //表情文字替换为表情图片 function renderEmo(text) { let pattern = /\[[\u4e00-\u9fa5]+\]/g; return text.replace(pattern,function (match) { return `<span class="emo-span bg-${emo_index[match]}">span>` }) } //文字中间插入表情 function insert(str) { var value = input_dom.value; if(typeof document.selection != "undefined") { document.selection.createRange().text = str; } else { return input_dom.value = value.substr(0,input_dom.selectionStart)+str+value.substring(input_dom.selectionStart,value.length); } } }) script> html>
  • CSS代码

    .bg-1 {
        width: 66px; height: 66px;
        background: url('emoji.png') -10px -10px;
    }
    .bg-2 {
        width: 66px; height: 66px;
        background: url('emoji.png') -96px -10px;
    }
    .bg-3 {
        width: 66px; height: 66px;
        background: url('emoji.png') -10px -96px;
    }
    .bg-4 {
        width: 66px; height: 66px;
        background: url('emoji.png') -96px -96px;
    }
    .bg-5 {
        width: 66px; height: 66px;
        background: url('emoji.png') -182px -10px;
    }
    .bg-6 {
        width: 66px; height: 66px;
        background: url('emoji.png') -182px -96px;
    }
    .bg-7 {
        width: 66px; height: 66px;
        background: url('emoji.png') -10px -182px;
    }
    .bg-8 {
        width: 66px; height: 66px;
        background: url('emoji.png') -96px -182px;
    }

    你可能感兴趣的:(css技巧)