js用類封裝pop彈窗組件
下面的彈出框組件使用的是類來封裝的。一個(gè)彈窗組件通過new一個(gè)實(shí)例來生成。
下面直接上代碼:
html結(jié)構(gòu):
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> /*基本的樣式*/ button{width: 1.6rem;height: 0.5rem;font-size: 0.3rem;line-height: 0.5rem;background: red;color: white;font-weight: bold} .hide{display: none;} .js-pop{ width: 100%; height: 100%; z-index: 100; position: absolute; top:0; left: 0; font-size: 0.24rem; } .js-pop .mask{ position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: #000; opacity: 0.2; } .js-pop .content{ position: fixed; top: 50%; left: 50%; width: 5.80rem; height: 4.81rem; margin: -2.405rem 0 0 -2.9rem; background: url("pop-bg.png") no-repeat; background-size: contain; color: #262626; text-align: center; } .js-pop .content .close{ position: absolute; top: .25rem; right: .08rem; width: .54rem; height: .48rem; z-index: 4; cursor:pointer; } .js-pop .content .prize-title { height: .39rem; min-width: 1.77rem; margin: .28rem auto; line-height: 0.39rem; color: white; text-align: left; text-indent: 1rem; } .js-pop .content .prize-content { color: #a40000; font-size: .24rem; margin:0 0.1rem 0 0.49rem; line-height: 0.4rem; width: 5.2rem; } </style> </head> <body> <button id="bb">顯示彈窗</button> <div class="js-pop js-prize-pop hide" id="popLogin"> <div class="mask"></div> <div class="content"> <div class="close"></div> <div class="prize-title">溫馨提示</div> <div class="prize-content" style="margin-top: 1rem"> 登錄后才能參與活動(dòng)哦! <br/> 自動(dòng)登錄跳轉(zhuǎn)中...... </div> </div> </div> <!--引入jquery--> <script type="text/javascript" src="http://image.37wan.cn/common/js/jquery-1.9.1.min.js"></script> <!--引入Pop組件--> <script src="pop.js"></script> <script> // rem代碼 var windowW = $(window).width(); var ratio = windowW/640; $("html").css("fontSize",100*ratio+"px"); $(window).on("resize",function(){ var windowW = $(window).width(); var ratio = windowW/640; $("html").css("fontSize",100*ratio+"px"); }); //new一個(gè)Pop實(shí)例 var popLogin=new Pop($("#popLogin")); $("#bb").on("click",function(){ popLogin.open(); }); </script> </body> </html>
pop.js代碼:
//Pop的構(gòu)造函數(shù) var Pop=function(wrap,option){ var $this=this; var opt={ closeCall:null }; $.extend(opt,option); var mask=wrap.find(".mask"); //特權(quán)方法:1、open();2、close();3、setPrize();4、setContent()控制彈窗內(nèi)顯示的內(nèi)容 this.open=function(){ wrap.show(); mask.show(); }; this.close=function(callbalck){ wrap.hide(); mask.hide(); opt.closeCall&&callbalck(); }; this.setPrize=function(text){ wrap.find(".js-prize").text(text); }; this.setContent = function (text) { wrap.find(".js-content").text(text); }; function events(){ wrap.on("click",".close",function(e){ e.stopPropagation(); $this.close(opt.closeCall); }); } events(); };
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Js頁(yè)面通過POST傳遞參數(shù)跳轉(zhuǎn)到新頁(yè)面詳解
這篇文章主要給大家介紹了關(guān)于在Js頁(yè)面通過POST傳遞參數(shù)跳轉(zhuǎn)到新頁(yè)面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08JavaScript內(nèi)存管理與閉包實(shí)例詳解
不管什么樣的編程語(yǔ)言,在代碼的執(zhí)行過程中都是需要給它分配內(nèi)存的,下面這篇文章主要給大家介紹了關(guān)于JavaScript內(nèi)存管理與閉包的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06JavaScript簡(jiǎn)單實(shí)現(xiàn)鼠標(biāo)拖動(dòng)選擇功能
本篇文章主要是對(duì)JavaScript簡(jiǎn)單實(shí)現(xiàn)鼠標(biāo)拖動(dòng)選擇功能的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03javascript實(shí)現(xiàn)焦點(diǎn)滾動(dòng)圖效果 具體方法
以下JS代碼實(shí)現(xiàn)了焦點(diǎn)滾動(dòng)圖的效果方法,有需要的朋友可以參考一下2013-06-06Bootstrap滾動(dòng)監(jiān)聽(Scrollspy)插件詳解
滾動(dòng)監(jiān)聽插件是用來根據(jù)滾動(dòng)條所處在的位置自動(dòng)更新導(dǎo)航項(xiàng)目, 顯示導(dǎo)航項(xiàng)目高亮顯示。這篇文章主要介紹了Bootstrap滾動(dòng)監(jiān)聽(Scrollspy)插件的相關(guān)資料,需要的朋友可以參考下2016-04-04