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

 更新时间:2020年09月11日 12:37:40   作者:前端阿龙  
这篇文章主要介绍了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 ? 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界面发送表情的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue.js iview打包上线后字体图标不显示解决办法

    vue.js iview打包上线后字体图标不显示解决办法

    这篇文章主要介绍了vue.js iview打包上线后字体图标不显示解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • vue实现虚拟滚动渲染成千上万条数据

    vue实现虚拟滚动渲染成千上万条数据

    本文主要介绍了vue实现虚拟滚动渲染成千上万条数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Vue中使用的EventBus有生命周期

    Vue中使用的EventBus有生命周期

    这篇文章主要介绍了Vue中使用的EventBus有生命周期的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • vite+vue3项目解决低版本兼容性问题解决方案(Safari白屏)

    vite+vue3项目解决低版本兼容性问题解决方案(Safari白屏)

    这篇文章主要介绍了vite+vue3项目解决低版本兼容性问题(Safari白屏),使用官方插件 @vitejs/plugin-legacy 为打包后的文件提供传统浏览器兼容性支持,本文给大家介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • vue手写<RouterLink/>组件实现demo详解

    vue手写<RouterLink/>组件实现demo详解

    这篇文章主要为大家介绍了vue手写<RouterLink/>组件实现demo详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • ant-design-vue Table pagination分页实现全过程

    ant-design-vue Table pagination分页实现全过程

    这篇文章主要介绍了ant-design-vue Table pagination分页实现全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Vue页面首次载入优化的全过程

    Vue页面首次载入优化的全过程

    凡是做SPA的项目,特别是移动端的SAP项目,首屏加载速度必定是一个绕不过去的话题,下面这篇文章主要给大家介绍了关于Vue页面首次载入优化的相关资料,需要的朋友可以参考下
    2021-12-12
  • Vue2.x Todo之自定义指令实现自动聚焦的方法

    Vue2.x Todo之自定义指令实现自动聚焦的方法

    我们希望用户双击 todo 进入编辑状态后输入框自动获取焦点,而不是需要先手动点一下。这篇文章主要介绍了Vue 2.x Todo之自定义指令实现自动聚焦,非常具有实用价值,需要的朋友可以参考下
    2019-01-01
  • element-ui 中的table的列隐藏问题解决

    element-ui 中的table的列隐藏问题解决

    这篇文章主要介绍了element-ui 中的table的列隐藏问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Vue3在jsx下父子组件实现使用插槽方式

    Vue3在jsx下父子组件实现使用插槽方式

    这篇文章主要介绍了Vue3在jsx下父子组件实现使用插槽方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10

最新评论