自己动手写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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java后端开发职责_工作职责和岗位职责有什么区别

    java后端开发职责_工作职责和岗位职责有什么区别java后台开发岗位职责:1.参与项目后端的设计、开发工作,承担核心功能模块的代码编写,确保项目进度和质量;2.参与开发人员codereview工作,并能提供性能优化、安全性建议;3.参与系统架构设计、接口规范制定、技术文档编写等。4.参与现有系统的优化改进。岗位要求:1.本科及以上学历,计算机相关专业优先,【扎实的数据结构/算法与编码能力】;2.JAVA基础扎实,1年及以上JAV…

    2025年5月27日
    8
  • spring aop实例讲解_java swing 教程

    spring aop实例讲解_java swing 教程1.SpringAOP是什么?AspectOrientedProgramming:面向切面编程什么时候会出现面向切面编程的需求?按照软件重构的思想,如果多个类中出现重复的代码,就应该考虑定义一个共同的抽象类,将这些共同的代码提取到抽象类中,比如Teacher,Student都有username,那么就可以把username及相关的get、set方法抽取到SysUser中,这种情况,我们称…

    2022年8月11日
    3
  • SpringBoot整合Dubbo3.0基础配置

    SpringBoot整合Dubbo3.0基础配置SpringBoot 整合 Dubbo3 0 基础配置 dubbo spring boot starter 一 说明众所周知 阿里早已把 dubbo 捐赠给了 Apache 现在 dubbo 由 Apache 在维护更新 dubbo 也已经成了 Apache 下的顶级项目 所以本 demo 项目所依赖的坐标是 Apache 官方最新的 3 0 4 坐标 dependency groupId org apache dubbo groupId dependency

    2025年11月15日
    3
  • 对 FLAG_ACTIVITY_NEW_TASK、FLAG_ACTIVITY_CLEAR_TOP、FLAG_ACTIVITY_SINGLE_TOP 的理解

    对 FLAG_ACTIVITY_NEW_TASK、FLAG_ACTIVITY_CLEAR_TOP、FLAG_ACTIVITY_SINGLE_TOP 的理解为了看得更清晰,以下使用代称newtask:FLAG_ACTIVITY_NEW_TASKcleartop:FLAG_ACTIVITY_CLEAR_TOPsingletop:FLAG_ACTIVITY_SINGLE_TOP文章目录default单独singletop单独cleartopcleartop+singletopnewtask单独newtasknewtask+sin…

    2022年7月17日
    18
  • JavaScript——二叉树层序遍历

    JavaScript——二叉树层序遍历题目描述给你一个二叉树,请你返回其按层序遍历得到的节点值。(即逐层地,从左到右访问所有节点)。示例二叉树:[3,9,20,null,null,15,7], 3/\920/\157返回其层序遍历结果:[[3],[9,20],[15,7]]递归实现代码varlevelOrder=function(root){if(root===null)return[]l

    2022年5月21日
    53
  • STM32中文参考手册_STM32读取ESP8266数据

    STM32中文参考手册_STM32读取ESP8266数据http://blog.csdn.net/u012722571/article/details/47295245lanmanck原创】这篇文章已经说了STM32的启动过程:http://blog.csdn.net/lanmanck/article/details/8252560我们也知道怎么跳到main函数了,那么,中断发生后,又是怎么跑到中断入口地址的呢?从stm

    2025年7月27日
    2

发表回复

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

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