/**
* Created by yx on 2017/12/21.
*/
export default {
/**
* 带按钮的弹框
* <!--自定义提示标题,内容,单个按钮事件-->
*/
showAlert:function(content,callback,singleButton){
if(typeof(content)=="string"){
if(callback){
if(singleButton){
// alert("内容加function两个按钮");
showDouble(content,callback);
}else{
// alert("内容加function一个按钮");
showSingle(content,callback);
}
return;
}
showSingle(content);
}
},
//弹窗提示自定义弹框
eduToast:function(msg, duration){
duration = isNaN(duration) ? 3000 : duration;
var m = document.createElement('div');
m.innerHTML = msg;
m.style.cssText = "width: 60%;min-width: 150px;opacity: 0.7;height: 30px;color: rgb(255, 255, 255);line-height: 30px;text-align: center;border-radius: 5px;position: fixed;top: 40%;left: 20%;z-index: 999999;background: rgb(0, 0, 0);font-size: 12px;";
document.body.appendChild(m);
setTimeout(function () {
var d = 0.5;
m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
m.style.opacity = '0';
setTimeout(function () {
document.body.removeChild(m)
}, d * 1000);
}, duration);
},
};
var containHtml;
/**
<!--自定义内容,两个个按钮事件-->
*/
function showDouble(content,callback){
if(containHtml)return;
containHtml = '<div id="cover"><div id="tipView"> <div id="tv_title"></div> <div id="tv_content"></div><div> <button id="tv_cancleBtn">取消</button><button id="tv_sureBtn">确定</button></div> </div></div>';
var cover = document.createElement('div');
document.body.appendChild(cover);
cover.outerHTML = containHtml;
document.getElementById("cover").style.cssText = 'background: rgba(0,0,0,0.5);position: fixed; top: 0; left: 0; width: 100%;height: 100%;';
document.getElementById("tipView").style.cssText = 'position:fixed;padding-bottom: 15px;left:30px;right:30px;border-radius:8px; box-shadow:0 0 10px 5px rgba(0, 0, 0, .1), 0 0 10px 5px rgba(0, 0, 0, .1), 0 0 10px 5px rgba(0, 0, 0, .1);bottom:50%;margin-bottom:-30px;text-align:center;z-index: 1000';
document.getElementById("tv_title").style.cssText = 'color:#fff;border-top-left-radius:8px;border-top-right-radius:8px;height: 2.5em;line-height:2.6em;text-align: center;font-size: 16px';
document.getElementById("tv_content").style.cssText = 'font-size:15px; display:-webkit-box;display:-ms-flexbox;display:-webkit-flex;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;-webkit-align-items:center;margin:25px 15px 25px 15px;line-height:27px';
document.getElementById("tv_cancleBtn").style.cssText = 'color:#fff;width:80px;line-height:35px;font-size:14px;border-radius:6px;margin-right:30px;border:0';
document.getElementById("tv_sureBtn").style.cssText = 'color:#fff;width:100px;line-height:35px;font-size:14px;border-radius:6px;border:0';
showTips("提示",content,callback); document.getElementById('cover').addEventListener('click',removeFromSuperDiv);
document.getElementById('tipView').addEventListener('click',function (event) {
event.stopPropagation();
},false);
}
/**
<!--只显示提示内容-->
*/
function showSingle(content,callback){
if(containHtml)return;
containHtml = '<div id="cover"><div id="tipView"> <div id="tv_title"></div> <div id="tv_content"></div> <div id="tv_sureBtn">确定</div> </div></div>';
var cover = document.createElement('div');
document.body.appendChild(cover);
cover.outerHTML = containHtml;
document.getElementById("cover").style.cssText = 'background: rgba(0,0,0,0.5);position: fixed; top: 0; left: 0; width: 100%;height: 100%;';
document.getElementById("tipView").style.cssText = 'position:fixed;padding-bottom: 15px;left:30px;right:30px;border-radius:8px; box-shadow:0 0 10px 5px rgba(0, 0, 0, .1), 0 0 10px 5px rgba(0, 0, 0, .1), 0 0 10px 5px rgba(0, 0, 0, .1);bottom:50%;margin-bottom:-30px;text-align:center;z-index: 1000';
document.getElementById("tv_title").style.cssText = 'color:#fff;border-top-left-radius:8px;border-top-right-radius:8px;height: 2.5em;line-height:2.6em;text-align: center;font-size: 16px';
document.getElementById("tv_content").style.cssText = 'font-size:15px; display:-webkit-box;display:-ms-flexbox;display:-webkit-flex;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;-webkit-justify-content:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;-webkit-align-items:center;margin:25px 15px 25px 15px;line-height:27px';
document.getElementById("tv_sureBtn").style.cssText = 'color:#fff;width:100px;line-height:35px;font-size:14px;border-radius:6px;margin:0 auto;border:0';
showTips("提示",content,callback?callback:null);
document.getElementById('cover').addEventListener('click',removeFromSuperDiv);
document.getElementById('tipView').addEventListener('click',function (event) {
event.stopPropagation();
},false);
}
/**
<!--显示提示-->
*/
function showTips(title,content,callback) {
if(!content||content=="")return;
document.getElementById("tv_title").innerHTML=title;
document.getElementById("tv_content").innerHTML=content;
document.getElementById('tv_sureBtn').addEventListener('click',function () {
if(callback){callback();}
removeFromSuperDiv();
},false);
document.getElementById('tv_cancleBtn').addEventListener('click',function () {
removeFromSuperDiv();
},false);
}
/**
<!--移除弹框-->
*/
function removeFromSuperDiv(){
var cover = document.getElementById('cover');
if (cover != null){
cover.parentNode.removeChild(cover);
}
containHtml=null;
}
/**
*
<!--调用方法-->
<!--两个按钮-->
$().showAlert("我很好的的哈哈",function(){
alert("回来了");
},true);
<!--一个按钮-->
$().showAlert("我很好的的哈哈哈",function(){
alert("回来了");
},false);
*
*/

将其在vue目录中用一个js文件存起来,哪个页面需要弹窗时引入

import eduAlert from '@/js/alert.js'
 
在方法中就可以使用了
 
alertmethos(){
// eduAlert.showAlert("我很好的的哈哈",null,true);
// eduAlert.showAlert("我很好的的哈哈",function(){
// alert(1111);
// },true);
// eduAlert.showAlert("我很好的的哈哈",function(){
// alert(1111);
// },false);
// alert(1111)
eduAlert.eduToast("自定义弹窗时长弹窗",2000)
}

vue中封装一个全局的弹窗js的更多相关文章

  1. vue中封装一个倒计时

    <template> <div class="countDownBox"> <div class="row resetStyle" ...

  2. 用vue2.x注册一个全局的弹窗alert组件

    一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...

  3. 用vue2.x注册一个全局的弹窗alert组件、confirm组件

    一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...

  4. Vue 中如何定义全局的变量和常量

    Vue 中如何定义全局的变量和常量 我想要定义一个变量, 在项目的任何地方都可以访问到, 不需要每一次使用的时候, 都引入. 尝试1:创建 global.js 并且在其中定义   let a = 10 ...

  5. vue 中如何对公共css、 js 方法进行单文件统一管理,全局调用

    1.前言 最近,为公司开发交付的一个后台管理系统项目,我使用了 Vue 框架进行开发实践. 模块化.组件化.工程化的开发体验非常好.良好的 api,优雅的设计,对于工程师非常友好. 但是由于模块比较多 ...

  6. js中封装一个自己的简单数学对象

    封装一个数学对象求最大值最小值 <script> var myMath={ PI:3.1415926, max:function(){ var max=arguments[0];//注意a ...

  7. vue中如何引入全局样式或方法

    vue中我么会经常用到通用的一些全局的方法,如何左才能实现全局的复用减少代码累赘呢? 我们一般将公用的方法分装再utils.js文件中,然后再main.js主入口文件中将utils.js中的公共的方法 ...

  8. IOS中封装一个View的思路

    一.封装一个View的思路 1.将View内部的业务逻辑(显示内容)封装到View中 2.一般情况下,View的位置应该由父控件来决定,也就是位置不应该固定死在View内部 3.至于View的宽高,根 ...

  9. 项目开发中封装一个BarButtonItem类别-很实用

    Encapsulates a TabBarItem--封装一个BarButtonItem类 在我们程序的导航栏的左边或右边一般都会有这样的BarButtonItem,用来界面之间的跳转 如果我们有很多 ...

随机推荐

  1. 【iCore1S 双核心板_ARM】例程十:SYSTICK定时器实验——定时点亮LED

    实验原理: 通过STM32的三个GPIO口驱动三色LED的三个通道,设定GPIO为推挽输出,采用 灌电流的方式与LED连接,输出高电平LED灭,输出低电平LED亮,通过系统定时器实现 1s定时,每秒变 ...

  2. 【转】WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 菜单Menu的自定义样式: 右键菜单ContextMenu的自定义样式 ...

  3. 和我一起学Effective Java之创建和销毁对象

    前言 主要学习创建和销毁对象: 1.何时以及如何创建对象 2.何时以及如何避免创建对象 3.如何确保它们能够适时地销毁 4.如何管理对象销毁之前必须进行的清理动作 正文 一.用静态工厂方法代替构造器 ...

  4. "佛祖保佑 永无bug" 注释模板设置详解(仅供娱乐)

    1.注释模板效果图 今天在网上看到一段有趣的注释,佛祖保佑 永无bug, 效果如下图所示:  代码如下所示: /** * _ooOoo_ * o8888888o * 88" . " ...

  5. 【问题集】redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range

    redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range incrm ...

  6. 跨服务器做yum源

    服务器无法上网,然后自己根据光盘搭建的YUM源不够用.RPM安装软件,各种依赖,找包烦死. 先做个 能上外网的 http proxy 找一个可以上Internet的服务器,然后起一个squid服务, ...

  7. sencha touch 在新版谷歌浏览器中painted事件无法触发解决方案以及carousel 控件、togglefield控件、滚动条失效

    在2.3/2.4版本中,新版谷歌浏览器(43.44版本)里面painted事件是不会触发的,以及carousel 控件.togglefield控件.滚动条失效,官方的解决方案如下,测试可用 会出现这个 ...

  8. PHP针对中英文混合字符串长度判断及截取方法

    PHP自带的函数如strlen().mb_strlen()都是通过计算字符串所占字节数来统计字符串长度的,一个英文字符占1字节.例: $enStr = 'Hello,China!'; echo str ...

  9. 跟bWAPP学WEB安全(PHP代码)--认证绕过与会话管理

    背景 这里主要是代码逻辑问题,而不是代码使用函数的问题,因此在这个里面就不粘贴具体代码了,而是分类介绍下bWAPP中涉及的安全问题: 验证码问题 找回问题 账号口令问题 Cookies问题 Sessi ...

  10. 轮滑基础(一)(前摔,葫芦步,推步,A字转弯,弓步转弯)

    轮滑新手入门推荐? [柚子陪你学轮滑轮滑教学]第一集 轮滑安全 1,站: 站立:脚可以成v字,或者平行,手放膝盖或者前伸.平行站立 膝盖相距一拳头左右,两腿间距略小于肩宽.膝盖略弯,腰下压,抬头挺胸 ...