详解vue使用vue-layer-mobile组件实现toast,loading效果


Posted in Javascript onAugust 31, 2018

安装vue-layer-mobile

// 当前最新版本 1.2.0 
npm install vue-layer-mobile
// 如新版遇到问题可回退旧版本 
npm install vue-layer-mobile@1.0.0

此版本安装后启动会报错,报错提示将css里的display:box改成display:flex;

在main.js里面全局引用

import 'vue-layer-mobile/need/layer.css'
import layer from 'vue-layer-mobile'
Vue.use(layer)

使用方法

toast

this.$layer.toast({
 className: 'icon-check', // 图标className 如果为空 toast位置位于下方,否则居中 
 content: '提示文字',
 time: 2000 // 自动消失时间 toast类型默认消失时间为2000毫秒 
})

toast: 文字和图标:

this.$layer.toast({
 icon: 'icon-check', // 图标clssName 如果为空 toast位置位于下方,否则居中
 content: '提示文字',
 time: 2000 // 自动消失时间 toast类型默认消失时间为2000毫秒
})

loading

this.$layer.loading('加载中...')

dialog:

this.$layer.dialog({
 title: ['这是标题', 'background:red;'], // 第一个是标题内容 第二个是标题栏的style(可以为空)
 content: '这是内容',
 contentClass: 'className',
 btn: ['确定']
 time: 2000
})
// 如果有btn
.then(function (res){
 // res为0时是用户点击了左边 为1时用户点击了右边
 let position = res === 0 ? 'left' : 'right'
  console.log(position)
 })

 footer:

this.$layer.footer({
 content: '这是内容',
 btn: ['取消', '选项1', '选项2']
})
// 如果有btn
.then(function (res){
 var text = res==0 ? '取消' : '选项'+res
 console.log(text)
})

关闭弹窗

this.$layer.close()

如果在setTimeout里使用请注意this指向问题

另外,我感觉toast没有图标的时候默认显示在底部,不太好;loading的样式也有不太好看,自己改了layer-mobile的样式。

在oppo和vivo的webview中会出现,layer加载条无法关闭,非要手动点一下屏幕才能关闭。
解决办法:将this.$layer.close()写在方法最下面

.layui-m-layer{position:relative;z-index:19891014}.layui-m-layer *{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}.layui-m-layermain,.layui-m-layershade{position:fixed;left:0;top:0;width:100%;height:100%}.layui-m-layershade{background-color:rgba(0,0,0,0);pointer-events:auto}.layui-m-layermain{display:table;font-family:Helvetica,arial,sans-serif;pointer-events:none}.layui-m-layermain .layui-m-layersection{display:table-cell;vertical-align:middle;text-align:center}.layui-m-layerchild{position:relative;display:inline-block;text-align:left;background-color:#fff;font-size:14px;border-radius:5px;box-shadow:0 0 8px rgba(0,0,0,.1);pointer-events:auto;-webkit-overflow-scrolling:touch;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:.2s;animation-duration:.2s}@-webkit-keyframes layui-m-anim-scale{0%{opacity:0;-webkit-transform:scale(.5);transform:scale(.5)}100%{opacity:1;-webkit-transform:scale(1);transform:scale(1)}}@keyframes layui-m-anim-scale{0%{opacity:0;-webkit-transform:scale(.5);transform:scale(.5)}100%{opacity:1;-webkit-transform:scale(1);transform:scale(1)}}.layui-m-anim-scale{animation-name:layui-m-anim-scale;-webkit-animation-name:layui-m-anim-scale}@-webkit-keyframes layui-m-anim-up{0%{opacity:0;-webkit-transform:translateY(800px);transform:translateY(800px)}100%{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}}@keyframes layui-m-anim-up{0%{opacity:0;-webkit-transform:translateY(800px);transform:translateY(800px)}100%{opacity:1;-webkit-transform:translateY(0);transform:translateY(0)}}.layui-m-anim-up{-webkit-animation-name:layui-m-anim-up;animation-name:layui-m-anim-up}.layui-m-layer0 .layui-m-layerchild{width:90%;max-width:640px}.layui-m-layer1 .layui-m-layerchild{border:none;border-radius:0}.layui-m-layer2 .layui-m-layerchild{width:auto;max-width:260px;min-width:40px;border:none;background:0 0;box-shadow:none;color:#fff}.layui-m-layerchild h3{margin:0;padding:0 10px;height:60px;line-height:60px;font-size:16px;font-weight:400;border-radius:5px 5px 0 0;text-align:center}.layui-m-layerbtn span,.layui-m-layerchild h3{text-overflow:ellipsis;overflow:hidden;white-space:nowrap}.layui-m-layercont{padding:50px 30px;line-height:22px;text-align:center;color:#fff;}.layui-m-layer1 .layui-m-layercont{padding:0;text-align:left}.layui-m-layer2 .layui-m-layercont{text-align:center;padding:0;line-height:0}.layui-m-layer2 .layui-m-layercont i{width:25px;height:25px;margin-left:8px;display:inline-block;background-color:#fff;border-radius:100%;-webkit-animation:layui-m-anim-loading 1.4s infinite ease-in-out;animation:layui-m-anim-loading 1.4s infinite ease-in-out;-webkit-animation-fill-mode:both;animation-fill-mode:both}.layui-m-layerbtn,.layui-m-layerbtn span{position:relative;text-align:center;border-radius:0 0 5px 5px}.layui-m-layer2 .layui-m-layercont p{margin-top:15px;color:white;margin-bottom:10px}@-webkit-keyframes layui-m-anim-loading{0%,100%,80%{transform:scale(0);-webkit-transform:scale(0)}40%{transform:scale(.8);-webkit-transform:scale(.8)}}@keyframes layui-m-anim-loading{0%,100%,80%{transform:scale(0);-webkit-transform:scale(0)}40%{transform:scale(.8);-webkit-transform:scale(.8)}}.layui-m-layer2 .layui-m-layercont i:first-child{margin-left:0;-webkit-animation-delay:-.32s;animation-delay:-.32s}.layui-m-layer2 .layui-m-layercont i.layui-m-layerload{-webkit-animation-delay:-.16s;animation-delay:-.16s}.layui-m-layer2 .layui-m-layercont>div{line-height:22px;padding-top:7px;margin-bottom:20px;font-size:14px}.layui-m-layerbtn{display:flex;display:-moz-box;display:-webkit-box;width:100%;height:50px;line-height:50px;font-size:0;border-top:1px solid #D0D0D0;background-color:#F2F2F2}.layui-m-layerbtn span{display:block;-moz-box-flex:1;box-flex:1;-webkit-box-flex:1;font-size:14px;cursor:pointer}.layui-m-layerbtn span[yes]{color:#40AFFE}.layui-m-layerbtn span[no]{border-right:1px solid #D0D0D0;border-radius:0 0 0 5px}.layui-m-layerbtn span:active{background-color:#F6F6F6}.layui-m-layerend{position:absolute;right:7px;top:10px;width:30px;height:30px;border:0;font-weight:400;background:0 0;cursor:pointer;-webkit-appearance:none;font-size:30px}.layui-m-layerend::after,.layui-m-layerend::before{position:absolute;left:5px;top:15px;content:'';width:18px;height:1px;background-color:#999;transform:rotate(45deg);-webkit-transform:rotate(45deg);border-radius:3px}.layui-m-layerend::after{transform:rotate(-45deg);-webkit-transform:rotate(-45deg)}body .layui-m-layer .layui-m-layer-footer{position:fixed;width:95%;max-width:100%;margin:0 auto;left:0;right:0;bottom:10px;background:0 0}.layui-m-layer-footer .layui-m-layercont{padding:20px;border-radius:5px 5px 0 0;background-color:rgba(255,255,255,.8)}.layui-m-layer-footer .layui-m-layerbtn{display:block;height:auto;background:0 0;border-top:none}.layui-m-layer-footer .layui-m-layerbtn span{background-color:rgba(255,255,255,.8)}.layui-m-layer-footer .layui-m-layerbtn span[no]{color:#FD482C;border-top:1px solid #c2c2c2;border-radius:0 0 5px 5px}.layui-m-layer-footer .layui-m-layerbtn span[yes]{margin-top:10px;border-radius:5px}body .layui-m-layer .layui-m-layer-msg{width:auto;max-width:90%;margin:0 auto;bottom:0;background-color:rgba(0,0,0,.7);color:#fff}.layui-m-layer-msg .layui-m-layercont{padding:10px 20px}
.layui-m-layer2 .layui-m-layercont{background: rgba(0,0,0,.8);padding:10px 15px;border-radius: 6px}
@font-face {font-family: "iconfont";
 src: url('iconfont.eot?t=1481268530942'); /* IE9*/
 src: url('iconfont.eot?t=1481268530942#iefix') format('embedded-opentype'), /* IE6-IE8 */
 url('iconfont.woff?t=1481268530942') format('woff'), /* chrome, firefox */
 url('iconfont.ttf?t=1481268530942') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
 url('iconfont.svg?t=1481268530942#iconfont') format('svg'); /* iOS 4.1- */
}

.iconfont {
 font-family:"iconfont" !important;
 font-size:16px;
 font-style:normal;
 -webkit-font-smoothing: antialiased;
 -webkit-text-stroke-width: 0.2px;
 -moz-osx-font-smoothing: grayscale;
  color:#fff;
}

.icon-appreciate:before { content: "\e644"; }

.icon-check:before { content: "\e645"; }

.icon-close:before { content: "\e646"; }

.icon-warn:before { content: "\e663"; }

.icon-delete:before { content: "\e6b4"; }

.icon-notice:before { content: "\e70a"; }

.icon-github:before { content: "\e66c"; }

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

Javascript 相关文章推荐
js兼容标准的表格变色效果
Jun 28 Javascript
JavaScript学习笔记之获取当前目录的实现代码
Dec 14 Javascript
jQuery学习笔记之jQuery构建函数的7种方法
Jun 03 Javascript
jQuery+Ajax+PHP弹出层异步登录效果(附源码下载)
May 27 Javascript
JavaScript中push(),join() 函数 实例详解
Sep 06 Javascript
Vue.js列表渲染绑定jQuery插件的正确姿势
Jun 29 jQuery
jQuery 实现图片的依次加载图片功能
Jul 06 jQuery
解决vue.js在编写过程中出现空格不规范报错的问题
Sep 20 Javascript
vue + element-ui实现简洁的导入导出功能
Dec 22 Javascript
Vue项目中设置背景图片方法
Feb 21 Javascript
Layui组件Table绑定行点击事件和获取行数据的方法
Aug 19 Javascript
JS中准确判断变量类型的方法
Jun 01 Javascript
Angular2之二级路由详解
Aug 31 #Javascript
基于jQuery ztree实现表格风格的树状结构
Aug 31 #jQuery
详解Vue CLI3配置之filenameHashing使用和源码设计使用和源码设计
Aug 31 #Javascript
Bootstrap Table 双击、单击行获取该行及全表内容
Aug 31 #Javascript
vue通过滚动行为实现从列表到详情,返回列表原位置的方法
Aug 31 #Javascript
详解小程序输入框闪烁及重影BUG解决方案
Aug 31 #Javascript
javascript闭包的使用之按钮切换功能
Aug 30 #Javascript
You might like
新版mysql+apache+php Linux安装指南
2006/10/09 PHP
PHP针对常规模板引擎中与CSS/JSON冲突的解决方法
2014/08/19 PHP
PHP利用func_get_args和func_num_args函数实现函数重载实例
2014/11/12 PHP
基于thinkPHP实现的微信自定义分享功能示例
2016/09/23 PHP
js removeChild 障眼法 可能出现的错误
2009/10/06 Javascript
40个有创意的jQuery图片和内容滑动及弹出插件收藏集之二
2011/12/31 Javascript
JavaScript通过事件代理高亮显示表格行的方法
2015/05/27 Javascript
JavaScript对象数组排序函数及六个用法
2015/12/23 Javascript
js实现的简单图片浮动效果完整实例
2016/05/10 Javascript
Bootstrap的Refresh Icon也spin起来
2016/07/13 Javascript
JS中使用mailto实现将用户在网页中输入的内容传递到本地邮件客户端
2016/10/08 Javascript
原生javascript上传图片带进度条【实例分享】
2017/04/06 Javascript
捕获未处理的Promise错误方法
2017/10/13 Javascript
Vue.js中的computed工作原理
2018/03/22 Javascript
在vue中阻止浏览器后退的实例
2019/11/06 Javascript
javascript读取本地文件和目录方法详解
2020/08/06 Javascript
[02:39]DOTA2英雄基础教程 极限穿梭编织者
2013/12/05 DOTA
基于Python实现的扫雷游戏实例代码
2014/08/01 Python
python中关于for循环的碎碎念
2017/06/30 Python
Python基于高斯消元法计算线性方程组示例
2018/01/17 Python
Python实现获取本地及远程图片大小的方法示例
2018/07/21 Python
一文带你了解Python中的字符串是什么
2018/11/20 Python
Python eval的常见错误封装及利用原理详解
2019/03/26 Python
Python实现计算对象的内存大小示例
2019/07/10 Python
Flask框架钩子函数功能与用法分析
2019/08/02 Python
python实现xml转json文件的示例代码
2020/12/30 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
2021/01/27 Python
高尔夫球鞋、服装、手套和装备:FootJoy
2018/12/15 全球购物
英国豪华家具和家居用品购物网站:Teddy Beau
2020/10/12 全球购物
介绍一下Linux文件的记录形式
2012/04/18 面试题
介绍一些UNIX常用简单命令
2014/11/11 面试题
考博自荐信
2013/10/25 职场文书
积极分子思想汇报
2014/01/04 职场文书
篮球兴趣小组活动总结
2014/07/07 职场文书
《实心球》教学反思
2016/02/23 职场文书
详解在SQLPlus中实现上下键翻查历史命令的功能
2022/03/18 SQL Server