自己封装一个弹窗JS

在我们平时的开发中,一定有很多地方需要用到弹窗提示功能。而系统自带的弹窗奇丑无比,而且我们不能自主控制。因此我们在开发过程中,自己封装一个弹窗JS方便我们使用。

代码demo如下:

  1 // JavaScript Document
  2 /*var allWidth = $(".menuList")[0].offsetWidth;
  3 $(".menuList").height(allWidth*298/1400+"px");*/
  4 var aAlert=function(content,btn1Word,callback){
  5     
  6         //布局弹框格式
  7         $("body").append('<div class="alertZZ">'+
  8                 '<div class="aAlert">'+
  9                     '<p style="text-align:center;font-size: 18px;margin-top: 15px;color:#fd6234;margin-left: -20px;"><img src="/webapp/discussion/ChyAppH5/img/tip.png" style=" 23px;position: relative;top: 4px;margin-right: 4px;">提示</p>'+
 10                     /*'<img src="/model/images/guanbi.png" class="guanbi"/>'+*/
 11                    '<div class="showWord">'+
 12                    '</div>'+
 13                    /*'<div class="btnDiv"><button id="btn1">确定</button></div>'+*/
 14                    '<div class="btnDiv">确 定</div>'+
 15                 '</div>'+
 16             '</div>'
 17         );
 18 
 19         //弹框样式调整
 20         $("body").css({'margin':'0','padding':'0','overflow':'hidden'});
 21         $(".mainDiv").css('overflow','hidden');
 22         $("img").css({/*100%;'display':'block',*/'border':'0px'});
 23         $(".alertZZ").css({
 24                 'width':'100%','height':'100%','position':'fixed',
 25                 'background':'rgba(0,0,0,0.25)','top':'0','left':'0','overflow':'hidden',
 26                 'z-index':'1000001'
 27         })
 28         $(".alertZZ .aAlert").css({
 29                 'width':'260px','background':'#FFF','position':'absolute',
 30                 'margin':'auto','background':'#FFF','z-index':'1000002',
 31                 'border-radius':'5px'
 32         });
 33         $(".alertZZ .aAlert .guanbi").css({'float':'right','margin':'10px 10px auto auto','padding':'10px','cursor': 'pointer'})
 34         $(".alertZZ .aAlert .showWord").css({
 35                 'padding':'0 15px','color':'#7d7d7d','margin':'15px auto 30px auto',
 36                 'overflow':'hidden','line-height':'22px',
 37                 'font-size':'14px','text-align':'center'
 38         })
 39         $(".alertZZ .aAlert #btn1").css({
 40                 'width':'100px','background':'#263552',
 41                 'color':'#FFF','border':'0px','height':'36px',
 42                 'line-height':'36px','display':'block',
 43                 'font-size':'14px','letter-spacing':'2px',
 44                 'margin':'auto','font-family':'微软雅黑','outline': 'none'
 45         })
 46         $(".alertZZ .aAlert .btnDiv").css({
 47                 'background':'#fd6234','margin':'20px auto auto auto',
 48                 'text-align':'center','width':'100%','padding':'10px 0',
 49                 'color':'#fff','border-bottom-left-radius':'5px',
 50                 'border-bottom-right-radius':'5px'
 51         });
 52         
 53         //弹框居中显示
 54         //alert("12"+document.documentElement.clientHeight);
 55     
 56             /*
 57             var alertLeft=window.innerWidth/2-$(".aAlert").outerWidth()/2,
 58                 alertTop=window.innerHeight/3-$(".aAlert").outerHeight()/3;
 59             $(".alertZZ .aAlert").css({'left':alertLeft+'px','top':alertTop+'px'});
 60             */
 61             var alertLeft=window.innerWidth/2-$(".aAlert").outerWidth()/2,
 62                 alertTop=window.innerHeight/3-$(".aAlert").outerHeight()/3;
 63         $(".alertZZ .aAlert").css({'left':alertLeft+'px','top':alertTop+'px'});
 64         //添加弹框的内容
 65         $(".alertZZ .aAlert .showWord").html(content);
 66         if(btn1Word!=""){
 67             $(".alertZZ .aAlert #btn1").html(btn1Word);
 68         }
 69         
 70         //alert($(".showWord").html());
 71         var str=$(".showWord").html();
 72         //获取该div包含字符的个数
 73         var strLength = 0;
 74         for(var i = 0;i < str.length; i++){
 75           if(str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2
 76              strLength += 2;
 77           else  
 78              strLength++;
 79         }
 80         //alert(strLength);
 81         if(strLength>42){$(".showWord").css('text-align','left');}
 82         
 83         //点击关闭按钮和这灰色的遮罩层,关闭弹框
 84          $(".alertZZ .aAlert .btnDiv").click(function (){
 85              $("body").css('overflow-y','auto');
 86              $(".mainDiv").css('overflow-y','auto');
 87              $(".alertZZ").remove();
 88             //$(".alertZZ .aAlert").hide();
 89             if($(".alertZZ .aAlert").hide()&&$(".alertZZ").hide()){
 90                 //callback();
 91             }
 92          });
 93     $(".alertZZ .guanbi").click(function (){
 94         $("body").css('overflow-y','auto');
 95          $(".mainDiv").css('overflow-y','auto');
 96         $(".alertZZ").remove();
 97         //$(".alertZZ .aAlert").hide();
 98     });
 99         
100 }

如果在开发的过程中,我们需要点击弹窗上的按钮后,程序再按照我们的需求走下去。我们还可以在相应的页面写JS控制,如点击确认后再跳转页面。

附上代码:

 1 $.ajax({
 2         url:url+"/******.do?method=******",
 3         type:"post",
 4         data:datas,
 5         dataType:"json",
 6         cache:false,
 7         ifModified:true,
 8         success:function(data){
 9             aAlert("提交成功!");
10             $(".btnDiv").click(function(){
11                 location.href = "www.baidu.com";
12             })
13         }
14 });

注意:其中 btnDiv 是弹窗中确定按钮的类名。

处不住的,只能遗弃;盼不来的,只能放弃;留不住的,只能终止。人生,就是这样,选择不易,放弃艰难。人生之苦,苦在选择,人生之难,难在放弃。人生,说到底,就是选择与放弃。

原文地址:https://www.cnblogs.com/jichuang/p/8583580.html