javascript-弹窗组件

本文介绍了一款基于RequireJS的简易弹窗插件开发过程,包括样式与功能实现细节,如toast提示、alert警告及confirm确认对话框等。

在平时开发的过程中,总会用到一些交互类型的提示,以前一直用的别人开发好的,比如 layer,一个功能非常强大的弹窗组件。在最近的基于require的项目中,总感觉用layer插件和AMD模块化相背离,所以自己就写了一个功能简单的弹窗提示插件,当然功能没有layer强大,只能所满足项目需求,废话不多说,直接P代码

style.css

 1 .pop-layer{
 2     position:fixed;
 3     height: auto;
 4     overflow: hidden;
 5     left:0;
 6     right: 0;
 7     top:0;
 8     bottom: 0;
 9     z-index: 999;
10     display: none;
11 }
12 .pop-layer .toast{
13     background:rgba(0, 0, 0, 0.75);
14     width: 60%;
15     margin:40% auto 0;
16     padding: 8px 15px;
17     border-radius: 4px;
18     text-align: center;
19     color: #fff;
20 }
21 .pop-layer .alert{
22     position: absolute;
23     left: 50%;
24     margin-left:-30%;
25     margin-top: 50%;
26     background:#fff;
27     width: 60%;
28     border-radius: 2px;
29 }
30 .pop-layer .alert-content{
31     height: auto;
32     overflow: hidden;
33     text-align: center;
34     padding: 20px 10px;
35 }
36 .pop-layer .alert-footer{
37     display: -webkit-flex;     
38     display: flex;
39     -webkit-align-items: center;
40     align-items: center;
41     /* for uc */
42     display: -webkit-box;  
43     -webkit-box-align: center;
44 }
45 .pop-layer .alert-footer div{
46     -webkit-flex: 1;          
47     flex: 1;         
48     /* for uc */
49     -webkit-box-flex: 1;      
50     -moz-box-flex: 1;         
51     -ms-flex: 1;
52     text-align:center;
53     padding: 10px 0;
54     border-top:1px solid #f0f0f0;
55 }
56 .pop-layer .alert-footer div.cancel-btn{
57     border-left:1px solid #f0f0f0;
58 }

popLayer.js 。用于项目是基于require开发的,所以该组件只是AMD的一个模块,依赖于jquery;

 1 define(["jquery"],function($){
 2     function PopLayer(){
 3         this.layer = $("<div class='pop-layer'></div>");
 4     };
 5 
 6     PopLayer.prototype.toast = function(config){
 7         this.layer.html("<div class='toast'>"+config.text+"</div>");
 8         $("body").append(this.layer);
 9         this.layer.fadeIn();
10         this.clear(config.time);
11     };
12 
13     PopLayer.prototype.alert = function(config){
14         config.model?this.layer.attr("style","background:rgba(0, 0, 0, 0.75)"):"";
15         this.layer.html("<div class='alert'><div class='alert-content'>"+config.text+"</div><div class='alert-footer'><div class='sure-btn'>确定</div></div></div>");
16         $("body").append(this.layer);
17         this.layer.fadeIn(100);
18         $(".sure-btn").bind("click",config.yes);
19     };
20 
21     PopLayer.prototype.confirm = function(config){
22         config.model?this.layer.attr("style","background:rgba(0, 0, 0, 0.75)"):"";
23         this.layer.html("<div class='alert'><div class='alert-content'>"+config.text+"</div><div class='alert-footer'><div class='sure-btn'>确定</div><div class='cancel-btn'>取消</div></div></div>");
24         $("body").append(this.layer);
25         this.layer.fadeIn(100);
26         $(".sure-btn").bind("click",config.yes);
27         $(".cancel-btn").bind("click",config.no);
28     };
29 
30     PopLayer.prototype.clear = function(time){
31         var time = time || 3000;
32         setTimeout(function(){
33             $(".pop-layer").fadeOut().remove();
34         },time);
35     };
36 
37     PopLayer.prototype.destroy = function(){
38         $(".pop-layer").fadeOut().remove()
39     };
40 
41     var popLayer = new PopLayer();
42     return popLayer;
43 });

模块的使用:

 1     使用方法
 2  
 3      popLayer.toast({
 4          text:"loading",
 5          time:3000
 6      });
 7  
 8  
 9      popLayer.alert({
10          text:"loading",
11          model:true,
12          yes:function(){
13              console.log(123);
14         }
15      });
16      popLayer.confirm({
17          text:"loading",
18          model:true,
19          yes:function(){
20              console.log(123);
21          },
22          no:function(){
23              popLayer.destroy();
24          }
25      });

功能及其的简单,后期会扩展

 

能力有限,大神勿喷

 

 

转载于:https://www.cnblogs.com/oygg/p/5681966.html

一个通用的Android端弹窗管理框架,内部维护弹窗优先级队列 具备弹窗管理扩展功能 整合Dialog,PoupoWindow,悬浮Widget,透明Webview,Toast,SnackBar,无需再为繁琐的业务弹窗逻辑所困扰如何添加依赖只需要两行代码轻松接入//add this to your repositories  maven { url 'https://www.jitpack.io' } //add this to your dependencies implementation 'com.github.MrCodeSniper:PopLayer:2.0.0'具体如何使用1.根据策略创建对应的弹窗view//Dialog形式 PopLayerView  mLayerView = new PopLayerView(this,R.layout.common_dialog_upgrade_app); //透明Webview形式 PopLayerView mLayerView = new PopLayerView(this,LayerConfig.redPocketScheme);2.开始装配弹窗配置Popi mUpgradePopi1 = new Popi.Builder()                 .setmPopId(4)//弹窗的唯一标识 当id发生改变 视为新的弹窗                 .setmPriority(2)//优先级这里不具体划分对应的范围 值越小优先级越高                 .setmCancelType(TRIGGER_CANCEL)//弹窗消失的类型分为 TRIGGER_CANCEL(触摸消失) COUNTDOWN_CANCEL (延时消失)                 .setMaxShowTimeLength(5)//最长显示时间(S)                 .setMaxShowCount(5)//最大显示次数                 .setmBeginDate(1548858028)//开始时间 2019-01-30 22:20:28                 .setmEndDate(1548944428)//结束时间 2019-01-31 22:20:28                 .setLayerView(mLayerView)//弹窗View                 .build();3.纳入弹窗管理//纳入弹窗管理 PopManager.getInstance().pushToQueue(mUpgradePopi); //开始显示弹窗 PopManager.getInstance().showNextPopi();效果预览未来的计划逐步统一 其他类型的弹窗 希望能提供给大家一个较为全面的应对业务需求的弹窗管理框架版本记录V1方案版本号LOG进度更新V1.0.0项目开源,完成弹窗管理与Dialog形式扩展Dialog策略扩展完成V1.0.1修复Dialog策略无法获取dialog实体bugDialog策略优化V1.0.2修复activity摧毁造成的弹窗异常 bugDialog策略优化V1.0.3优化了弹窗的使用更加方便快捷框架使用优化V2方案版本号LOG进度更新V2.0.0正式加入透明Webview弹窗策略扩展透明Webview策略扩展完成作者介绍Hello 我叫lalala,如果您喜欢这个项目 请给个star 能follow我那真是太好了!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值