<template><transition name="msgbox"><div v-if="show" class="msgbox-container" :class="className"><header>{{title}}</header><div class="content-body"><div>弹出内容可以嵌入html标签</div></div><footer><a v-if="cancel" href="javascript:;" @click="cancelBtn" class="button">{{cancel}}</a><a href="javascript:;" @click="successBtn" class="button">{{confirm}}</a></footer></div></transition>
</template>

<script>
export default {data () {return {show: false,title: '提示',content: '',confirm: '确定',cancel:'',className:''}},watch:{show (val) {if (val) {if(!this.hasClass(document.body,"pop-mask")){this.addClass(document.body,"pop-mask");}}else{this.removeClass(document.body,"pop-mask");}}},beforeMount () {//如果已经存在,则阻止出现第二次let node = document.querySelector('.msgbox-container')if (node && node.parentNode) {node.parentNode.removeChild(node)}},methods: {successBtn () {this.show = false;},cancelBtn () {this.show = false;},addClass(obj, cls){var obj_class = obj.className,blank = (obj_class != '') ? ' ' : '';var added = obj_class + blank + cls;obj.className = added;},removeClass(obj, cls){var obj_class = ' '+obj.className+' ';obj_class = obj_class.replace(/(\s+)/gi, ' ');var removed = obj_class.replace(' '+cls+' ', ' ');removed = removed.replace(/(^\s+)|(\s+$)/g, '');obj.className = removed;//替换原来的 class.
    },hasClass(obj, cls){var obj_class = obj.className,obj_class_lst = obj_class.split(/\s+/);var x = 0;for(x in obj_class_lst) {if(obj_class_lst[x] == cls) {return true;}}return false;}}
}
</script>

<style lang="scss" scoped>
.msgbox-container{position: fixed;top:50%;left:50%;width: 90%;background: #fff;color: #555;border-radius: 0.8rem;transform:translate(-50%,-50%) scale(1, 1);header{margin: 0;padding: 1.2rem 0;text-align: center;color: #333;height: 2rem;line-height: 2rem;font-size: 1.7rem;border-radius: 0.8rem 0.8rem 0 0;background: #fff;border-width: 0;border-bottom: 1px solid #ccc;}.content-body{font-size: 1.5rem;margin: 2rem 1rem;line-height: 2;max-height: 20rem;overflow-y: auto;color: #666;div{padding: 0 1rem;text-align: justify;word-break: break-all;}}footer {width: 100%;text-align: center;display: block !important;border-width: 0;border-top: 1px solid #ccc;overflow: hidden;background: transparent;border-radius: 0 0 0.8rem 0.8rem;.button{float: left;padding: 1rem 0;width: 50%;color: #999;box-sizing: border-box;line-height: 3rem;font-size: 1.7rem;background: #f7f7f7;border-right: 1px solid #D5D7D6;text-decoration: none;-webkit-tap-highlight-color: transparent;}.button:first-child:nth-last-child(1) {width: 100%;}.button:first-child:nth-last-child(2) ~ .button {width: 50%;}}
}
.msgbox-enter,.msgbox-leave-to{-webkit-transform:  translate(-50%,-50%) scale(0,0);
}
.msgbox-enter-active,.msgbox-leave-active{-webkit-transition: all .3s;transition: all .3s;
}
.msgbox-enter-to,.msgbox-leave{-webkit-transform:  translate(-50%,-50%) scale(1,1);
}
</style>

最后在 main.js 里面 配置

import emComponent from './components/custom/index'
Vue.use(emComponent)

具体使用方法

<template><div><label id="msgbox" @click.stop="showMsgbox">点击我显示msgbox</label></div>
</template>
<script>

<script>
export default {name: 'test',data(){return {}},methods:{showMsgbox(){this.$msgbox({title:'温馨提示',cancel:'取消',content:'这里是消息弹出内容',confirm:'确定按钮',className:'pop-custom'}).then(()=>{console.log("我点击了确定按钮")}).catch((err)=>{console.log("error");})}}
}
</script>

源码地址 https://github.com/zuobaiquan/vue/tree/master/vueExercise/vue-component/src/components/custom

转载于:https://www.cnblogs.com/zuobaiquan01/p/8435996.html

vue自定义插件-弹框相关推荐

  1. vue自定义插件 封装一个类似 element 中 message 消息提示框的插件

    vue自定义插件 封装一个类似 element 中 message 消息提示框的插件 一.资源文档 1.vue内置组件 transition 2.Vue.use() 3.Vue.extend() 4. ...

  2. elementUI table自定义表头弹框搜索,排序,显示tag标签

    elementUI table自定义表头弹框搜索,排序,显示tag标签 允许一种排序,多种搜索条件 包括三种类别,添加了solt插槽,也可以自定义: 输入框[input] 多选[checkbox] 范 ...

  3. 微信小程序自定义授权弹框

    微信小程序自定义授权弹框 最近微信获取用户信息的接口有调整,就是这货:wx.getUserInfo(OBJECT),文档描述如下: 此接口有调整,使用该接口将不再出现授权弹窗,请使用 <butt ...

  4. Android 简易的自定义确认弹框AlertDialog

    使用方式/构造函数参数解释: 参数一:上下文; 参数二:标题; 参数三:内容; 参数四:确认按钮点击回调 CustomDialog(this, "清空输入", "确定要清 ...

  5. Android自定义Dialog弹框效果

    这是弹框输入密码的,这里还有一个是我发现一个博友写的自定义Dialog弹框信息和背景阴影显示 ,两者结合一起看,最好了!!! 下面是弹框输入密码的!!! 首先需要自定义一个类,继承Dialog pac ...

  6. vue 定义全局弹框_VUE路由拦截:Vue自定义全局弹窗组件

    前言 在任何一个平台中,如果需要增加用户黏度,除了用户需要的基本内容外,用户登录注册提交信息也是非常重要的一环,可以了解用户基本信息,用户喜欢等. 抛出前后端混合开发外,vue可以轻松的实现路由拦截. ...

  7. vue移动端弹框组件

    这里说一下 vue-layer-mobile插件的使用 一.npm 安装 // 当前最新版本 1.2.0 npm install vue-layer-mobile // 如新版遇到问题可回退旧版本 n ...

  8. 微信小程序自定义picker弹框组件

    要有遥不可及的梦想,也要有脚踏实地的本事.----------- Grapefruit.Banuit Gang(香柚帮) 柚子写项目遇到了一个问题,就是微信小程序官方文档上面的弹框组件picker,它 ...

  9. swift4.2 - 一个自定义view弹框

    import UIKit /** 注册协议view:没找到 UI原图,咱不实现*/ class JYRegisterProtocolView: UIView {/// 点击同意协议的回调private ...

最新文章

  1. Android 数据库基本操作-2
  2. R语言在图上标出点坐标_从基变换的角度理解旋转矩阵R
  3. mybatis mapper.xml --注释 带参数的坑
  4. MySQL高级 - 查询缓存 - SELECT选项
  5. 这是对R的误解!R的应用原来这么广!
  6. Java读取properties配置文件时,中文乱码解决方法
  7. 6-7 使用函数输出水仙花数_「Java」再议printf函数
  8. 120万人同时在线考试,这么大的流量如何支撑
  9. 用友nc操作手册_铁军人物汤轩宇, 入职两年,她用努力和汗水编制出单户试算操作手册...
  10. CSS完美实现垂直居中-测试页
  11. Java基础算法--排序
  12. java 程序员职业规划,详细说明
  13. Android WebView实现长截图
  14. 「PHP 是最好的语言」这个梗是怎么来的?
  15. appium之toast处理
  16. 浏览器首页被恶意篡改
  17. 小程序码 踩坑记录(buffer转成图片显示问题)
  18. pdf epub reader 比较
  19. 如何限制访问一些网站
  20. PPT图片瘦身/图片提取

热门文章

  1. 【设计模式】适配器模式 Adapter Pattern
  2. 通信 —— 串口与并口
  3. Python当前线程休眠1秒钟
  4. 不同网段的PC与交换机的原理
  5. Ext 组件的一些操作
  6. python插件使用教程_Python插件机制实现详解
  7. kvm架构详解--理解CPU、内存、IO虚拟化技术、处理器硬件支持
  8. Flume Source
  9. 消费者rebalance机制分析
  10. python中list_python中list方法详解说明