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 相关文章推荐
JS中typeof与instanceof之间的区别总结
Nov 14 Javascript
Jquery 点击按钮自动高亮实现原理及代码
Apr 25 Javascript
javascript基于DOM实现省市级联下拉框的方法
May 14 Javascript
解决angular的$http.post()提交数据时后台接收不到参数值问题的方法
Dec 10 Javascript
JavaScript的Vue.js库入门学习教程
May 23 Javascript
js实现文字无缝向上滚动
Feb 16 Javascript
jQuery实现的简单拖动层示例
Feb 22 Javascript
详解使用Visual Studio Code对Node.js进行断点调试
Sep 14 Javascript
React Native中NavigatorIOS组件的简单使用详解
Jan 27 Javascript
JS实现的base64加密解密操作示例
Apr 18 Javascript
vue实现pdf文档在线预览功能
Nov 26 Javascript
JavaScript逻辑运算符相关总结
Sep 04 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无敌近乎加密方式!
2010/07/17 PHP
Chrome Web App开发小结
2014/09/04 PHP
php使用环形链表解决约瑟夫问题完整示例
2018/08/07 PHP
Laravel自动生成UUID,从建表到使用详解
2019/10/24 PHP
php中数组最简单的使用方法
2020/12/27 PHP
Javascript实例教程(19) 使用HoTMetal(3)
2006/12/23 Javascript
利用javascript实现web页面中指定区域打印
2013/10/30 Javascript
jQuery Mobile 导航栏代码
2013/11/01 Javascript
jquery 检测元素是否存在的实例代码
2013/11/19 Javascript
JavaScript中数组的合并以及排序实现示例
2015/10/24 Javascript
jQuery.trim() 函数及trim()用法详解
2015/10/26 Javascript
JavaScript实现点击按钮字体放大、缩小
2016/02/29 Javascript
web.js.字符串与正则表达式操作
2017/05/13 Javascript
基于BootStrap multiselect.js实现的下拉框联动效果
2017/07/28 Javascript
JS实现下拉菜单列表与登录注册弹窗效果
2017/08/10 Javascript
vue解决跨域路由冲突问题思路解析
2017/11/03 Javascript
微信小程序url传参写变量的方法
2018/08/09 Javascript
详解在不使用ssr的情况下解决Vue单页面SEO问题
2018/11/08 Javascript
基于JavaScript或jQuery实现网站夜间/高亮模式
2020/05/30 jQuery
利用PHP实现递归删除链表元素的方法示例
2020/10/23 Javascript
[01:35]辉夜杯战队访谈宣传片—LGD
2015/12/25 DOTA
[08:06]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Elephant 选手采访
2021/03/11 DOTA
python调用shell的方法
2013/11/20 Python
基于python实现在excel中读取与生成随机数写入excel中
2018/01/04 Python
快速了解python leveldb
2018/01/18 Python
pytorch神经网络之卷积层与全连接层参数的设置方法
2019/08/18 Python
Django中使用haystack+whoosh实现搜索功能
2019/10/08 Python
python中如何使用insert函数
2020/01/09 Python
python:删除离群值操作(每一行为一类数据)
2020/06/08 Python
Python创建临时文件和文件夹
2020/08/05 Python
使用py-spy解决scrapy卡死的问题方法
2020/09/29 Python
如何基于Python爬虫爬取美团酒店信息
2020/11/03 Python
aec加密 php_php aes加密解密类(兼容php5、php7)
2021/03/14 PHP
领导干部廉政自律承诺书
2014/05/26 职场文书
单位租房协议书样本
2014/10/30 职场文书
CSS实现五种常用的2D转换
2021/12/06 HTML / CSS