<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:;
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:;
border-bottom: 1px solid #ccc;
}
.content-body{
font-size: 1.5rem;
margin: 2rem 1rem;
line-height:;
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:;
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

vue自定义插件-弹框的更多相关文章

  1. vue 自定义动态弹框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. vue自定义插件封装,实现简易的elementUi的Message和MessageBox

    vue自定义插件封装示例 1.实现message插件封装(类似简易版的elementUi的message) message组件 <template>     <transition  ...

  3. JavaScript实现自定义alert弹框

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO

  4. CodePush自定义更新弹框及下载进度条

    CodePush 热更新之自定义更新弹框及下载进度 先来几张弹框效果图 非强制更新场景 image 强制更新场景 image 更新包下载进度效果 image 核心代码 这里的热更新Modal框,是封装 ...

  5. vue移动端弹框组件,vue-layer-mobile

    最近做一个移动端项目,弹框写的比较麻烦,查找资料,找到了这个组件,但是说明文档比较少,自己研究了下,把我碰到的错,和详细用法分享给大家!有疑问可以打开组件看一看,这个组件是仿layer-mobile的 ...

  6. 自定义alert弹框,title不显示域名

    问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...

  7. WPF 如何自定义一个弹框

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 简述: 手工以原生Grid的方式,自定义了一个仿弹窗效果,优点可以自定义,缺点需要自己实现以及维护整个弹窗的效 ...

  8. Vue 自定义全局消息框组件

    消息弹框组件,默认3秒后自动关闭,可设置info/success/warning/error类型 效果图: 文件目录: Message.vue <template> <transit ...

  9. 自定义alert弹框,title不显示域名(重写alert)

    问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...

随机推荐

  1. html总结:背景图片拉伸

    两种方法: ⑴推荐方法 <style>body {background-image:url(images/backimage.jpg);background-size:cover;}< ...

  2. Python解释器有哪些?Python解释器种类

    Python是一门解释器语言,代码想运行,必须通过解释器执行,Python存在多种解释器,分别基于不同语言开发,每个解释器有不同的特点,但都能正常运行Python代码,以下是常用的五种Python解释 ...

  3. rem移动端适配方案

    一. rem vs em 单位 定义 特点 rem font size of the root element 以根元素字体大小为基准 em font size of the element 以父元素 ...

  4. node-sass下载失败 关于webpack

    安装node-sass提示没有vendor目录的解决办法在node-sass目录下面新建一个vendor的空目录,然后运行npm/cnpm rebuild node-sass --save-dev即可 ...

  5. vue组件star开发基于vue-cli

    <template> <div class="stars"> <div v-for="(item,ind) in num" :ke ...

  6. java设计模式:概述与GoF的23种设计模式

    软件设计模式的产生背景 设计模式这个术语最初并不是出现在软件设计中,而是被用于建筑领域的设计中. 1977 年,美国著名建筑大师.加利福尼亚大学伯克利分校环境结构中心主任克里斯托夫·亚历山大(Chri ...

  7. C# Note22: 《Effective C#》笔记

    参考:<Effective C#>快速笔记(一)- C# 语言习惯 参考:<Effective C#>快速笔记(二)- .NET 资源托管 参考:<Effective C ...

  8. Django--CRM--一级, 二级 菜单表

    一. 一级菜单表 1. 首先要修改权限表的字段, 在权限表下面加上icon和 is_menu 的字段 2. 展示结果 # 我们既然想要动态生成一级菜单,那么就需要从数据库中拿出当前登录的用户的菜单表是 ...

  9. 二进制安装MongoDB

    1.下载mongodb cd /usr/local/src/ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz ...

  10. 老男孩python学习自修第八天【函数式编程】

    1.可变参数,将传参自动汇总成列表 2.可变参数,将参数自动汇总成字典 实战如下: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ def show(*arg ...