欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue项目中仿element-ui弹框效果的实例代码

程序员文章站 2023-12-15 19:46:58
最近要写个弹框,发现element-ui弹框样式还可以,就copy下来改吧改吧。也不分步骤详细介绍了直接上代码。 在组件目录中新建文件夹message 我把messag...

最近要写个弹框,发现element-ui弹框样式还可以,就copy下来改吧改吧。也不分步骤详细介绍了直接上代码。

在组件目录中新建文件夹message

我把message目录里的东西给大家贴出来

vue项目中仿element-ui弹框效果的实例代码

message文件夹

vue项目中仿element-ui弹框效果的实例代码

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 ? `message-${ type }` : '',
   center ? 'center' : '',
   showclose ? 'closable' : '',
   customclass
   ]"
    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
   ? `message-icon icon-${ typemap[this.type] }`
   : '';
  }
 },
 watch: {
  closed(newval) {
  if (newval) {
   this.visible = false;
   this.$el.addeventlistener('transitionend', this.destroyelement);
  }
  }
 },
 methods: {
  destroyelement() {
  this.$el.removeeventlistener('transitionend', this.destroyelement);
  this.$destroy(true);
  this.$el.parentnode.removechild(this.$el);
  },
  close() {
  this.closed = true;
  if (typeof this.onclose === 'function') {
   this.onclose(this);
  }
  },
  cleartimer() {
  cleartimeout(this.timer);
  },
  starttimer() {
  if (this.duration > 0) {
   this.timer = settimeout(() => {
   if (!this.closed) {
    this.close();
   }
   }, this.duration);
  }
  },
  keydown(e) {
  if (e.keycode === 27) { // esc关闭消息
   if (!this.closed) {
   this.close();
   }
  }
  }
 },
 mounted() {
  this.starttimer();
  document.addeventlistener('keydown', this.keydown);
 },
 beforedestroy() {
  document.removeeventlistener('keydown', this.keydown);
 }
 }
</script>
<style lang="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
});

vue项目中仿element-ui弹框效果的实例代码

type为success

vue项目中仿element-ui弹框效果的实例代码

type为warning

vue项目中仿element-ui弹框效果的实例代码

type为info

vue项目中仿element-ui弹框效果的实例代码

type为errpr

小icon的图片用自己的图片哦

总结

以上所述是小编给大家介绍的vue项目中仿element-ui弹框效果的实例代码,希望对大家有所帮助

上一篇:

下一篇: