<template> <div class="modal-layout-root"> <transition name="modal-fade"> <div class="modal-container" v-show="visible"> <!-- 头部标题 --> <div class="modal-header"> <div class="modal-title">{{ title }}</div> <i class="iconfont close-icon" @click="close"></i> </div> <!-- 内容区域 --> <div class="modal-content"> <slot name="content"></slot> </div> <!-- 底部按钮 --> <div class="modal-footer"> <div class="modal-btn"> <button class="cancel" ref="cancelBtn" @click="close" @mousedown="cancelMouseDown" @mouseup="cancelMouseUp">取消</button> <button class="ok" @click="ok">确认</button> </div> </div> </div> </transition> <!-- 遮罩层 --> <div class="mask" v-show="visible" @click="close"></div> </div> </template> <script> export default { name: "modal", data () { return {} }, props: { // 模态框标题 title: { type: String, default: () => { return '模态框标题' }, }, // 显示隐藏控件 visible: { type: Boolean, default: () => { return false }, }, }, mounted () { this.$nextTick(() => { const body = document.querySelector("body"); if (body.append) { body.append(this.$el); } else { body.appendChild(this.$el); } }); }, beforeDestroy () { document.body.removeChild(this.$el) }, methods: { // 取消 close () { this.$emit('cancel') this.$emit('update:visible', false) }, // 确认 ok () { this.$emit('submit') this.$emit('update:visible', false) }, // 取消按钮 鼠标按下事件 cancelMouseDown () { this.$refs.cancelBtn.style.color = '#096dd9' this.$refs.cancelBtn.style.border = '1px solid #096dd9' }, // 取消按钮 鼠标松开事件 cancelMouseUp () { this.$refs.cancelBtn.style.color = '#595959' this.$refs.cancelBtn.style.border = '1px solid #d9d9d9' }, }, watch: { // 操作遮罩层的展示/隐藏 visible () { if (this.visible == true) { document.querySelector('body').setAttribute('style', 'overflow:hidden !important;') } else { document.querySelector('body').removeAttribute('style') } }, }, } </script>