js+css實現模態層效果,jscss實現模態
在做web前端的時候,有些時候會涉及到模態層,在此提供一種實現思路,希望對大家有用。先貼效果吧:
模態層效果
下面說說在寫模態層的時候的思路:通過可配置的參數width,height,title以及content用來設定彈出的資訊框顯示的內容,並通過可配置參數container用來設定模態層顯示的地區。思路很簡單,主要是一些css樣式和js處理,詳見源碼:
modal.css
html,body{ font-size: 12px; font-family: "微軟雅黑";}.modal{ position: absolute; top:0px; left: 0px; border: 1px solid #000; background: #555; opacity: 0.4;}.infowin{ border: 1px solid #777777; background: #fff; box-shadow: 0 0 0.75em #777777; -moz-box-shadow: 0 0 0.75em #777777; -webkit-box-shadow: 0 0 0.75em #777777; -o-box-shadow: 0 0 0.75em #777777; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; -o-border-radius: 5px;} .title{ border-bottom: 1px solid #777777;}.title_content{ padding: 5px; padding-left: 10px; font-size: 14px; font-family: "微軟雅黑"; font-weight: bold;}.close{ background: url(close.png) no-repeat; width: 25px; height: 25px; float: right;}.close:hover{ cursor: pointer;}.content{ padding-left: 10px; padding-top: 10px;}
jquery.modal.js
(function($){ $.fn.modalInfowindow = function(options){ var defaults = {}; var options = $.extend(defaults, options); var container=$(this); var width=options.width, height=options.height, title=options.title, content=options.content; //模態層容器 var modal=$("<div id='modal'></div>"); modal.css("width","100%"); modal.css("height","100%"); //模態層 var modal_div=$("<div class='modal'></div>"); modal_div.css("width","100%"); modal_div.css("height","100%"); //資訊框 var infoWin=$("<div class='infowin'></div>"); infoWin.css("width",width+"px"); infoWin.css("height",height+"px"); infoWin.css("position","absolute"); infoWin.css("top",(container.height()-height)/2+"px"); infoWin.css("left",(container.width()-width)/2+"px"); //標題 var infoWin_title=$("<div class='title'></div>"); var infoWin_title_close=$("<div class='close'></div>") infoWin_title_close.on("click",function(){ console.log("Close Modal!"); modal.hide(); }); var infoWin_title_content=$("<div class='title_content'></div>") infoWin_title_content.append(title); infoWin_title.append(infoWin_title_close); infoWin_title.append(infoWin_title_content); //內容 var infoWin_content=$("<div class='content'></div>"); infoWin_content.append(content); //資訊框添加標題和內容 infoWin.append(infoWin_title); infoWin.append(infoWin_content); //模態層容器添加模態層和資訊框 modal.append(modal_div); modal.append(infoWin); //將模態層添加到容器 container.append(modal); }})(jQuery);將之封裝成一個jquery外掛程式,提高可重用性,在頁面短的調用方式如下:
<div class="container" id="container"> <a class="button" onclick="ShowModal()">快顯視窗</a></div>
頁面端涉及到的樣式:
<style type="text/css"> .container{ width: 600px; height: 300px; position: relative; border: 1px solid #777777; } .button{ position: absolute; left: 15%; top: 15%; background: #eee; padding: 5px 10px ; } .button:hover{ background: #aaa; cursor: pointer; } </style>
調用modal外掛程式:
<script type="text/javascript" src="jquery-1.8.3.js"></script> <script type="text/javascript" src="jquery.modal.js"></script> <script type="text/javascript"> function ShowModal(){ $('#container').modalInfowindow({ width:300, height:150, title:"中國", content:"中國是中華人名共和國的簡稱" }); } </script>
其中,content可為html代碼。
源碼下載
HTML中怎用DIV CSS javascript類比實現模態視窗的彈出?
鎖屏效果的原理:共3層,最裡面一層是網頁的內容,其次一層是透明層,最外面一層是快顯視窗,而控制這三層顯示先後順序的是z-index值,z-index值越大越靠近外面,若理解不了,你可以這樣想:在數學裡,我們有學過直角座標系吧,裡面分為x軸、y軸、z軸,其中z-index你可以看成是z軸,z軸的值是不是,越往外,值越大。只要將這三層的z-index值設為不同,在最外面的彈出框那層z-index值設為最大,然後將中間那層的透明度改小一點,就好了。
HTML中怎用DIV+CSS+javascript類比實現模態視窗的彈出?
<style type="text/css">
<!--
#ap {
position:absolute;
z-index:1; left: 400px;
top: 140px;
background-color: #DAF5FC;
border: 1px solid #00CCFF;
display:none;
}
#qp{
position:absolute;
z-index:1;
filter:alpha(opacity=60);
width:100%;
height:100%;
background-color: #eeeeee;
display:none;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<div id="qp"></div>
<div id="ap">
<div align="right" style="background-color:#0099CC">
<a href="javascript:" onclick="closediv();">關閉</a>
</div>
</div>
<p>
<input type="button" onclick="acdiv();" value="確定" />
<p>
<p>
<script language="javascript">
i=1;
function acdiv()
{
qp.style.display='block';
ap.style.display='block';
if(i<15){
i++;
ap.style.width=2*i*i;
ap.style.height=1.1*i*i;
ap.style.left=20*i;
ap.style.top=10*i;
var timer = setTimeout("acdiv()",1);
}
}
function closediv()
{
ap.style.display='none';
qp.style.display='none';
i=0;
} </script>
這是我自已寫的...餘下全文>>