目录结构
{{title}}
{{content}}
{{cancelText}}
{{confirmText}}
export default {
name: 'index',
data () {
return {
title: '提示',
content: null,
cancelText: '取消',
confirmText: '确认',
cancel: function () {},
confirm: function () {},
isSync: false,
isShow: false
}
},
methods: {
onCancel () {
if (!this.isSync) this.isShow = false
this.cancel()
},
onConfirm () {
if (!this.isSync) this.isShow = false
this.confirm()
}
}
}
#app{
overflow: hidden;
}
.ls-mask{
width: 100%;
height: 100%;
position: fixed;
left: 0;
top: 0;
background: rgba(0,0,0,0.3);
}
.ls-mask-window{
padding-top: 20px;
width: 80%;
background: white;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.ls-mask-window>h2{
text-align: center;
}
.ls-mask-content{
padding: 20px;
text-align: center;
}
.ls-mask-footer{
height: 45px;
border-top: 1px solid #f0f0f0;
display: flex;
}
.ls-mask-footer>div{
flex: 1;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
}
.ls-mask-footer>div:active{
background: #fafafa;
}
.ls-mask-footer>div:nth-child(1){
border-right: 1px solid #f0f0f0;
color: gray;
}
popup/index.js
import Vue from 'vue'
import Popup from './index.vue'
const PopupBox = Vue.extend(Popup)
Popup.install = function (data) {
console.log(data)
let instance = new PopupBox({
data
}).$mount()
document.body.appendChild(instance.$el)
Vue.nextTick(() => {
instance.isShow = true
})
}
export default Popup
main.js
import Popup from './components/popup/index'
Vue.prototype.$popup = Popup.install
popup_test.vue(测试页面)
弹窗
export default {
name: 'popup_test',
data () {
return {
}
},
methods: {
showPopup () {
this.$popup({
title: '提示', // 弹窗标题
content: '零三的笔记web03.cn', // 弹窗内容
cancelText: '稍后再看', // 左边按钮文本
confirmText: '马上查看', // 右边按钮文本
isSync: true, // 是否异步
cancel: () => {
// 点击左边按钮事件
console.log('取消')
},
confirm: () => {
// 点击右边按钮事件
window.open('https://web03.cn')
}
})
}
}
}
效果图