html5加vue弹窗,vue 封装 全局弹窗 js-dom

/**

* Created by yx on 2017/12/21.*/exportdefault{/**

* 带按钮的弹框

* */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);

},

};varcontainHtml;/**

*/

functionshowDouble(content,callback){if(containHtml)return;

containHtml= '

取消确定
';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);

}/**

*/

functionshowSingle(content,callback){if(containHtml)return;

containHtml= '

确定
';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);

}/**

*/

functionshowTips(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);

}/**

*/

functionremoveFromSuperDiv(){var cover = document.getElementById('cover');if (cover != null){

cover.parentNode.removeChild(cover);

}

containHtml=null;

}/**

*

$().showAlert("我很好的的哈哈",function(){

alert("回来了");

},true);

$().showAlert("我很好的的哈哈哈",function(){

alert("回来了");

},false);

**/

Vue3封装全局弹窗组件的步骤如下: 1. 创建一个Vue实例,作为全局弹窗组件的容器。可以使用`createApp`方法创建Vue实例,并将其挂载到一个DOM元素上。 2. 在全局弹窗组件上定义必要的属性和方法。比如,可以定义一个`visible`属性控制弹窗显示与隐藏,一个`title`属性用于显示弹窗的标题,一个`content`属性用于显示弹窗的内容等。 3. 在全局弹窗组件内部实现弹窗的样式和交互逻辑。可以使用Vue的模板语法和样式定义实现弹窗的外观和样式效果,并通过Vue的响应式特性,实现弹窗的交互逻辑,比如点击关闭按钮时隐藏弹窗。 4. 添全局方法,在Vue实例的原型上添一个方法,用于在任意组件中调用弹窗组件。可以使用`app.config.globalProperties`来添全局方法,以便在任何地方都可以访问到该方法。 5. 在组件中使用全局弹窗组件。在需要显示弹窗的组件中,通过调用全局方法来调用弹窗组件。可以通过传递参数的方式,动态设置弹窗的内容和样式。 6. 在全局弹窗组件的内部实现弹窗的生命周期钩子函数,比如`mounted`函数用于在弹窗组件被挂载到DOM后执行相应的逻辑。 通过以上步骤,就可以封装一个可在任何组件中使用的全局弹窗组件。在使用过程中,只需要调用全局方法,传入相应的参数,即可显示自定义的弹窗内容和样式,提供更好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值