美文网首页
uni-app之表情

uni-app之表情

作者: Cute_小肥鸡 | 来源:发表于2020-11-20 20:04 被阅读0次

1、表情插件JS

图1
<template>
    <view class="emotion-box">
        <swiper class="swiper" disable-programmatic-animation="true" indicator-dots="true" style="height: 360rpx;">
            <swiper-item class="emotion-box-line">
                <view v-for="(line, i) in list1" v-bind:key="i">
                    <view v-for="(im, ix) in line" v-bind:key="ix" class="swiper-item emotion-item" :style="{width:img_width+'rpx'}"
                     @click="clickHandler(im)">
                        <rich-text :nodes="im.toString().replace(/^([1-9]\d?|97)$/g, emotion(im))"></rich-text>
                    </view>
                </view>
            </swiper-item>
            <swiper-item class="emotion-box-line">
                <view v-for="(line, i) in list2" v-bind:key="i">
                    <view v-for="(im, ix) in line" v-bind:key="ix" class="swiper-item emotion-item" :style="{width:img_width+'rpx'}"
                     @click="clickHandler(im)">
                        <rich-text :nodes="im.toString().replace(/^([1-9]\d?|97)$/g, emotion(im))"></rich-text>
                    </view>
                </view>
            </swiper-item>
            <swiper-item class="emotion-box-line">
                <view v-for="(line, i) in list3" v-bind:key="i">
                    <view v-for="(im, ix) in line" v-bind:key="ix" class="swiper-item emotion-item" :style="{width:img_width+'rpx'}"
                     @click="clickHandler(im)">
                        <rich-text :nodes="im.toString().replace(/^([1-9]\d?|97)$/g, emotion(im))"></rich-text>
                    </view>
                </view>
            </swiper-item>
            <swiper-item class="emotion-box-line">
                <view v-for="(line, i) in list4" v-bind:key="i">
                    <view v-for="(im, ix) in line" v-bind:key="ix" class="swiper-item emotion-item" :style="{width:img_width+'rpx'}"
                     @click="clickHandler(im)">
                        <rich-text :nodes="im.toString().replace(/^([1-9]\d?|97)$/g, emotion(im))"></rich-text>
                    </view>
                </view>
            </swiper-item>
            <swiper-item class="emotion-box-line">
                <view v-for="(line, i) in list5" v-bind:key="i">
                    <view v-for="(im, ix) in line" v-bind:key="ix" class="swiper-item emotion-item" :style="{width:img_width+'rpx'}"
                     @click="clickHandler(im)">
                        <rich-text :nodes="im.toString().replace(/^([1-9]\d?|97)$/g, emotion(im))"></rich-text>
                    </view>
                </view>
            </swiper-item>
        </swiper>
    </view>
</template>

<script>
    export default {
        props: {

        },

        data() {
            return {
                // 画矩阵
                list1: [
                    [1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9],
                    [10, 11, 12],
                    [13, 14, 15],
                    [16, 17, 18],
                    [19, 20, 21],
                    [22, 23, 98]
                ],
                list2: [
                    [25, 26, 27],
                    [28, 29, 30],
                    [31, 32, 33],
                    [34, 35, 36],
                    [37, 38, 39],
                    [40, 41, 42],
                    [43, 44, 45],
                    [46, 47, 98]
                ],
                list3: [
                    [49, 50, 51],
                    [52, 53, 54],
                    [55, 56, 57],
                    [58, 59, 60],
                    [61, 62, 63],
                    [64, 65, 66],
                    [67, 68, 69],
                    [70, 71, 98]
                ],
                list4: [
                    [73, 74, 75],
                    [76, 77, 78],
                    [79, 80, 81],
                    [82, 83, 84],
                    [85, 86, 87],
                    [88, 89, 90],
                    [91, 92, 93],
                    [94, 95, 98]
                ],
                list5: [
                    [24],
                    [48],
                    [72],
                    [96],
                    [97],
                    [98]
                ],
                item: [],
                img_width: 0
            }
        },
        mounted() {
            // #ifdef APP-PLUS || H5
            const query = uni.createSelectorQuery().in(this);
            query.select('.emotion-box').boundingClientRect(data => {
                console.log(data.width / 8);
                this.img_width = data.width / 8;
            }).exec();
            // #endif
            // #ifdef MP-WEIXIN
            // this.img_width = 22;
            this.img_width = 92;
            // #endif
        },
        methods: {

            clickHandler(i) {
                let emotion = `[em_${i}]`
                this.$emit('emotion', emotion);
            },
            emotion(res) {
                const list = Array.from({
                    length: 100
                }, (v, k) => k)
                let index = list.indexOf(res)
                //  请将此处地址更换为自己服务器的地址
                return `<img src="http://localhost/statics/images/bkhumor-emoji/${index}.gif" align="middle">`
            }
        }
    }
</script>

<style scoped>
    .emotion-box {
        height: 300px;
        margin: 0 auto;
        width: 750rpx;
        box-sizing: border-box;
        padding-top: 8upx;
        overflow: hidden;
        background: white;
    }

    .emotion-box-line {
        display: flex;
    }

    .emotion-item {
        flex: 1;
        text-align: center;
        cursor: pointer;
        padding: 30upx;
    }
</style>

2、在需要表情插件的地方,引用插件 并 使用插件

图2
<template>
    <view>
        <emotion @emotion="handleEmj"></emotion>
    </view>
</template>

<script>
    import emotion from '@/components/bkhumor-emoji/index.vue';

    export default {
        components: {
            emotion
        },
        data() {
            return {
                HDComTextareaContent: '' //textarea框的值
            };
        },
        onShow() {
            // 未登录则退到登录口
            this.goLogin();
        },
        onLoad(e) {
        
        },
        methods: {
            handleEmj(i) {
                if (i == '[em_98]') {
                    //匹配最后一个表情符号并删除。
                    this.HDComTextareaContent = this.HDComTextareaContent.replace(/(\[[^\]]+\]|[\s\S])$/, '');
                } else {
                    this.HDComTextareaContent += i;
                }
            }
        }
    }
</script>

<style>
</style>
效果图

相关文章

  • uni-app之表情

    1、表情插件JS 2、在需要表情插件的地方,引用插件 并 使用插件

  • 2019-03-20

    9 多端开发之uni-app起步 1.在HBuilderX中新建uni-app 2.项目初始目录结构 4.配置pa...

  • Uni-App学习路线

    Uni-App学习路线 学习Uni-app用到的技术 Uni-app官网[https://uniapp.dclou...

  • uni-app 介绍

    uni-app 介绍 1、什么是uni-app? uni-app是一个使用 Vue.js 开发所有前端应用的框架,...

  • uni-app学习:CSS之flex布局居中

    uni-app学习:CSS之flex布局居中 1、上下居中(垂直居中) style="height:40px; b...

  • uni-app入门详解

    1 uni-app学习 1.1 什么是uni-app uni-app是一个使用Vue.js语法来开发所有前端应用的...

  • uni-app之scroll的triggered作用

    关于 uni-app之scroll 的 下拉刷新 和 上拉加载,理解其中triggered的作用。 参考链接: h...

  • Uni-app之数据存储

    storage 2.globalData定义 使用 3.vuex可查看之前的简书Uni-app / Vue之vue...

  • uni-app开发踩坑集合(会持续更新)

    uni-app官网指路 uni-app开发中的坑 1.无法覆盖uni-app提供的组件的样式 直接重写样式会发现并...

  • uni-app 之登录页面

    学习了一周多uni-app,上一周主要是开发原生插件给uni-app调用。本周开始学习怎么使用uni-app写页面...

网友评论

      本文标题:uni-app之表情

      本文链接:https://www.haomeiwen.com/subject/dqjjiktx.html