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实现的移入页面上空文本框时,让它变为焦点,移出清除焦点
Jul 26 Javascript
js匿名函数作为函数参数详解
Jun 01 Javascript
Angular中$cacheFactory的作用和用法实例详解
Aug 19 Javascript
详谈JS中数组的迭代方法和归并方法
Aug 11 Javascript
vue不通过路由直接获取url中参数的方法示例
Aug 24 Javascript
Vue导出json数据到Excel电子表格的示例
Dec 04 Javascript
cnpm加速Angular项目创建的方法
Sep 07 Javascript
从0到1构建vueSSR项目之node以及vue-cli3的配置
Mar 07 Javascript
使用Vue-Awesome-Swiper实现旋转叠加轮播效果&amp;平移轮播效果
Aug 16 Javascript
nuxt框架中对vuex进行模块化设置的实现方法
Sep 06 Javascript
如何在vue中使用jointjs过程解析
May 29 Javascript
OpenLayers3实现地图鹰眼以及地图比例尺的添加
Sep 25 Javascript
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
php中文验证码实现方法
2015/06/18 PHP
PHP错误和异常处理功能模块示例
2016/11/12 PHP
php实现简单的守护进程创建、开启与关闭操作
2019/08/13 PHP
php实现分页功能的详细实例方法
2019/09/29 PHP
phpstorm激活码2020附使用详细教程
2020/09/25 PHP
js no-repeat写法 背景不重复
2009/03/18 Javascript
javascript RadioButtonList获取选中值
2009/04/09 Javascript
window.print打印指定div实例代码
2013/12/13 Javascript
jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果
2015/10/28 Javascript
JS与jQuery实现隔行变色的方法
2016/09/09 Javascript
JS定时检测任务任务完成后执行下一步的解决办法
2016/12/22 Javascript
10分钟上手vue-cli 3.0 入门介绍
2018/04/04 Javascript
JS验证输入的是否是数字及保留几位小数问题
2018/05/09 Javascript
jQuery实现的鼠标拖动浮层功能示例【拖动div等任何标签】
2018/12/29 jQuery
vue中解决拖拽改变存在iframe的div大小时卡顿问题
2020/07/22 Javascript
vue设置默认首页的操作
2020/08/12 Javascript
[41:08]TNC vs VG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python使用ctypes模块调用windowsapi获取系统版本示例
2014/04/17 Python
浅析python打包工具distutils、setuptools
2018/04/20 Python
Django REST framework 视图和路由详解
2019/07/19 Python
django美化后台django-suit的安装配置操作
2020/07/12 Python
CSS3弹性布局内容对齐(justify-content)属性使用详解
2017/07/31 HTML / CSS
使用phonegap获取设备的一些信息方法
2017/03/31 HTML / CSS
eVitamins日本:在线购买折扣维生素、补品和草药
2019/04/04 全球购物
入党申请书自我鉴定
2013/10/12 职场文书
财务会计实习报告体会
2013/12/20 职场文书
最新大学职业规划书范文
2013/12/30 职场文书
网页美工求职信
2014/02/15 职场文书
计生专干事迹
2014/05/28 职场文书
班级心理活动总结
2014/07/04 职场文书
小学教师师德师风个人整改措施
2014/09/18 职场文书
2015年评职称工作总结范文
2015/04/20 职场文书
Python爬虫之爬取哔哩哔哩热门视频排行榜
2021/04/28 Python
详解python网络进程
2021/06/15 Python
叶县这家生产军用电台的兵工厂,人称“四机部”,走出一上将
2022/02/18 无线电
vue本地构建热更新卡顿的问题“75 advanced module optimization”完美解决方案
2022/08/05 Vue.js