vue界面发送表情的实现代码


Posted in Javascript onSeptember 11, 2020

完全照搬不一定能写出来 只是让看个思想

<template>  
 <section class="dialogue-section clearfix" >
      <div class="row clearfix" v-for="(item,index) in msgs" :key = index>
        <img :src="item.uid == myInfo.uid ? myInfo.avatar :otherInfo.avatar" :class="item.uid == myInfo.uid ? 'headerleft' : 'headerright'">
        <p :class="item.uid == myInfo.uid ? '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" style="width:30px; height:30px;">')
  },
  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": "?",
  "name": "grinning face"
 },
 {
  "codes": "1F603",
  "char": "?",
  "name": "grinning face with big eyes"
 },
 {
  "codes": "1F604",
  "char": "?",
  "name": "grinning face with smiling eyes"
 },
 {
  "codes": "1F601",
  "char": "?",
  "name": "beaming face with smiling eyes"
 },
 {
  "codes": "1F606",
  "char": "?",
  "name": "grinning squinting face"
 },
 {
  "codes": "1F605",
  "char": "?",
  "name": "grinning face with sweat"
 },
 {
  "codes": "1F923",
  "char": "?",
  "name": "rolling on the floor laughing"
 },
 {
  "codes": "1F602",
  "char": "?",
  "name": "face with tears of joy"
 },
 {
  "codes": "1F642",
  "char": "?",
  "name": "slightly smiling face"
 },
 {
  "codes": "1F643",
  "char": "?",
  "name": "upside-down face"
 },
 {
  "codes": "1F609",
  "char": "?",
  "name": "winking face"
 },
 {
  "codes": "1F60A",
  "char": "?",
  "name": "smiling face with smiling eyes"
 },
 {
  "codes": "1F607",
  "char": "?",
  "name": "smiling face with halo"
 },
 {
  "codes": "1F970",
  "char": "?",
  "name": "smiling face with hearts"
 },
 {
  "codes": "1F60D",
  "char": "?",
  "name": "smiling face with heart-eyes"
 },
 {
  "codes": "1F929",
  "char": "?",
  "name": "star-struck"
 },
 {
  "codes": "1F618",
  "char": "?",
  "name": "face blowing a kiss"
 },
 {
  "codes": "1F617",
  "char": "?",
  "name": "kissing face"
 },
 {
  "codes": "1F61A",
  "char": "?",
  "name": "kissing face with closed eyes"
 },
 {
  "codes": "1F619",
  "char": "?",
  "name": "kissing face with smiling eyes"
 },
 {
  "codes": "1F44B",
  "char": "?",
  "name": "waving hand"
 },
 {
  "codes": "1F91A",
  "char": "?",
  "name": "raised back of hand"
 },
 {
  "codes": "1F590",
  "char": "?",
  "name": "hand with fingers splayed"
 },
 {
  "codes": "270B",
  "char": "✋",
  "name": "raised hand"
 },
 {
  "codes": "1F596",
  "char": "?",
  "name": "vulcan salute"
 },
 {
  "codes": "1F44C",
  "char": "?",
  "name": "OK hand"
 },
 {
  "codes": "1F90F",
  "char": "?",
  "name": "pinching hand"
 },
 {
  "codes": "270C",
  "char": "✌",
  "name": "victory hand"
 },
 {
  "codes": "1F91E",
  "char": "?",
  "name": "crossed fingers"
 },
 {
  "codes": "1F91F",
  "char": "?",
  "name": "love-you gesture"
 },
 {
  "codes": "1F918",
  "char": "?",
  "name": "sign of the horns"
 },
 {
  "codes": "1F919",
  "char": "?",
  "name": "call me hand"
 },
 {
  "codes": "1F448",
  "char": "?",
  "name": "backhand index pointing left"
 },
 {
  "codes": "1F449",
  "char": "?",
  "name": "backhand index pointing right"
 },
 {
  "codes": "1F446",
  "char": "?",
  "name": "backhand index pointing up"
 },
 {
  "codes": "1F595",
  "char": "?",
  "name": "middle finger"
 },
 {
  "codes": "1F447",
  "char": "?",
  "name": "backhand index pointing down"
 },
 {
  "codes": "261D FE0F",
  "char": "☝️",
  "name": "index pointing up"
 },
 {
  "codes": "1F44D",
  "char": "?",
  "name": "thumbs up"
 },
 {
  "codes": "1F44E",
  "char": "?",
  "name": "thumbs down"
 },
 {
  "codes": "270A",
  "char": "✊",
  "name": "raised fist"
 },
 {
  "codes": "1F44A",
  "char": "?",
  "name": "oncoming fist"
 },
 {
  "codes": "1F91B",
  "char": "?",
  "name": "left-facing fist"
 },
 {
  "codes": "1F91C",
  "char": "?",
  "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界面发送表情的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
一种JavaScript的设计模式
Nov 22 Javascript
ASP.NET中基于JQUERY的高性能的TreeView补充
Feb 23 Javascript
JS实现根据出生年月计算年龄
Jan 10 Javascript
jQuery使用after()方法在元素后面添加多项内容的方法
Mar 26 Javascript
基于jquery css3实现点击动画弹出表单源码特效
Aug 31 Javascript
jQuery循环遍历子节点并获取值的方法
Apr 14 Javascript
老司机带你解读jQuery插件开发流程
May 16 Javascript
JavaScript基于对象去除数组重复项的方法
Oct 09 Javascript
浅谈Node.js CVE-2017-14849 漏洞分析(详细步骤)
Nov 10 Javascript
angularjs实现的购物金额计算工具示例
May 08 Javascript
thinkjs微信中控之微信鉴权登陆的实现代码
Aug 08 Javascript
Vue作用域插槽实现方法及作用详解
Jul 08 Javascript
在vue中配置不同的代理同时访问不同的后台操作
Sep 11 #Javascript
vscode中的vue项目报错Property ‘xxx‘ does not exist on type ‘CombinedVueInstance<{ readyOnly...Vetur(2339)
Sep 11 #Javascript
Vue proxyTable配置多个接口地址,解决跨域的问题
Sep 11 #Javascript
关于ES6尾调用优化的使用
Sep 11 #Javascript
在js文件中引入(调用)另一个js文件的三种方法
Sep 11 #Javascript
Vue项目开发常见问题和解决方案总结
Sep 11 #Javascript
JavaScript实现简单日历效果
Sep 11 #Javascript
You might like
php使用Session和文件统计在线人数
2015/07/04 PHP
Laravel 实现在Blade模版中使用全局变量代替路径的例子
2019/10/22 PHP
PHP程序员简单的开展服务治理架构操作详解(三)
2020/05/14 PHP
jQuery 学习6 操纵元素显示效果的函数
2010/02/07 Javascript
基于jQuery的可以控制左右滚动及自动滚动效果的代码
2010/07/25 Javascript
为Extjs加加速(javascript加速)
2010/08/19 Javascript
JQuery.closest(),parent(),parents()寻找父结点
2012/02/17 Javascript
利用ajaxfileupload插件实现文件上传无刷新的具体方法
2013/06/08 Javascript
js加减乘除丢失精度问题解决方法
2014/05/16 Javascript
Node.js安装教程和NPM包管理器使用详解
2014/08/16 Javascript
基于jquery ui的alert,confirm方案(支持换肤)
2015/04/03 Javascript
jQuery插件expander实现图片翻转特效
2015/05/21 Javascript
JS日期加减,日期运算代码
2015/11/05 Javascript
基于Jquery插件Uploadify实现实时显示进度条上传图片
2020/03/26 Javascript
js实现本地时间同步功能
2017/08/26 Javascript
Vue cli构建及项目打包以及出现的问题解决
2018/08/27 Javascript
JavaScript中变量提升和函数提升的详解
2020/08/07 Javascript
vue从后台渲染文章列表以及根据id跳转文章详情详解
2020/12/14 Vue.js
[00:52]DOTA2第二届亚洲邀请赛预选赛宣传片
2017/01/13 DOTA
[04:51]TI10典藏宝瓶Ⅱ外观视频展示
2020/08/15 DOTA
python实现zencart产品数据导入到magento(python导入数据)
2014/04/03 Python
python使用多线程不断刷新网页的方法
2015/03/31 Python
Python装饰器入门学习教程(九步学习)
2016/01/28 Python
python利用正则表达式排除集合中字符的功能示例
2017/10/10 Python
python使用循环打印所有三位数水仙花数的实例
2018/11/13 Python
用python标准库difflib比较两份文件的异同详解
2018/11/16 Python
在python中实现强制关闭线程的示例
2019/01/22 Python
Html5实现首页动态视频背景的示例代码
2019/09/25 HTML / CSS
Html5 video标签视频的最佳实践
2020/02/26 HTML / CSS
Kendra Scott官网:美国领先的时尚配饰品牌
2020/10/22 全球购物
医学生求职自荐信
2013/10/25 职场文书
回门宴新郎答谢词
2014/01/12 职场文书
销售主管岗位职责
2014/02/08 职场文书
四风问题民主生活会对照检查材料思想汇报
2014/09/27 职场文书
85句关于理想的名言警句大全
2019/08/22 职场文书
这样写python注释让代码更加的优雅
2021/06/02 Python