温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue实现在界面发送表情

发布时间:2020-11-03 19:33:16 来源:亿速云 阅读:205 作者:Leah 栏目:开发技术

vue实现在界面发送表情?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<template>  
 <section class="dialogue-section clearfix" >
      <div class="row clearfix" v-for="(item,index) in msgs" :key = index>
        <img :src="item.uid == myInfo.uid &#63; myInfo.avatar :otherInfo.avatar" :class="item.uid == myInfo.uid &#63; 'headerleft' : 'headerright'">
        <p :class="item.uid == myInfo.uid &#63; 'textleft' : 'textright'" v-html="customEmoji(item.content)"></p>
      </div>
    </section>
     <div id="emoji-list" class="flex-column" v-if="emojiShow"> //首先根据这个来判断发送表情弹窗用不用出现
       <div class="flex-cell flex-row" v-for="list in imgs">
        <div class="flex-cell flex-row cell" v-for="item in list" @click="inputEmoji(item)">
         <img :src="'./emoji/'+ item + '.png'">
        </div>
       </div>
      </div>
</template>
<script>
import { sendMsg } from "@/ws"; //是一个长连接
import _ from "lodash";//这个是js一个很强大的库 
import eventBus from '@/eventBus'//这是一个子父传递的公共文件
console.log(emoji)
export default {
 data() {
  this.imgs = _.chunk(emoji, 6) //这个是调用lodash库的chunk方法 把 六个元素分成一个数组只不过是emoji这个数组中的二维数组
  return {
   emojiShow: false //刚开始默认不显示 点击按钮显示 点击的按钮上可以写@click='emojiShow=emojiShow'这种写法
  };
 },
 methods: {
  customEmoji(text) { //这个函数就是服务器端把传过来的名称转化为图片的
   return text.replace(/\[([A-Za-z0-9_]+)\]/g, '<img src="./emoji/$1.png" >')
  },
  inputEmoji(pic) {
   this.content += `[${pic}]`//传过来的名字转为图片
  }
};
</script>
<style scoped>
@import '../../assets/css/dialogue.css';

#emoji-list {
 height: 230px;
 background: #fff;
}
#emoji-list .cell {
 line-height: 13vh;
 border-right: 1rpx solid #ddd;
 border-bottom: 1rpx solid #ddd;
}
.flex-row {
 display: flex;
 flex-direction: row;
 justify-content: center;
 align-items: center;
}
.flex-column {
 display: flex;
 flex-direction: column;
 justify-content: center;
 align-items: stretch;
}
.flex-cell {
 flex: 1;
}
.cell img {
 width: 35px;
 height: 35px;
}
</style>

补充知识:vue+element-ui实现聊天表情包

我是用的本地json数据实现的,表情不是很多,首先创建个json文件,代码如下:

[{
  "codes": "1F600",
  "char": "&#128512;",
  "name": "grinning face"
 },
 {
  "codes": "1F603",
  "char": "&#128515;",
  "name": "grinning face with big eyes"
 },
 {
  "codes": "1F604",
  "char": "&#128516;",
  "name": "grinning face with smiling eyes"
 },
 {
  "codes": "1F601",
  "char": "&#128513;",
  "name": "beaming face with smiling eyes"
 },
 {
  "codes": "1F606",
  "char": "&#128518;",
  "name": "grinning squinting face"
 },
 {
  "codes": "1F605",
  "char": "&#128517;",
  "name": "grinning face with sweat"
 },
 {
  "codes": "1F923",
  "char": "&#129315;",
  "name": "rolling on the floor laughing"
 },
 {
  "codes": "1F602",
  "char": "&#128514;",
  "name": "face with tears of joy"
 },
 {
  "codes": "1F642",
  "char": "&#128578;",
  "name": "slightly smiling face"
 },
 {
  "codes": "1F643",
  "char": "&#128579;",
  "name": "upside-down face"
 },
 {
  "codes": "1F609",
  "char": "&#128521;",
  "name": "winking face"
 },
 {
  "codes": "1F60A",
  "char": "&#128522;",
  "name": "smiling face with smiling eyes"
 },
 {
  "codes": "1F607",
  "char": "&#128519;",
  "name": "smiling face with halo"
 },
 {
  "codes": "1F970",
  "char": "&#129392;",
  "name": "smiling face with hearts"
 },
 {
  "codes": "1F60D",
  "char": "&#128525;",
  "name": "smiling face with heart-eyes"
 },
 {
  "codes": "1F929",
  "char": "&#129321;",
  "name": "star-struck"
 },
 {
  "codes": "1F618",
  "char": "&#128536;",
  "name": "face blowing a kiss"
 },
 {
  "codes": "1F617",
  "char": "&#128535;",
  "name": "kissing face"
 },
 {
  "codes": "1F61A",
  "char": "&#128538;",
  "name": "kissing face with closed eyes"
 },
 {
  "codes": "1F619",
  "char": "&#128537;",
  "name": "kissing face with smiling eyes"
 },
 {
  "codes": "1F44B",
  "char": "&#128075;",
  "name": "waving hand"
 },
 {
  "codes": "1F91A",
  "char": "&#129306;",
  "name": "raised back of hand"
 },
 {
  "codes": "1F590",
  "char": "&#128400;",
  "name": "hand with fingers splayed"
 },
 {
  "codes": "270B",
  "char": "&#9995;",
  "name": "raised hand"
 },
 {
  "codes": "1F596",
  "char": "&#128406;",
  "name": "vulcan salute"
 },
 {
  "codes": "1F44C",
  "char": "&#128076;",
  "name": "OK hand"
 },
 {
  "codes": "1F90F",
  "char": "&#129295;",
  "name": "pinching hand"
 },
 {
  "codes": "270C",
  "char": "&#9996;",
  "name": "victory hand"
 },
 {
  "codes": "1F91E",
  "char": "&#129310;",
  "name": "crossed fingers"
 },
 {
  "codes": "1F91F",
  "char": "&#129311;",
  "name": "love-you gesture"
 },
 {
  "codes": "1F918",
  "char": "&#129304;",
  "name": "sign of the horns"
 },
 {
  "codes": "1F919",
  "char": "&#129305;",
  "name": "call me hand"
 },
 {
  "codes": "1F448",
  "char": "&#128072;",
  "name": "backhand index pointing left"
 },
 {
  "codes": "1F449",
  "char": "&#128073;",
  "name": "backhand index pointing right"
 },
 {
  "codes": "1F446",
  "char": "&#128070;",
  "name": "backhand index pointing up"
 },
 {
  "codes": "1F595",
  "char": "&#128405;",
  "name": "middle finger"
 },
 {
  "codes": "1F447",
  "char": "&#128071;",
  "name": "backhand index pointing down"
 },
 {
  "codes": "261D FE0F",
  "char": "&#9757;&#65039;",
  "name": "index pointing up"
 },
 {
  "codes": "1F44D",
  "char": "&#128077;",
  "name": "thumbs up"
 },
 {
  "codes": "1F44E",
  "char": "&#128078;",
  "name": "thumbs down"
 },
 {
  "codes": "270A",
  "char": "&#9994;",
  "name": "raised fist"
 },
 {
  "codes": "1F44A",
  "char": "&#128074;",
  "name": "oncoming fist"
 },
 {
  "codes": "1F91B",
  "char": "&#129307;",
  "name": "left-facing fist"
 },
 {
  "codes": "1F91C",
  "char": "&#129308;",
  "name": "right-facing fist"
 }

]

vue组件中代码直接贴出来,废话不多说:

<template>
 <div class="chatIcon">
  <el-popover
   placement="top-start"
   width="400"
   trigger="click"
   class="emoBox"
  >
   <div class="emotionList">
    <a
     href="javascript:void(0);" rel="external nofollow" 
     @click="getEmo(index)"
     v-for="(item, index) in faceList"
     :key="index"
     class="emotionItem"
     >{{ item }}</a
    >
   </div>
   <el-button
    class="emotionSelect"
    icon="iconfont icon-biaoqing"
    slot="reference"
   ></el-button>
  </el-popover>
  <el-input
   v-model="textarea"
   class="chatText"
   resize="none"
   type="textarea"
   id="textarea"
   rows="5"
   @keyup.enter.native="sendInfo"
  ></el-input>
 </div>
</template>
<script>
const appData = require("@/utils/emoji.json");
export default {
 mounted() {
  for (let i in appData) {
   this.faceList.push(appData[i].char);
  }
 },
 data() {
  return {
   faceList: [],
   textarea: ""
  };
 },
 methods: {
  getEmo(index) {
   var textArea = document.getElementById("textarea");
   function changeSelectedText(obj, str) {
    if (window.getSelection) {
     // 非IE浏览器
     textArea.setRangeText(str);
     // 在未选中文本的情况下,重新设置光标位置
     textArea.selectionStart += str.length;
     textArea.focus();
    } else if (document.selection) {
     // IE浏览器
     obj.focus();
     var sel = document.selection.createRange();
     sel.text = str;
    }
   }
   changeSelectedText(textArea, this.faceList[index]);
   this.textarea = textArea.value; // 要同步data中的数据
   // console.log(this.faceList[index]);
   return;
  }
 }
};
</script>

<style lang="scss">
/* el-popover是和app同级的,所以scoped的局部属性设置了无效 */
/* 需要设置全局style */
.el-popover {
 height: 200px;
 width: 400px;
 overflow: scroll;
 overflow-x: auto;
}
</style>

<style scoped>
.chatIcon {
 padding: 0 10px;
 font-size: 25px;
}
.emotionList {
 display: flex;
 flex-wrap: wrap;
 padding: 5px;
}
.emotionItem {
 width: 10%;
 font-size: 20px;
 text-align: center;
}
/*包含以下四种的链接*/
.emotionItem {
 text-decoration: none;
}
/*正常的未被访问过的链接*/
.emotionItem:link {
 text-decoration: none;
}
/*已经访问过的链接*/
.emotionItem:visited {
 text-decoration: none;
}
/*鼠标划过(停留)的链接*/
.emotionItem:hover {
 text-decoration: none;
}
/* 正在点击的链接*/
.emotionItem:active {
 text-decoration: none;
}
</style>

关于vue实现在界面发送表情问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI