vue2 el-popover性能优化

vue2项目中,在列表等需要循环渲染的地方,使用el-popover时,数据量大了以后,会造成页面卡顿。

解决方案:基于el-popver二次封装

 <template>
    <div class="my-popover-container">
        <span ref="referenceRef" class="comp-reference" @click="triggerPop" @mouseenter="mouseEnter" @mouseleave="mouseLeave">
            <slot name="referenceContent"></slot>
        </span>
        <el-popover
            v-show="showPop"
            ref="hasClosePopoverRef"
            v-bind="$attrs"
            :popper-class="`self-class-close-btn-popover ${popoverClassName}`"
            trigger="manual"
            :append-to-body="true"
            :width="width"
            :placement="placement"
            :reference="reference"
            @show="showPopover"
            @after-leave="hide"
        >
            <div ref="popoverContentRef" class="popover-content">
                <div v-if="title || showClose" class="content-top">
                    <span class="title">{{ title }}</span>
                    <i v-if="showClose" class="el-icon-close" @click="close"></i>
                </div>
                <div v-if="showAfterLoading ? show : true" class="content-center" :style="{ 'max-height': maxContentHeight, ...contentCenterStyle }">
                    <slot></slot>
                </div>
            </div>
        </el-popover>
    </div>
</template>

<script>
export default {
    name: 'MyPopover',
    props: {
        width: {
            type: [Number, String],
            default: 150
        },
        placement: {
            type: String,
            default: 'top-start'
        },
        // 触发方式hover/click
        trigger: {
            type: String,
            default: 'click'
        },
        title: {
            type: String,
            default: ''
        },
        // 内容最大高度, 超出滚动
        maxContentHeight: {
            type: String,
            default: 'auto'
        },
        // 是否需要popover显示后再加载内容
        showAfterLoading: {
            type: Boolean,
            default: false
        },
        // 是否显示关闭按钮
        showClose: {
            type: Boolean,
            default: true
        },
        contentCenterStyle: {
            type: Object,
            default: () => {}
        },
        // 点击弹窗内容,是否关闭弹窗
        isClickClose: {
            type: Boolean,
            default: false
        },
        // popper-class其他类名
        popoverClassName: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            show: false,
            showPop: false,
            reference: null,
            timeOut: null
        }
    },
    watch: {
        showPop(val) {
            this.$emit('show-pop', val)
            if (!val) {
                document.removeEventListener('click', this.handleDocumentClick, true)
                document.removeEventListener('scroll', this.updatePopover, true)
                document.removeEventListener('mousemove', this.handleDocumentMousemove, true)
            }
        }
    },
    beforeDestroy() {
        document.removeEventListener('click', this.handleDocumentClick, true)
        document.removeEventListener('scroll', this.updatePopover, true)
        document.removeEventListener('mousemove', this.handleDocumentMousemove, true)
    },
    methods: {
        mouseEnter() {
            if (this.trigger !== 'hover') return
            this.reference = this.$refs.referenceRef
            this.showPop = true
            this.$nextTick(() => {
                document.addEventListener('scroll', this.updatePopover, true) // 解决popover不跟随触发元素滚动问题
                this.$refs.hasClosePopoverRef.doShow()
                this.$nextTick(() => {
                    this.$refs.hasClosePopoverRef.updatePopper()
                })
            })
        },
        mouseLeave() {
            if (this.trigger !== 'hover') return
            this.$nextTick(() => {
                document.addEventListener('mousemove', this.handleDocumentMousemove, true)
            })
        },
        triggerPop() {
            if (this.trigger !== 'click') return
            this.reference = this.$refs.referenceRef
            // 类似el-select的展开收起效果,需要transition="el-zoom-in-top"
            if (this.isClickClose) {
                if (this.showPop) {
                    this.close()
                    return
                }
            }
            this.showPop = true
            this.$nextTick(() => {
                document.addEventListener('click', this.handleDocumentClick, true)
                document.addEventListener('scroll', this.updatePopover, true) // 解决popover不跟随触发元素滚动问题
                this.$refs.hasClosePopoverRef.doShow()
                this.$nextTick(() => {
                    this.$refs.hasClosePopoverRef.updatePopper()
                })
            })
        },
        close() {
            this.$refs?.hasClosePopoverRef?.doClose()
            this.showPop = false
        },
        showPopover() {
            this.show = true
            this.$nextTick(() => {
                this.$refs.hasClosePopoverRef.updatePopper()
            })
        },
        hide() {
            this.show = false
        },
        handleDocumentClick(e) {
            if (!this.isClickClose) {
                if (!this.$refs.popoverContentRef.contains(e.target)) {
                    this.close()
                }
            } else {
                if (!this.$refs.referenceRef.contains(e.target)) {
                    this.close()
                }
            }
        },
        handleDocumentMousemove(e) {
            clearTimeout(this.timeOut)
            this.timeOut = setTimeout(() => {
                if (!this.$refs?.hasClosePopoverRef?.$refs?.popper.contains(e.target) && !this.$refs.referenceRef.contains(e.target)) {
                    this.close()
                }
            }, 20)
        },
        updatePopover() {
            this.$refs.hasClosePopoverRef && this.$refs.hasClosePopoverRef.updatePopper()
        }
    }
}
</script>
<style lang="scss" scoped>
.my-popover-container {
    display: inline-block;
    line-height: 0;
    .comp-reference {
        display: inline-block;
        line-height: 0;
    }
}
</style>
<style lang="scss">
.self-class-close-btn-popover {
    min-width: 100px;
    padding: 0;
    font-family: PingFangSC, PingFang SC;
    &.popover-out-container {
        box-shadow: 0px 3px 14px 6px rgba(0, 0, 0, 0.05), 0px 8px 10px 1px rgba(0, 0, 0, 0.06), 0px 5px 5px -3px rgba(0, 0, 0, 0.1);
        border-radius: 4px;
        border: 1px solid #dcdcdc;
        &.el-popper[x-placement^='bottom'] {
            margin-top: 8px;
        }
    }
    &.not-arrow-b-mt4 {
        &.el-popper[x-placement^='bottom'] {
            margin-top: 4px;
        }
    }
    .popover-content {
        .content-top {
            display: flex;
            height: 51px;
            padding: 0 16px;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #f2f3f5;
            .title {
                font-weight: 500;
                font-size: 14px;
                color: #333333;
                line-height: 22px;
            }
            .el-icon-close {
                font-size: 16px;
                color: #666666;
                cursor: pointer;
                &:hover {
                    color: #db9130;
                }
            }
        }
        .content-center {
            margin: 16px 0;
            padding: 0 16px;
            overflow-y: auto;
        }
    }
}
</style>

组件使用

<my-popover
    width="240"
    placement="bottom-start"
    popover-class-name="popover-out-container"
    transition="el-zoom-in-top"
    :show-close="false"
    :is-click-close="true"
    :content-center-style="{ margin: '4px 0', padding: 0 }"
    @show-pop="cutFlowShow"
>
        <div>这里是内容</div>
    <span slot="referenceContent">触发</span>
</my-popover>

 

posted @ 2024-10-16 10:47  hong_li  阅读(72)  评论(0编辑  收藏  举报