概述
实例效果
样式结构
<template>
<Transition name="down">
<div class="xtx-message" :style="style[type]" v-show="isshow">
<i class="iconfont" :class="style[type].icon"></i>
<span class="text">{{ text }}</span>
</div>
</Transition>
</template>
<style scoped lang="less">
.xtx-message {
width: 300px;
height: 50px;
position: fixed;
z-index: 9999;
left: 50%;
margin-left: -150px;
top: 30px;
line-height: 50px;
padding: 0 25px;
border: 1px solid #e4e4e4;
background: #f5f5f5;
color: #999;
border-radius: 4px;
i {
margin-right: 4px;
vertical-align: middle;
}
.text {
vertical-align: middle;
}
}
.down-enter-from {
transform: translateY(-70px);
opacity: 0;
}
.down-enter-to {
transform: translateY(0px);
}
.down-enter-active,
.down-leave-active{
transition: all 1s;
}
.down-leave-from{
transform: translateY(0px);
opacity: 1;
}
.down-leave-to{
transform: translateY(-100px);
opacity: 0;
}
</style>
js交互 简单样式
<script lang="ts" setup name="XtxMessage">
import { onMounted, ref } from 'vue';
defineProps<{
type: 'success' | 'error' | 'warning',
text: string
}>()
const isshow = ref(false)
// 定义一个对象,包含三种情况的样式,对象key就是类型字符串
const style = {
warning: {
icon: 'icon-warning',
color: '#E6A23C',
backgroundColor: 'rgb(253, 246, 236)',
borderColor: 'rgb(250, 236, 216)',
},
error: {
icon: 'icon-shanchu',
color: '#F56C6C',
backgroundColor: 'rgb(254, 240, 240)',
borderColor: 'rgb(253, 226, 226)',
},
success: {
icon: 'icon-queren2',
color: '#67C23A',
backgroundColor: 'rgb(240, 249, 235)',
borderColor: 'rgb(225, 243, 216)',
},
}
onMounted(() => {
isshow.value = true
setTimeout(() => {
isshow.value = false
}, 2000)
})
</script>
js交互逻辑代码
import XtxMessage from "./XtxMessage.vue";
import { h, render } from "vue";
const div = document.createElement("div");
div.className = "xtx-message-container";
document.body.appendChild(div);
export default function Message(
type: "success" | "error" | "warning",
text: string,
directive: number
) {
const vNode = h(XtxMessage, { type, text, directive });
render(vNode, div);
setTimeout(() => {
render(null, div);
}, directive);
}
Message.success = function (text: string, directive = 5000) {
Message("success", text, directive);
};
Message.error = function (text: string, directive = 5000) {
Message("error", text, directive);
};
Message.warning = function (text: string, directive = 5000) {
Message("warning", text, directive);
};
最后
以上就是顺利大炮为你收集整理的Vue3封装Message消息提示框的全部内容,希望文章能够帮你解决Vue3封装Message消息提示框所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复