vue项⽬中⾃⼰写popup弹窗功能-⼼得-案例效果图
上图展⽰有两种⽅式(以下代码演⽰只粘贴主要样式和逻辑代码)
⽅式⼀:是通过组件卸载的⽅式
⽅式⼆:是通过弹窗内部控制显⽰和隐藏
主要公共样式代码
// 弹窗
.popup-mask {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: 50001;
background: rgba(0, 0, 0, 0.5);
.popup-content {
/
/ position: fixed;
position: absolute;
width: 50%;
// min-height: 240px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
// box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
// border-radius: 5px;
box-sizing: border-box;
background: #fff;
/
/ opacity: 1;
overflow: hidden;
z-index: 50002;
.header{
position: relative;
background-color: #F9F9F9;
border-bottom: 1px solid #627D93; display: flex;
align-items: center;
justify-content: center;
.title{
font-size: 24px;
font-family: Microsoft YaHei;
font-weight: bold;
color: #2E516F;
}
img{
position: absolute;
top: 6px;
right: 20px;
width: 48px;
height: 48px;
}
&::after{
content: "";
width: 10px;
height: 10px;
background: rgba(0, 0, 0, 0.5);
position: absolute;
top: -1px;
left: -1px;
}
&::before{
content: "";
width: 10px;
height: 10px;
background: rgba(0, 0, 0, 0.5);
position: absolute;
top: -1px;
right: -1px;
}
}
.content{
padding: 50px 50px 20px;
min-height: 156px;
background-color: #fff;
}
.footer{
display: flex;
align-items: center;
justify-content: space-around;
height: 120px;
border-top: 1px solid #627D93;
background-color: #F9F9F9;
&.button{
a{
width: 200px;
height: 50px;
}
}
}
&.s{
width: 470px;
}
&.m{
width: 674px;
}
&.l{
}
}
}
/* 设置持续时间和动画函数 */
.popup-enter-active,
.popup-leave-active {
transition: opacity 0.5s linear;
}
.popup-enter,
.popup-leave-to {
opacity: 0;
}
⽅式⼀
优点:通过组件卸载⽅式,可以达到清空组件内数据(还原data中定义的数据项)定义popup弹窗组件
<transition name="popup">
<div class="popup-mask" v-if="show_info_box" @click.self="closeInfoPopup"> <!-- 提⽰弹窗 -->
<div class="popup-content s" v-if="isShowPopup == 0">
<div class="header">
<span class="title">提⽰</span>
<span class="close" @click="closeInfoPopup">X</span>
</div>
<div class="content">提⽰内容</div>
<div class="footer button">
<a class="btn_infoqr_m_r_p">确认</a>
<a class="btn_infoqx_m_r_t" @click="closeInfoPopup">取消</a>
</div>
</div>
</div>
</transition>
</template>
<script>
export default {
props: {
setDataDetail: {
type: Object,
required: true,
// default: function () {
// return {
// id: "1",
// name: "dyh",
// show_type: 1,
// };
// },
},
},
data() {
return {
//弹窗状态 0 提⽰
isShowPopup: 0,
show_info_box: !false,
};
},
watch: {},
created() {},
mounted() {
},
methods: {
// 关闭弹窗
closeInfoPopup() {
this.$bus.$emit('popupUse',0);
},
},
beforeDestroy() {
console.log("页⾯卸载popup");
},
};
</script>
页⾯内使⽤
主要通过v-if="show_info_box"控制组件的卸载和安装
<template>
<div class="about">
<h1>可以卸载的弹窗</h1>
<button @click="showPopup()">弹窗</button>
<transition name="popup">
<Popup3 v-if="show_info_box" :setDataDetail="setDataDetail" />
</transition>
</div>
</template>
<script>
import Popup3 from "@/components/Popup3";
export default {
name: "Popupuse",
components: {
Popup3,
},
data() {
return {
show_info_box: false,
setDataDetail: {},
};
},
computed: {},
mounted() {
this.$bus.$on("popupUse", () => {
this.showPopup();
});
},
methods: {
/
borderbox/ 弹窗显⽰/隐藏
showPopup() {
this.show_info_box = !this.show_info_box;
},
},
beforeDestroy() {
this.$bus.$off(["popupUse"]);
console.log("popupuse卸载了");
},
};
</script>
提⽰:上⾯重复的内容可以使⽤vue混⼊简化,为了好理解,我就不简化了
⽅式⼆
定义popup弹窗组件
组件内通过v-if关闭弹窗,不能将组件卸载,导致组件内会残留数据,影响下次使⽤可以通过关闭组件时⼿动重置data中数据项实现
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论