自己动手写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>
自己动手写Vue插件Toast的更多相关文章
- 自己动手写Android插件化框架
自己动手写Android插件化框架 转 http://www.imooc.com/article/details/id/252238 最近在工作中接触到了Android插件内的开发,发现自己这种技 ...
- 自己动手编写vue插件
一.为什么要自己动手写插件呢,原因有二: 其一:是因为最近产品了提了一个在web端接收,消息通知的需求,产品要求在若干个页面内如果有消息,就要弹出消息弹窗展示给用户,略加思索之后,第一反应就是写个消息 ...
- 手把手教你写vue插件并发布(二)
前记:上一篇 https://www.cnblogs.com/adouwt/p/9211003.html, 说到了一个完整的vue插件开发.发布的流程,总结下来就讲了这么一个事,如何注入vue, 如果 ...
- 手把手教你写vue插件并发布(一)
vue的插件开发 这篇文章较长,需要一定的阅读时间.这里有一份改善版本的插件笔记,在一个项目下完成开发.测试.发布的全过程.https://www.cnblogs.com/adouwt/p/96555 ...
- 自己动手写Android插件化框架,让老板对你刮目相看
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由达文西发表于云+社区专栏 最近在工作中接触到了Android插件内的开发,发现自己这种技术还缺乏最基本的了解,以至于在一些基本问题上浪 ...
- 自己动手写jQuery插件---Tip(提示框)
对jQuery相信很多同学和我一样平时都是拿来主义,没办法,要怪只能怪jQuery太火了,各种插件基本能满足平时的要求.但是这毕竟不是长久之道,古人云:“授之以鱼,不如授之以渔”. 为了方便之前没有接 ...
- 自己动手写fullPage插件
仿造fullPage.js https://alvarotrigo.com/fullPage/#firstPage 自己参照网上教程写了一个,加了注释.主要是练习造轮子的能力,需求是不断变化的只拿来用 ...
- 发布vue插件到npm上
总体分为2个步骤 一,先写好插件 二,发布到npm上面 一,写vue插件 vue有一个开放的方法install,在vue插件需要写在这个方法里面,在vue官网,里面说的很清楚,这个方法里面可以是全局方 ...
- 自己写jquery插件之模版插件高级篇(一)
需求场景 最近项目改版中,发现很多地方有这样一个操作(见下图gif动画演示),很多地方都有用到.这里不讨论它的用户体验怎么样. 仅仅是从复用的角度,如果每个页面都去写text和select元素,两个b ...
随机推荐
- 如何将IOS版本的更新下载文件指向到自己的服务器
针对那些使用企业签名但是没有发布到AppSotre的IOS版本APP自动更新问题解决方案: 在apicloud中是这样说明的: 因为要填写plist地址所以不能向安卓那样直接填写服务器文件地址,但是直 ...
- C# 每月第一天和最后一天
//每月第一天 - DateTime.Now.Day); //每月最后一天 var endTime=DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMont ...
- ajax调用WebService 不能跨域
http://www.cnblogs.com/dojo-lzz/p/4265637.html "Access-Control-Allow-Origin":'http://local ...
- Extjs Window用法详解 2 打印具体应用
Extjs 中的按钮元素 { xtype: 'buttongroup', title: '打印', items: [ me.tsbDel = Ext.create('Ext.button.Button ...
- Java登陆拦截器
package com.beidou.warehouseerp.interceptor; import com.alibaba.fastjson.JSON; import com.beidou.war ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- 微信WeixinJSBridge的接口使用
以下都要包含weixinApi.js(见底部git里的js文件) 1).分享 WeixinApi.ready(function(Api) { // 微信分享的数据 var wxData = { &qu ...
- 【LOJ】#2108. 「JLOI2015」装备购买
题解 换成long double才过--出题人丧心病狂卡精度 只要按照费用排序从小到大排序,一个个插入线性基,插入的时候加上费用即可 代码 #include <bits/stdc++.h> ...
- Linux 获取目录中最后一个文件的名字
find /application/docker_hub/logs/fof1private/amount_dev -type l | xargs basename
- MaximumClique HDU1530
最大团问题入门题 最基础的方法是dfs 参考: 首先,我们先得到后几个点组成的最大团到底是多大,(最开始的时候肯定是最后一个点单独构成一个最大团,点数为1)然后我们再 DFS: 初始化: 从一个点 u ...