扩展element-ui 抽屉组件,实现左右或上下拉伸

<!--
扩展element-ui 抽屉组件,实现左右或上下拉伸
-->

<template>
    <el-drawer
        :title="title"
        :visible.sync="show"
        :class="direction === 'rtl' ? 'drawer-drag' : ''"
        :modal="false"
        :size="size || '660px'"
        append-to-body
        @close="$emit('close')"
        @closed="$emit('closed');$emit('update:visible', false)"
        :direction="direction">

        <div class="drag-bar" v-if="direction === 'rtl'" draggable v-resize-left></div>
        <div class="drag-bar-top" v-else draggable v-resize-top></div>

        <slot></slot>


    </el-drawer>
</template>

<script>
export default {
    props: {
        title: String,
        visible: Boolean,
        direction: {
            type: String,
            default: 'rtl'
        },
        size: String
    },
    mounted() {
        this.$root.$on('close-drawer', () => {
            this.show = false
            this.$emit('update:visible', false)
        })
        this.show = this.visible
    },
    watch: {
        visible (val) {
            this.show = val
        }
    },
    data () {
        return {
            show: false
        }
    },
    directives: {
        /** 向左拉伸 */
        resizeLeft: { //
            inserted: (el) => {
                let docW = document.body.clientWidth - 300,
                    l = 0,
                    parent = el.parentNode
                let findParent = (el) => {
                    let p = el.parentNode
                    if(p) {
                        if(p.className.match('el-drawer__wrapper') !== null) {
                            parent = p
                        }else {
                            findParent(p)
                        }
                    }
                }
                findParent(el)
                docW = parent.clientWidth

                let setpos = (e) => {
                    if(e.clientX > 0) {
                        let dw =Math.max(100, docW + (l - e.clientX))
                        parent.style.width = dw + 'px'
                    }
                }
                el.ondragstart = function(e) {
                    l = e.clientX
                }
                el.ondrag = setpos
                el.ondragend = function () {
                    docW = parent.clientWidth
                }
            }
        },
        /** 向上拉伸 */
        resizeTop: { //
            inserted: (el) => {
                let docW = document.body.clientWidth - 300,
                    l = 0,
                    parent = el.parentNode
                let findParent = (el) => {
                    let p = el.parentNode
                    if(p) {
                        if(p.className.match('el-drawer ') !== null) {
                            parent = p
                        }else {
                            findParent(p)
                        }
                    }
                }
                findParent(el)
                docW = parent.clientHeight

                let setpos = (e) => {
                    if(e.clientY > 0) {
                        let dw =Math.max(100, docW + (l - e.clientY))

                        parent.style.height = dw + 'px'
                    }
                }
                el.ondragstart = function(e) {
                    l = e.clientY
                }
                el.ondrag = setpos
                el.ondragend = function (e) {
                    docW = parent.clientHeight

                }
            }
        },
    },

}
</script>

<style lang="less" scoped>
.drag-bar {
    position: absolute;
    left: -20px;
    width: 40px;
    top: 0;
    height: 100%;
    cursor: w-resize;
    z-index: 1002;
    //  background: red;

}
.drag-bar-top {
    position: absolute;
    top: -20px;
    height: 40px;
    left: 0;
    width: 100%;
    cursor: n-resize;
    z-index: 1002;
    // background: red;
}
.drawer-drag {
    width: 661px;
    left: auto;
    box-shadow: 0 3px 6px rgba(0, 0, 0, .2);

    /deep/ * {
        -moz-user-select: none;           /*火狐*/
        -webkit-user-select: none;        /*webkit浏览器*/
        -ms-user-select: none;            /*IE10*/
        -khtml-user-select: none;         /*早期浏览器*/
        user-select: none;                /** 去掉蓝色选中底*/
    }
    /deep/ .el-drawer__header {
        border-bottom: 1px solid #ddd;
        padding-bottom: 20px;
    }
    /deep/.el-drawer {
        width: 100% !important;
    }
}
</style>


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 未来需要改进的地方 1、写作——有助于想法的锻炼,同时对于表达也是有帮助——生活中常常需要用到——实用 2、美术—...
    笑云昵阅读 193评论 0 0
  • 今日体验,客情关系是基础……像是建造一座房子一样,根基牢固才是最大的根本 核心,行业进入悲壮的竞争态势, 用,修好...
    王海博阅读 160评论 0 0
  • 起风了 水面亮闪闪的一片,荡开 对着光 在这个空荡荡的河边 不需要思想的纠缠 只要一起随着落叶 飘扬 白茫茫的一片...
    王家王侯阅读 234评论 0 0
  • 夜莺2517阅读 127,761评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 6,967评论 1 6