vue插件--仿微信小程序showModel实现模态提示窗功能


Posted in Javascript onAugust 19, 2020

效果图:

vue插件--仿微信小程序showModel实现模态提示窗功能

下面是源码:

index.js

import Vue from 'vue';
import model from './model.vue';
 
 
export default {
 install(Vue) {
 
 const defaults = {
  show: false,
  mask: true,
  title: '提示',
  content: '这是正文',
  confirmButton: true,
  cancelButton: true,
  confirmText: '确认',
  cancelText: '取消',
  cancelCallBack: () => {},
  confirmCallBack: () => {}
 };
 
 const modelVueConstructor = Vue.extend(model);
 
 Vue.prototype.$model = (options = {}) => {
  if (Vue.prototype.$isServer) return;
  options = Object.assign({}, defaults, options);
  let parent = document.body ;
  let instance = new modelVueConstructor({
  el: document.createElement('div'),
  data: options
  });
  parent.appendChild(instance.$el);
 
  return instance;
 };
 
 },
};

model.vue

<template>
 <div v-if="show" class="model-container">
 <div class="model-main">
  <div class="model-title">{{title}}</div>
  <div class="model-content" v-html="content"></div>
  <div class="model-buttons">
  <button v-if="cancelButton" @click="cancelClick" class="button">{{cancelText}}</button>
  <button v-if="confirmButton" @click="confirmClick" class="button confirm">{{confirmText}}</button>
  </div>
 </div>
 <div v-show="mask" class="model-mask"></div>
 </div>
 
</template>
 
<script type="text/babel">
 export default {
 data() {
 return {
 show: false,
 mask: true,
 title: '提示',
 content: '这是正文',
 confirmButton: true,
 cancelButton: true,
 confirmText: '确认',
 cancelText: '取消',
 cancelCallBack: () => {},
 confirmCallBack: () => {}
 };
 },
 methods: {
 cancelClick(){
  this.show = false;
  this.cancelCallBack();
 },
 confirmClick(){
  this.show = false;
  this.confirmCallBack();
 }
 }
 };
</script>
<style lang="less" scoped>
 .model-container{
 width: 100%;
 height: 100vh;
 position: fixed;
 top: 0;
 left: 0;
 z-index: var(--model-index);
 display: flex;
 justify-content: center;
 align-items: center;
 .model-main{
  position: relative;
  z-index: 9;
  width: 80%;
  background-color: #ffffff;
  border-radius: 10px;
  overflow: hidden;
  text-align: center;
  .model-title{
  font-size: 18px;
  color: #333;
  width: 100%;
  padding: 18px;
  font-weight: bold;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  }
  .model-content{
  font-size: 16px;
  color: #666;
  padding: 10px;
  padding-top: 0px;
  padding-bottom: 20px;
  }
  .model-buttons{
  width: 100%;
  display: flex;
  align-items: center;
  .button{
   flex: 1;
   padding: 18px 10px;
   overflow: hidden;
   white-space: nowrap;
   text-overflow: ellipsis;
   font-size: 16px;
   outline: none;
   background-color: #ffffff;
   border-top: 1px solid #f2f2f2;
   border-right: 1px solid #f2f2f2;
   &.confirm{
   color: var(--theme);
   font-weight: bold;
   }
   &:last-child{
   border-right: 0;
   }
   &:active{
   background-color: #f2f2f2;
   }
  }
  }
 }
 .model-mask{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  background-color: rgba(0,0,0,0.45);
 }
 }
</style>

通过添加实例方法,把插件添加到vue.prototype上来实现。

在使用之前需要将插件挂载到Vue全局实例上:

main.js

import VueModel from './components/model/index.js';
Vue.use(VueModel);

完成上述条件后,就可以在你的vue项目中使用啦:

this.$model({
 show: true,
 title: "提示",
 content: "提示内容",
 cancelButton: true,
 confirmCallBack: () => {
 console.log("确认");
 },
 cancelCallBack: () => {
 console.log("取消");
 }
});

总结

到此这篇关于vue插件--仿微信小程序showModel实现模态提示窗的文章就介绍到这了,更多相关微信小程序showModel实现模态提示窗内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
关于jquery动态增减控件的一些想法和小插件
Aug 01 Javascript
多种方法实现JS动态添加事件
Nov 01 Javascript
JS实现定时页面弹出类似QQ新闻的提示框
Nov 07 Javascript
Javascript中对象继承的实现小例
May 12 Javascript
node.js+express制作网页计算器
Jan 17 Javascript
jQuery事件绑定on()与弹窗实现代码
Apr 28 Javascript
node.js express中app.param的用法详解
Jul 16 Javascript
vue 通过下拉框组件学习vue中的父子通讯
Dec 19 Javascript
Vue2.5 结合 Element UI 之 Table 和 Pagination 组件实现分页功能
Jan 26 Javascript
详解三种方式解决vue中v-html元素中标签样式
Nov 22 Javascript
vue 使用 canvas 实现手写电子签名
Mar 06 Javascript
jquery+css3实现的经典弹出层效果示例
May 16 jQuery
jQuery实现评论模块
Aug 19 #jQuery
javaScript代码飘红报错看不懂?读完这篇文章再试试
Aug 19 #Javascript
jQuery实现简单评论功能
Aug 19 #jQuery
原生JS实现多条件筛选
Aug 19 #Javascript
微信小程序wx.getUserInfo授权获取用户信息(头像、昵称)的实现
Aug 19 #Javascript
微信小程序通过websocket实时语音识别的实现代码
Aug 19 #Javascript
JS实现炫酷雪花飘落效果
Aug 19 #Javascript
You might like
新浪SAE云平台下使用codeigniter的数据库配置
2014/06/12 PHP
thinkPHP框架中layer.js的封装与使用方法示例
2019/01/18 PHP
PHP PDOStatement::fetchAll讲解
2019/01/31 PHP
laravel 框架结合关联查询 when()用法分析
2019/11/22 PHP
Aster vs Newbee BO5 第三场2.19
2021/03/10 DOTA
Add Formatted Data to a Spreadsheet
2007/06/12 Javascript
根据地区不同显示时间的javascript代码
2007/08/13 Javascript
JQuery 学习笔记 选择器之四
2009/07/23 Javascript
javascript验证只能输入数字和一个小数点示例
2013/10/21 Javascript
js实现点小图看大图效果的思路及示例代码
2013/10/28 Javascript
jquery获取当前日期的方法
2015/01/14 Javascript
javascript密码强度校验代码(两种方法)
2015/08/10 Javascript
js基础之DOM中元素对象的属性方法详解
2016/10/28 Javascript
jQuery动态生成Bootstrap表格
2016/11/01 Javascript
canvas绘制万花筒效果(代码分享)
2017/01/20 Javascript
bootstrap日期控件问题(双日期、清空等问题解决)
2017/04/19 Javascript
vue 监听键盘回车事件详解 @keyup.enter || @keyup.enter.native
2018/08/25 Javascript
微信小程序实现图片滚动效果示例
2018/12/05 Javascript
nodejs中方法和模块用法示例
2018/12/24 NodeJs
JavaScript学习笔记之基于定时器实现图片无缝滚动功能详解
2019/01/09 Javascript
[02:51]2014DOTA2 TI小组赛总结中国军团全部进军钥匙球馆
2014/07/15 DOTA
详解Python判定IP地址合法性的三种方法
2018/03/06 Python
Centos 升级到python3后pip 无法使用的解决方法
2018/06/12 Python
查找python项目依赖并生成requirements.txt的方法
2018/07/10 Python
python版大富翁源代码分享
2018/11/19 Python
Pycharm使用远程linux服务器conda/python环境在本地运行的方法(图解))
2019/12/09 Python
html5 冒号分隔符对齐的实现
2019/07/31 HTML / CSS
DERMAdoctor官网:美国著名皮肤护理品牌
2019/07/06 全球购物
葡萄牙航空官方网站:TAP Air Portugal
2019/10/31 全球购物
大学运动会加油稿200字(5篇)
2014/09/27 职场文书
《中国古代诗歌散文欣赏》高中语文教材
2019/08/20 职场文书
MySQL中使用or、in与union all在查询命令下的效率对比
2021/05/26 MySQL
Python一些基本的图像操作和处理总结
2021/06/23 Python
SpringCloud的JPA连接PostgreSql的教程
2021/06/26 Java/Android
JavaScript正则表达式实现注册信息校验功能
2022/05/30 Java/Android
Fluentd搭建日志收集服务
2022/09/23 Servers