vue 自定义提示框(Toast)组件的实现代码


Posted in Javascript onAugust 17, 2018

1.自定义 提示框 组件

src / components / Toast / index.js

/**
 * 自定义 提示框( Toast )组件
 */
var Toast = {};
var showToast = false, // 存储toast显示状态
 showLoad = false, // 存储loading显示状态
 toastVM = null, // 存储toast vm
 loadNode = null; // 存储loading节点元素
 
Toast.install = function (Vue, options) {
 // 参数
 var opt = {
  defaultType: 'bottom',
  duration: '2500',
  wordWrap: false
 };
 for (var property in options) {
  opt[property] = options[property];
 }
 
 Vue.prototype.$toast = function (tips, type) {
 
  var curType = type ? type : opt.defaultType;
  var wordWrap = opt.wordWrap ? 'lx-word-wrap' : '';
  var style = opt.width ? 'style="width: ' + opt.width + '"' : '';
  var tmp = '<div v-show="show" :class="type" class="lx-toast ' + wordWrap + '" ' + style + '>{{tip}}</div>';
 
  if (showToast) {
   // 如果toast还在,则不再执行
   return;
  }
  if (!toastVM) {
   var toastTpl = Vue.extend({
    data: function () {
     return {
      show: showToast,
      tip: tips,
      type: 'lx-toast-' + curType
     }
    },
    template: tmp
   });
   toastVM = new toastTpl()
   var tpl = toastVM.$mount().$el;
   document.body.appendChild(tpl);
  }
  toastVM.type = 'lx-toast-' + curType;
  toastVM.tip = tips;
  toastVM.show = showToast = true;
 
  setTimeout(function () {
   toastVM.show = showToast = false;
  }, opt.duration)
 };
 
 ['bottom', 'center', 'top'].forEach(function (type) {
  Vue.prototype.$toast[type] = function (tips) {
   return Vue.prototype.$toast(tips, type)
  }
 });
 
 Vue.prototype.$loading = function (tips, type) {
  if (type == 'close') {
   loadNode.show = showLoad = false;
  } else {
   if (showLoad) {
    // 如果loading还在,则不再执行
    return;
   }
   var loadTpl = Vue.extend({
    data: function () {
     return {
      show: showLoad
     }
    },
    template: '<div v-show="show" class="lx-load-mark"><div class="lx-load-box"><div class="lx-loading"><div class="loading_leaf loading_leaf_0"></div><div class="loading_leaf loading_leaf_1"></div><div class="loading_leaf loading_leaf_2"></div><div class="loading_leaf loading_leaf_3"></div><div class="loading_leaf loading_leaf_4"></div><div class="loading_leaf loading_leaf_5"></div><div class="loading_leaf loading_leaf_6"></div><div class="loading_leaf loading_leaf_7"></div><div class="loading_leaf loading_leaf_8"></div><div class="loading_leaf loading_leaf_9"></div><div class="loading_leaf loading_leaf_10"></div><div class="loading_leaf loading_leaf_11"></div></div><div class="lx-load-content">' + tips + '</div></div></div>'
   });
   loadNode = new loadTpl();
   var tpl = loadNode.$mount().$el;
 
   document.body.appendChild(tpl);
   loadNode.show = showLoad = true;
  }
 };
 
 ['open', 'close'].forEach(function (type) {
  Vue.prototype.$loading[type] = function (tips) {
   return Vue.prototype.$loading(tips, type)
  }
 });
}
 
// 向外暴露接口
module.exports = Toast;

src / components / Toast / toast.css

/**
 * Toast 样式
 */
.lx-toast {
 position: fixed;
 bottom: 100px;
 left: 50%;
 box-sizing: border-box;
 max-width: 80%;
 height: 40px;
 line-height: 20px;
 padding: 10px 20px;
 transform: translateX(-50%);
 -webkit-transform: translateX(-50%);
 text-align: center;
 z-index: 9999;
 font-size: 14px;
 color: #fff;
 border-radius: 5px;
 background: rgba(0, 0, 0, 0.7);
 animation: show-toast .5s;
 -webkit-animation: show-toast .5s;
 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
}
 
.lx-toast.lx-word-wrap {
 width: 80%;
 white-space: inherit;
 height: auto;
}
 
.lx-toast.lx-toast-top {
 top: 50px;
 bottom: inherit;
}
 
.lx-toast.lx-toast-center {
 top: 50%;
 margin-top: -20px;
 bottom: inherit;
}
 
@keyframes show-toast {
 from {
  opacity: 0;
  transform: translate(-50%, -10px);
  -webkit-transform: translate(-50%, -10px);
 }
 to {
  opacity: 1;
  transform: translate(-50%, 0);
  -webkit-transform: translate(-50%, 0);
 }
}
 
.lx-load-mark {
 position: fixed;
 left: 0;
 top: 0;
 width: 100%;
 height: 100%;
 z-index: 9999;
}
 
.lx-load-box {
 position: fixed;
 z-index: 3;
 width: 7.6em;
 min-height: 7.6em;
 top: 180px;
 left: 50%;
 margin-left: -3.8em;
 background: rgba(0, 0, 0, 0.7);
 text-align: center;
 border-radius: 5px;
 color: #FFFFFF;
}
 
.lx-load-content {
 margin-top: 64%;
 font-size: 14px;
}
 
.lx-loading {
 position: absolute;
 width: 0px;
 left: 50%;
 top: 38%;
}
 
.loading_leaf {
 position: absolute;
 top: -1px;
 opacity: 0.25;
}
 
.loading_leaf:before {
 content: " ";
 position: absolute;
 width: 9.14px;
 height: 3.08px;
 background: #d1d1d5;
 box-shadow: rgba(0, 0, 0, 0.0980392) 0px 0px 1px;
 border-radius: 1px;
 -webkit-transform-origin: left 50% 0px;
 transform-origin: left 50% 0px;
}
 
.loading_leaf_0 {
 -webkit-animation: opacity-0 1.25s linear infinite;
 animation: opacity-0 1.25s linear infinite;
}
 
.loading_leaf_0:before {
 -webkit-transform: rotate(0deg) translate(7.92px, 0px);
 transform: rotate(0deg) translate(7.92px, 0px);
}
 
.loading_leaf_1 {
 -webkit-animation: opacity-1 1.25s linear infinite;
 animation: opacity-1 1.25s linear infinite;
}
 
.loading_leaf_1:before {
 -webkit-transform: rotate(30deg) translate(7.92px, 0px);
 transform: rotate(30deg) translate(7.92px, 0px);
}
 
.loading_leaf_2 {
 -webkit-animation: opacity-2 1.25s linear infinite;
 animation: opacity-2 1.25s linear infinite;
}
 
.loading_leaf_2:before {
 -webkit-transform: rotate(60deg) translate(7.92px, 0px);
 transform: rotate(60deg) translate(7.92px, 0px);
}
 
.loading_leaf_3 {
 -webkit-animation: opacity-3 1.25s linear infinite;
 animation: opacity-3 1.25s linear infinite;
}
 
.loading_leaf_3:before {
 -webkit-transform: rotate(90deg) translate(7.92px, 0px);
 transform: rotate(90deg) translate(7.92px, 0px);
}
 
.loading_leaf_4 {
 -webkit-animation: opacity-4 1.25s linear infinite;
 animation: opacity-4 1.25s linear infinite;
}
 
.loading_leaf_4:before {
 -webkit-transform: rotate(120deg) translate(7.92px, 0px);
 transform: rotate(120deg) translate(7.92px, 0px);
}
 
.loading_leaf_5 {
 -webkit-animation: opacity-5 1.25s linear infinite;
 animation: opacity-5 1.25s linear infinite;
}
 
.loading_leaf_5:before {
 -webkit-transform: rotate(150deg) translate(7.92px, 0px);
 transform: rotate(150deg) translate(7.92px, 0px);
}
 
.loading_leaf_6 {
 -webkit-animation: opacity-6 1.25s linear infinite;
 animation: opacity-6 1.25s linear infinite;
}
 
.loading_leaf_6:before {
 -webkit-transform: rotate(180deg) translate(7.92px, 0px);
 transform: rotate(180deg) translate(7.92px, 0px);
}
 
.loading_leaf_7 {
 -webkit-animation: opacity-7 1.25s linear infinite;
 animation: opacity-7 1.25s linear infinite;
}
 
.loading_leaf_7:before {
 -webkit-transform: rotate(210deg) translate(7.92px, 0px);
 transform: rotate(210deg) translate(7.92px, 0px);
}
 
.loading_leaf_8 {
 -webkit-animation: opacity-8 1.25s linear infinite;
 animation: opacity-8 1.25s linear infinite;
}
 
.loading_leaf_8:before {
 -webkit-transform: rotate(240deg) translate(7.92px, 0px);
 transform: rotate(240deg) translate(7.92px, 0px);
}
 
.loading_leaf_9 {
 -webkit-animation: opacity-9 1.25s linear infinite;
 animation: opacity-9 1.25s linear infinite;
}
 
.loading_leaf_9:before {
 -webkit-transform: rotate(270deg) translate(7.92px, 0px);
 transform: rotate(270deg) translate(7.92px, 0px);
}
 
.loading_leaf_10 {
 -webkit-animation: opacity-10 1.25s linear infinite;
 animation: opacity-10 1.25s linear infinite;
}
 
.loading_leaf_10:before {
 -webkit-transform: rotate(300deg) translate(7.92px, 0px);
 transform: rotate(300deg) translate(7.92px, 0px);
}
 
.loading_leaf_11 {
 -webkit-animation: opacity-11 1.25s linear infinite;
 animation: opacity-11 1.25s linear infinite;
}
 
.loading_leaf_11:before {
 -webkit-transform: rotate(330deg) translate(7.92px, 0px);
 transform: rotate(330deg) translate(7.92px, 0px);
}
 
@-webkit-keyframes opacity-0 {
 0% {
  opacity: 0.25;
 }
 0.01% {
  opacity: 0.25;
 }
 0.02% {
  opacity: 1;
 }
 60.01% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.25;
 }
}
 
@-webkit-keyframes opacity-1 {
 0% {
  opacity: 0.25;
 }
 8.34333% {
  opacity: 0.25;
 }
 8.35333% {
  opacity: 1;
 }
 68.3433% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.25;
 }
}
 
@-webkit-keyframes opacity-2 {
 0% {
  opacity: 0.25;
 }
 16.6767% {
  opacity: 0.25;
 }
 16.6867% {
  opacity: 1;
 }
 76.6767% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.25;
 }
}
 
@-webkit-keyframes opacity-3 {
 0% {
  opacity: 0.25;
 }
 25.01% {
  opacity: 0.25;
 }
 25.02% {
  opacity: 1;
 }
 85.01% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.25;
 }
}
 
@-webkit-keyframes opacity-4 {
 0% {
  opacity: 0.25;
 }
 33.3433% {
  opacity: 0.25;
 }
 33.3533% {
  opacity: 1;
 }
 93.3433% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.25;
 }
}
 
@-webkit-keyframes opacity-5 {
 0% {
  opacity: 0.270958333333333;
 }
 41.6767% {
  opacity: 0.25;
 }
 41.6867% {
  opacity: 1;
 }
 1.67667% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.270958333333333;
 }
}
 
@-webkit-keyframes opacity-6 {
 0% {
  opacity: 0.375125;
 }
 50.01% {
  opacity: 0.25;
 }
 50.02% {
  opacity: 1;
 }
 10.01% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.375125;
 }
}
 
@-webkit-keyframes opacity-7 {
 0% {
  opacity: 0.479291666666667;
 }
 58.3433% {
  opacity: 0.25;
 }
 58.3533% {
  opacity: 1;
 }
 18.3433% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.479291666666667;
 }
}
 
@-webkit-keyframes opacity-8 {
 0% {
  opacity: 0.583458333333333;
 }
 66.6767% {
  opacity: 0.25;
 }
 66.6867% {
  opacity: 1;
 }
 26.6767% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.583458333333333;
 }
}
 
@-webkit-keyframes opacity-9 {
 0% {
  opacity: 0.687625;
 }
 75.01% {
  opacity: 0.25;
 }
 75.02% {
  opacity: 1;
 }
 35.01% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.687625;
 }
}
 
@-webkit-keyframes opacity-10 {
 0% {
  opacity: 0.791791666666667;
 }
 83.3433% {
  opacity: 0.25;
 }
 83.3533% {
  opacity: 1;
 }
 43.3433% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.791791666666667;
 }
}
 
@-webkit-keyframes opacity-11 {
 0% {
  opacity: 0.895958333333333;
 }
 91.6767% {
  opacity: 0.25;
 }
 91.6867% {
  opacity: 1;
 }
 51.6767% {
  opacity: 0.25;
 }
 100% {
  opacity: 0.895958333333333;
 }
}

2.全局引入

main.js

// 全局引入Toast
import './components/Toast/toast.css';
import Toast from './components/Toast/index';
Vue.use(Toast);

3.页面调用

Toast.vue

<!-- 提示框 -->

<template>

 <div>

  <!-- 标题栏 -->

  <mt-header title= "提示框" >

  <router-link to= "/" slot= "left" >

   <mt-button icon= "back" >返回</mt-button>

  </router-link>

  </mt-header>

  <!-- 内容 -->

  <button @click= "openTop()" >top</button>

  <button @click= "openCenter()" >center</button>

  <button @click= "openBottom()" >bottom</button>

  <button @click= "openLoading()" >loading</button>

 </div>

</template>

<script>

 export default {

  name: 'Toast' ,

  data(){

  return {

   //

  }

  },

  methods:{

  openTop(){

   this .$toast.top( 'top' );

  },

  openCenter(){

   this .$toast.center( 'center' );

  },

  openBottom(){

   this .$toast( 'bottom' );  // or this.$toast.bottom('bottom');

  },

  openLoading(){

   this .$loading( 'loading...' );

   let self = this ;

   setTimeout( function () {

   self.closeLoading()

   }, 2000)

  },

  closeLoading(){

   this .$loading.close();

  }

  }

 }

</script>

<style lang= "less" scoped>
 //
</style>

4.效果图

vue 自定义提示框(Toast)组件的实现代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
表单提交验证类
Jul 14 Javascript
通过ifame指向的页面高度调整iframe的高度
Oct 05 Javascript
Javascript合并表格中具有相同内容单元格示例
Aug 11 Javascript
jQuery对html元素取值与赋值的方法
Nov 20 Javascript
使用javascript为网页增加夜间模式
Jan 26 Javascript
Jquery效果大全之制作电脑健康体检得分特效附源码下载
Nov 02 Javascript
jQuery绑定自定义事件的魔法升级版
Jun 30 Javascript
vue、react等单页面项目应该这样子部署到服务器
Jan 03 Javascript
关于单文件组件.vue的使用
Sep 20 Javascript
vue+element-ui实现表格编辑的三种实现方式
Oct 31 Javascript
react 原生实现头像滚动播放的示例
Apr 21 Javascript
vue 给数组添加新对象并赋值
Apr 20 Vue.js
微信小程序实现即时通信聊天功能的实例代码
Aug 17 #Javascript
在iFrame子页面里实现模态框的方法
Aug 17 #Javascript
layer弹出层 iframe层去掉滚动条的实例代码
Aug 17 #Javascript
解决layer弹层遮罩挡住窗体的问题
Aug 17 #Javascript
layer.close()关闭进度条和Iframe窗的方法
Aug 17 #Javascript
Vue组件创建和传值的方法
Aug 17 #Javascript
webpack多入口多出口的实现方法
Aug 17 #Javascript
You might like
Adodb的十个实例(清晰版)
2006/12/31 PHP
一个PHP分页类的代码
2011/05/18 PHP
无法载入 mcrypt 扩展,请检查 PHP 配置终极解决方案
2011/07/18 PHP
深入了解 register_globals (附register_globals=off 网站打不开的解决方法)
2012/06/27 PHP
php实现的mongodb操作类
2015/05/28 PHP
jQuery Ajax之$.get()方法和$.post()方法
2009/10/12 Javascript
jQuery实现列表自动滚动循环滚动展示新闻
2014/08/22 Javascript
jquery 取子节点及当前节点属性值的方法
2014/08/24 Javascript
jquery实现简单的二级导航下拉菜单效果
2015/09/07 Javascript
深入浅析JavaScript中数据共享和数据传递
2016/04/25 Javascript
Bootstrap编写导航栏和登陆框
2016/05/30 Javascript
第一次接触神奇的Bootstrap
2016/10/14 Javascript
详解小程序循环require之坑
2019/03/08 Javascript
node.JS路径解析之PATH模块使用方法详解
2020/02/06 Javascript
Node使用koa2实现一个简单JWT鉴权的方法
2021/01/26 Javascript
Python中%r和%s的详解及区别
2017/03/16 Python
Python爬虫常用库的安装及其环境配置
2018/09/19 Python
python爬虫之自制英汉字典
2019/06/24 Python
pygame实现贪吃蛇游戏(上)
2019/10/29 Python
5款实用的python 工具推荐
2020/10/13 Python
python Cartopy的基础使用详解
2020/11/01 Python
CSS+jQuery+PHP+MySQL实现的在线答题功能
2015/04/25 HTML / CSS
新英格兰最大的特色礼品连锁店:The Paper Store
2018/07/23 全球购物
SportsDirect.com新加坡:英国第一体育零售商
2019/03/30 全球购物
Vilebrequin美国官方网上商店:法国豪华泳装品牌
2020/02/22 全球购物
机械设计及其自动化专业推荐信
2013/10/31 职场文书
安全员岗位职责
2013/11/11 职场文书
分公司任命书
2014/06/06 职场文书
党建工作整改措施
2014/10/28 职场文书
优秀校长事迹材料
2014/12/24 职场文书
《包身工》教学反思
2016/02/23 职场文书
遇事可以测出您的见识与格局
2019/09/16 职场文书
JS数组的常用方法整理
2021/03/31 Javascript
go结构体嵌套的切片数组操作
2021/04/28 Golang
教你如何用Python实现人脸识别(含源代码)
2021/06/23 Python
详解Vue3使用axios的配置教程
2022/04/29 Vue.js