自己封裝一個彈窗JS,自己封裝彈窗JS

來源:互聯網
上載者:User

自己封裝一個彈窗JS,自己封裝彈窗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>'+ 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({/*width: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 是彈窗中確定按鈕的類名。

處不住的,只能遺棄;盼不來的,只能放棄;留不住的,只能終止。人生,就是這樣,選擇不易,放棄艱難。人生之苦,苦在選擇,人生之難,難在放棄。人生,說到底,就是選擇與放棄。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.