最近要写个弹框,发现element-ui弹框样式还可以,就copy下来改吧改吧。也不分步骤详细介绍了直接上代码。
在组件目录中新建文件夹message
我把message目录里的东西给大家贴出来
message文件夹
src文件夹
index.js
import Message from './src/main.js'; export default Message;
mian.js
import Vue from 'vue';
import Main from './main.vue';
let MessageConstructor = Vue.extend(Main);
let instance;
let instances = [];
let seed = 1;
const Message = (options = {}) => {
if (typeof options === 'string') {
options = {
message: options
};
}
let userOnClose = options.onClose;
let id = 'message_' + seed++;
options.onClose = function() {
Message.close(id, userOnClose);
};
instance = new MessageConstructor({
data: options
});
instance.id = id;
instance.vm = instance.$mount();
document.body.appendChild(instance.vm.$el);
instance.vm.visible = true;
instance.dom = instance.vm.$el;
instance.dom.style.zIndex = 999;
instances.push(instance);
return instance.vm;
}
['success', 'warning', 'info', 'error'].forEach(type => {
Message[type] = options => {
if (typeof options === 'string') {
options = {
message: options
};
}
options.type = type;
return Message(options);
};
});
Message.close = function(id, userOnClose) {
for (let i = 0, len = instances.length; i < len; i++) {
if (id === instances[i].id) {
if (typeof userOnClose === 'function') {
userOnClose(instances[i]);
}
instances.splice(i, 1);
break;
}
}
};
Message.closeAll = function() {
for (let i = instances.length - 1; i >= 0; i--) {
instances[i].close();
}
};
export default Message;
mian.vue
<template>
<transition name="message-fade">
<div :class="[
'message',
type && !iconClass "
v-show="visible"
@mouseenter="clearTimer"
@mouseleave="startTimer">
<i :class="iconClass" v-if="iconClass"></i>
<i :class="typeClass" v-else></i>
<slot>
<p v-if="!dangerouslyUseHTMLString" class="message-content">{{ message }}</p>
<p v-else v-html="message" class="message-content"></p>
</slot>
<i v-if="showClose" class="message-close-btn icon-close" @click="close"></i>
</div>
</transition>
</template>
<script>
const typeMap = {
success: 'success',
info: 'info',
warning: 'warning',
error: 'error'
};
export default {
data () {
return {
visible: false,
message: '',
duration: 1000,
type: 'info',
iconClass: '',
customClass: '',
onClose: null,
showClose: false,
closed: false,
timer: null,
dangerouslyUseHTMLString: false,
center: false
}
},
computed: {
typeClass() {
return this.type && !this.iconClass
"less">
.message {
min-width: 200px;
box-sizing: border-box;
border-radius: 3px;
border: 1px solid #ebeef5;
position: fixed;
left: 50%;
top: 20px;
transform: translateX(-50%);
background-color: #edf2f3;
transition: opacity 0.3s, transform .4s;
overflow: hidden;
padding: 10px;
display: flex;
align-items: center;
}
.message-icon{
width: 15px;
height: 15px;
border-radius: 100%;
background: #fff;
display: inline-block;
margin-right: 10px;
&.icon-success{
background: url("../../../assets/image/icon-success.png") no-repeat center center;
background-size: auto 100%;
}
&.icon-error{
background: url("../../../assets/image/icon-error.png") no-repeat center center;
background-size: auto 100%;
}
&.icon-info{
background: url("../../../assets/image/icon-info.png") no-repeat center center;
background-size: auto 100%;
}
&.icon-warning{
background: url("../../../assets/image/icon-warning.png") no-repeat center center;
background-size: auto 100%;
}
}
.message-success{
background: #f2f8ec;
border-color: #e4f2da;
.message-content{
color: #7ebe50;
}
}
.message-error{
background: #fcf0f0;
border-color: #f9e3e2;
.message-content{
color: #e57470;
}
}
.message-warning{
background: #fcf6ed;
border-color: #f8ecda;
.message-content{
color: #dca450;
}
}
.message-info{
background: #eef2fb;
border-color: #ebeef4;
.message-content{
color: #919398;
}
}
.message-fade-enter,
.message-fade-leave-active {
opacity: 0;
transform: translate(-50%, -100%);
}
</style>
以上就是封装的所有代码
接下来就来看看如何引用
main.js中引入
import Message from '@/components/message/index.js'
Vue.prototype.$message = Message
调用
在你需要的页面调用
this.$message({
message: '提示消息',
type:'error' //type有四个值 1.error 2.success 3.info 4.warning
});
type为success
type为warning
type为info
type为errpr
小icon的图片用自己的图片哦
总结
以上所述是小编给大家介绍的vue项目中仿element-ui弹框效果的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“vue项目中仿element-ui弹框效果的实例代码”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年11月08日
2025年11月08日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]





