VUE:vue中使用toast

2024-06-06 13:32

本文主要是介绍VUE:vue中使用toast,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

新建JS
/*** 自定义 提示框( Toast )组件*/
var Toast = {};
var showToast = false, // 存储toast显示状态showLoad = false, // 存储loading显示状态toastVM = null, // 存储toast vmloadNode = null; // 存储loading节点元素Toast.install = function (Vue, options) {// 参数var opt = {defaultType: 'bottom',duration: '1500',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;
新建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;*/font-size: 30px;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;}
}
main.js中全局引入
// 全局引入Toast
import './components/Toast/toast.css';
import Toast from './components/Toast/index';
Vue.use(Toast);
页面调用
    <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()">该商标分类已加入购物车!</button><button @click="openBottom()">bottom</button><button @click="openLoading()">loading</button></div>
  methods: {openCenter(){this.$toast.center('该商标分类已加入购物车!');},
效果图

在这里插入图片描述

这篇关于VUE:vue中使用toast的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1036247

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解