自己动手写Vue插件Toast

自己动手写Vue插件Toast

大家好,又见面了,我是全栈君。

自己动手写Vue插件Toast

		<style>
			.vue-toast {
				width: 100%;
				height: 100%;
				position: fixed;
				top: 0px;
				left: 0px;
				background: rgba(0, 0, 0, 0.3);
			}
			
			.vue-tip {
				position: fixed;
				left: 50%;
				width: 100px;
				line-height: 40px;
				padding: 0 10px;
				margin-left: -60px;
				text-align: center;
				z-index: 9999;
				font-size: 14px;
				color: #fff;
				border-radius: 5px;
				background-color: rgba(0, 0, 0, .7);
				 
			}
			
			.vue-tip.tip-center {
				top: 50%;
			}
			
			.vue-tip.tip-bottom {
				bottom: 50px;
			}
			
			.vue-tip.tip-top {
				top: 50px;
			}
			
			.fadeIn {
				animation-name: fadeIn;
				-webkit-animation-name: fadeIn;
				animation-duration: .5s;
				-webkit-animation-duration: .5s;
				animation-timing-function: ease-in-out;
				-webkit-animation-timing-function: ease-in-out;
				visibility: visible !important;
			}
			
			@keyframes fadeIn {
				0% {
					transform: scale(0);
					opacity: 0.0;
				}
				60% {
					transform: scale(1.1);
				}
				80% {
					transform: scale(0.9);
					opacity: 1;
				}
				100% {
					transform: scale(1);
					opacity: 1;
				}
			}
			
			@-webkit-keyframes fadeIn {
				0% {
					-webkit-transform: scale(0);
					opacity: 0.0;
				}
				60% {
					-webkit-transform: scale(1.1);
				}
				80% {
					-webkit-transform: scale(0.9);
					opacity: 1;
				}
				100% {
					-webkit-transform: scale(1);
					opacity: 1;
				}
			}
		</style>

  

var Toast = {};
//避免重复install,设立flag
Toast.installed = false;
Toast.install = function(Vue, options) {
	if(Toast.installed) return;
	let opt = {
		// 默认显示位置
		defaultType: "center",
		// 默认持续时间
		duration: "3000"
	}
	// 使用options的配置
	for(let i in options) {
		opt[i] = options[i]
	}
	Vue.prototype.$toast = (toast, type) => {
		// 如果有传type,位置则设为该type
		var chooseType = type ? type : opt.defaultType;
		// 如果页面有toast则不继续执行
		if(document.querySelector('.vue-toast')) return;
		// 1、创建构造器,定义好提示信息的模板
		let toastTip = Vue.extend({
			template: `
						           <div class="vue-toast">
						              <div class="vue-tip tip-${chooseType} fadeIn">${toast}</div>
						           </div>
						          `
		});
		// 2、创建实例,挂载到文档以后的地方
		console.log(new toastTip().$mount())
		let tpl = new toastTip().$mount().$el;
		// 3、把创建的实例添加到body中
		document.body.appendChild(tpl);
		// 4.三秒后移除
		setTimeout(() => {
			document.body.removeChild(tpl);
		}, opt.duration);
		//阻止遮罩滑动
		document.querySelector("div.vue-toast").addEventListener("touchmove", function(e) {
			e.stopPropagation();
			e.preventDefault();
		});

		Toast.installed = true;

	};
	// 显示不同的位置
	['bottom', 'top', 'center'].forEach(type => {
		Vue.prototype.$toast[type] = (tips) => {
			return Vue.prototype.$toast(tips, type)
		}
	})
};
// 自动安装  ,有了ES6就不要写AMD,CMD了

if(typeof window !== 'undefined' && window.Vue) {
	window.Vue.use(Toast)
};

export default Toast;

 

 

<template>
  <div id="me">
  	<h1>{
    
    {getMy&&getMy.name}}--{
    
    {getMy&&getMy.age}}</h1>
	  <h1>我是Vue {
    
    {this.$store.state.my.name}}--{
    
    {this.$store.state.my.age}}</h1>
	  <div class="btn">
	  	<button @click='setName'>设置姓名</button>
	    <button @click='setAge'>设置年龄</button>
	    <button @click='setAction'>异步Action</button>
	    <button @click='alertToast'>Toast</button>
	  </div>
  </div> 
</template>

<script>
	import Vue from 'vue'
	import { mapGetters,mapMutations,mapActions  } from 'vuex'
	import Toast from '../js/Toast'
	Vue.use(Toast,{    //支持全局配置
		defaultType: "top",
		duration: "10000"
	})
	export default {
		name: 'Me',
		methods: {
			 ...mapMutations([
	      'setMe' // 映射 this.setMe() 为 this.$store.commit('setMe')
	    ]),
	     ...mapActions([
	      'getTopic' // 映射 this.getTopic() 为 this.$store.dispatch('getTopic')
	    ]),
			setName(){
				this.setMe({
					type:'name',
					num:'王麻子'
				})
			},
			setAge(){
				this.$store.commit('setMe',{
					type:'age',
					num:'100'
				})
			},
			setAction(){
				this.getTopic();
			},
			alertToast(){
				console.log(this);
				console.log(this.$toast("我是一个Toast"));
				//console.log(this.$toast.bottom("我是一个Toast"));
			}
		},
		computed: {
	    ...mapGetters([
	       'getApiVal','getApiValComm','getMy'
    ])
  }
	}
</script>

<style scoped>
	#me {
 	width: 80%;
 	height: 1000px;
 	margin: 0 auto;
 	border: 1px solid red;
 	background: white;
 }
 
 h1 {
 	color: red;
 	text-align: center;
 }
 .btn{
 	display: flex;
 	justify-content: space-around;
 	align-items: center;
 }
 button{
 	border: 1px solid red;
 	width: 100px;
 	height: 30px;
 	background: blue;
 	color: white;
 	cursor: pointer;
 }
</style>

  

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108442.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于File类概念及方法的一些介绍

    关于File类概念及方法的一些介绍java.io.File类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。Java把电脑中的文件和文件夹(目录)封装为一个File类,我们可以使用File类对文件和文件夹进行操作。File类的方法可以实现:1.创建一个文件/文件夹2.删除文件/文件夹3.获取文件/文件夹4.判断文件/文件夹是否存在5.对文件夹进行遍历6.获取文件的大小File类是一个与系统无关的类,任何操作系统都可以使用这个类中的方法重点:File:文件;Directory:文件夹/目录

    2022年6月7日
    39
  • vim与vi的区别,及常用操作,有查找关键词,向上向下翻页,快速跳到一行首末尾,设置取消行号,撤销编辑,跳到最后一行,翻页

    vim与vi的区别,及常用操作,有查找关键词,向上向下翻页,快速跳到一行首末尾,设置取消行号,撤销编辑,跳到最后一行,翻页

    2021年7月18日
    124
  • RFID-MFRC522射频识别模块,S50卡M1

    RFID-MFRC522射频识别模块,S50卡M1射频识别模块什么是RFIDMFRC522S50-M1卡1、主要指标2、存储结构RC522与ArduinoUNO的接线什么是RFID无线射频识别即射频识别技术(RadioFrequencyIdentification,RFID),是自动识别技术的一种,通过无线射频方式进行非接触双向数据通信,利用无线射频方式对记录媒体(电子标签或射频卡)进行读写,从而达到识别目标和数据交换的目的。MF…

    2022年7月14日
    14
  • 云存储性能_内存256g和128g的区别

    云存储性能_内存256g和128g的区别与以磁盘存储为主的普通数据库相比,内存数据库的数据访问速度可以高出几个数量级,能大幅提高运算性能,更适合高并发、低延时的业务场景。不过,当前大部分内存数据库仍然采用SQL模型,而SQL缺乏一些必要的数据类型和运算,不能充分利用内存的特征实现某些高性能算法。仅仅是把外存的数据和运算简单地搬进内存,固然也能获得比外存好得多的性能,但还没有充分利用内存特征,也就不能获得极致的性能。下面我们来看看,有哪些适合内存特征的算法和存储机制,可以进一步提升内存数据库计算速度。我们知道,内存可以通过地址(指针)来访问

    2025年8月14日
    3
  • CountDownTimer_bytebuffer slice

    CountDownTimer_bytebuffer slicepublicclassCountDownTimerManager{/***总倒计时*/privatelongmillisInFuture=0;/***回调时间*/privatelongcountDownInterval;/***倒计时完成回调*/privateFinishCountDownfinishCountDown;/**

    2022年9月18日
    2
  • java输出乱码是什么问题_jsp获取request参数

    java输出乱码是什么问题_jsp获取request参数最近有个业务需要模拟表单提交到asp页面中,但是我的项目编码是UTF8,而asp页面是GB2312,中文字段提交后,到达数据库后是乱码.问题的解决在于模拟提交的时候置

    2022年10月7日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号