jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框(仿天貓的刪除對(duì)話框)
更新時(shí)間:2014年04月10日 15:48:33 作者:
單擊刪除按鈕或者登陸按鈕后,彈出對(duì)話框問(wèn)你是否刪除或者彈出一個(gè)登陸對(duì)話框,本文使用jquery來(lái)實(shí)現(xiàn)這種效果,需要的朋友可以參考下
我們?cè)谔熵堖M(jìn)行購(gòu)物的時(shí)候,經(jīng)常會(huì)碰到單擊刪除按鈕或者登陸按鈕后,彈出對(duì)話框問(wèn)你是否刪除或者彈出一個(gè)登陸對(duì)話框,并且我們也是可以看到我們之前頁(yè)面的信息,就是點(diǎn)擊不了,只有對(duì)對(duì)話框進(jìn)行操作后才有相應(yīng)的變化。截圖如下(以天貓為例)
如圖所示,上面就是天貓的效果圖,其實(shí)這就是通過(guò)jQuery實(shí)現(xiàn)的,并且實(shí)現(xiàn)的過(guò)程也不是很不復(fù)雜,那么現(xiàn)在就讓我們來(lái)看看實(shí)現(xiàn)的過(guò)程吧。
首先是頁(yè)面的布局部分:delete.html
<!DOCTYPE html>
<html>
<head>
<title>遮罩彈出窗口</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../css/delete.css">
<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="../js/delete.js"></script>
</head>
<body>
<div class="divShow">
<input type="checkbox" id="chexkBox1"> <a href="#">這是一條可以刪除的記錄</a>
<input id="button1" type="button" value="刪除" class="btn">
</div>
<div class="mask"></div>
<div class="dialog">
<div class="title">
<img alt="點(diǎn)擊可以關(guān)閉" src="../images/delete.gif" width="30px" height="30px;">
刪除時(shí)提示
</div>
<div class="content">
<img alt="" src="../images/delete.gif" width="60px" height="60px">
<span>你真的要?jiǎng)h除這條記錄嗎?</span>
</div>
<div class="bottom">
<input type="button" id="ok" value="確定" class="btn">
<input type="button" id="noOk" value="取消" class="btn">
</div>
</div>
</body>
</html>
需要做出說(shuō)明的是,我只添加了一條記錄,其實(shí)可以模擬多條記錄的刪除。這里我們有三層div結(jié)構(gòu),其中mask和dialog使我們通過(guò)jquery進(jìn)行觸發(fā)的,接下來(lái)我們講下css的布局,先上代碼:delete.html
@CHARSET "UTF-8";
*{
margin: 0px;
padding: 0px;
}
.divShow{
line-height: 32px;
height: 32px;
background-color: #eee;
width: 280px;
padding-left: 10px;
}
.dialog{
width: 360px;
border: 1px #666 solid;
position: absolute;
display: none;
z-index: 101;//保證該層在最上面顯示
}
.dialog .title{
background:#fbaf15;
padding: 10px;
color: #fff;
font-weight: bold;
}
.dialog .title img{
float:right;
}
.dialog .content{
background: #fff;
padding: 25px;
height: 60px;
}
.dialog .content img{
float: left;
}
.dialog .content span{
float: left;
padding: 10px;
}
.dialog .bottom{
text-align: right;
padding: 10 10 10 0;
background: #eee;
}
.mask{
width: 100%;
height: 100%;
background: #000;
position: absolute;
top: 0px;
left: 0px;
display: none;
z-index: 100;
}
.btn{
border: #666 1px solid;
width: 65px;
}
在CSS文件中,我需要著重說(shuō)明的是z-index的使用,z-index表示的層的堆疊順序,如果數(shù)值越高,表示越在上層顯示,mask的z-index是100,dialog的z-index是101,數(shù)值足夠大的原因就是保證絕對(duì)在頂層顯示,通過(guò)數(shù)值的調(diào)增可以控制div層的顯示。
接下來(lái)就是最為主要的js代碼,當(dāng)然在使用jquery時(shí),我們要導(dǎo)入jquery包:<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
delete.js
$(function(){
//綁定刪除按鈕的觸發(fā)事件
$("#button1").click(function(){
$(".mask").css("opacity","0.3").show();
showDialog();
$(".dialog").show();
});
/*
* 根據(jù)當(dāng)前頁(yè)面于滾動(dòng)條的位置,設(shè)置提示對(duì)話框的TOP和left
*/
function showDialog(){
var objw=$(window);//當(dāng)前窗口
var objc=$(".dialog");//當(dāng)前對(duì)話框
var brsw=objw.width();
var brsh=objw.height();
var sclL=objw.scrollLeft();
var sclT=objw.scrollTop();
var curw=objc.width();
var curh=objc.height();
//計(jì)算對(duì)話框居中時(shí)的左邊距
var left=sclL+(brsw -curw)/2;
var top=sclT+(brsh-curh)/2;
//設(shè)置對(duì)話框居中
objc.css({"left":left,"top":top});
}
//當(dāng)頁(yè)面窗口大小改變時(shí)觸發(fā)的事件
$(window).resize(function(){
if(!$(".dialog").is(":visible")){
return;
}
showDialog();
});
//注冊(cè)關(guān)閉圖片單擊事件
$(".title img").click(function(){
$(".dialog").hide();
$(".mask").hide();
});
//取消按鈕事件
$("#noOk").click(function(){
$(".dialog").hide();
$(".mask").hide();
});
//確定按鈕事假
$("#ok").click(function(){
$(".dialog").hide();
$(".mask").hide();
if($("input:checked").length !=0){
//注意過(guò)濾器選擇器中間不能存在空格$("input :checked")這樣是錯(cuò)誤的
$(".divShow").remove();//刪除某條數(shù)據(jù)
}
});
});<span style="white-space:pre">
需要說(shuō)明的是主要代買(mǎi)就是showDialog()的用于動(dòng)態(tài)的確定對(duì)話框的顯示位置。
如圖所示,上面就是天貓的效果圖,其實(shí)這就是通過(guò)jQuery實(shí)現(xiàn)的,并且實(shí)現(xiàn)的過(guò)程也不是很不復(fù)雜,那么現(xiàn)在就讓我們來(lái)看看實(shí)現(xiàn)的過(guò)程吧。
首先是頁(yè)面的布局部分:delete.html
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<title>遮罩彈出窗口</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../css/delete.css">
<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="../js/delete.js"></script>
</head>
<body>
<div class="divShow">
<input type="checkbox" id="chexkBox1"> <a href="#">這是一條可以刪除的記錄</a>
<input id="button1" type="button" value="刪除" class="btn">
</div>
<div class="mask"></div>
<div class="dialog">
<div class="title">
<img alt="點(diǎn)擊可以關(guān)閉" src="../images/delete.gif" width="30px" height="30px;">
刪除時(shí)提示
</div>
<div class="content">
<img alt="" src="../images/delete.gif" width="60px" height="60px">
<span>你真的要?jiǎng)h除這條記錄嗎?</span>
</div>
<div class="bottom">
<input type="button" id="ok" value="確定" class="btn">
<input type="button" id="noOk" value="取消" class="btn">
</div>
</div>
</body>
</html>
需要做出說(shuō)明的是,我只添加了一條記錄,其實(shí)可以模擬多條記錄的刪除。這里我們有三層div結(jié)構(gòu),其中mask和dialog使我們通過(guò)jquery進(jìn)行觸發(fā)的,接下來(lái)我們講下css的布局,先上代碼:delete.html
復(fù)制代碼 代碼如下:
@CHARSET "UTF-8";
*{
margin: 0px;
padding: 0px;
}
.divShow{
line-height: 32px;
height: 32px;
background-color: #eee;
width: 280px;
padding-left: 10px;
}
.dialog{
width: 360px;
border: 1px #666 solid;
position: absolute;
display: none;
z-index: 101;//保證該層在最上面顯示
}
.dialog .title{
background:#fbaf15;
padding: 10px;
color: #fff;
font-weight: bold;
}
.dialog .title img{
float:right;
}
.dialog .content{
background: #fff;
padding: 25px;
height: 60px;
}
.dialog .content img{
float: left;
}
.dialog .content span{
float: left;
padding: 10px;
}
.dialog .bottom{
text-align: right;
padding: 10 10 10 0;
background: #eee;
}
.mask{
width: 100%;
height: 100%;
background: #000;
position: absolute;
top: 0px;
left: 0px;
display: none;
z-index: 100;
}
.btn{
border: #666 1px solid;
width: 65px;
}
在CSS文件中,我需要著重說(shuō)明的是z-index的使用,z-index表示的層的堆疊順序,如果數(shù)值越高,表示越在上層顯示,mask的z-index是100,dialog的z-index是101,數(shù)值足夠大的原因就是保證絕對(duì)在頂層顯示,通過(guò)數(shù)值的調(diào)增可以控制div層的顯示。
接下來(lái)就是最為主要的js代碼,當(dāng)然在使用jquery時(shí),我們要導(dǎo)入jquery包:<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
delete.js
復(fù)制代碼 代碼如下:
$(function(){
//綁定刪除按鈕的觸發(fā)事件
$("#button1").click(function(){
$(".mask").css("opacity","0.3").show();
showDialog();
$(".dialog").show();
});
/*
* 根據(jù)當(dāng)前頁(yè)面于滾動(dòng)條的位置,設(shè)置提示對(duì)話框的TOP和left
*/
function showDialog(){
var objw=$(window);//當(dāng)前窗口
var objc=$(".dialog");//當(dāng)前對(duì)話框
var brsw=objw.width();
var brsh=objw.height();
var sclL=objw.scrollLeft();
var sclT=objw.scrollTop();
var curw=objc.width();
var curh=objc.height();
//計(jì)算對(duì)話框居中時(shí)的左邊距
var left=sclL+(brsw -curw)/2;
var top=sclT+(brsh-curh)/2;
//設(shè)置對(duì)話框居中
objc.css({"left":left,"top":top});
}
//當(dāng)頁(yè)面窗口大小改變時(shí)觸發(fā)的事件
$(window).resize(function(){
if(!$(".dialog").is(":visible")){
return;
}
showDialog();
});
//注冊(cè)關(guān)閉圖片單擊事件
$(".title img").click(function(){
$(".dialog").hide();
$(".mask").hide();
});
//取消按鈕事件
$("#noOk").click(function(){
$(".dialog").hide();
$(".mask").hide();
});
//確定按鈕事假
$("#ok").click(function(){
$(".dialog").hide();
$(".mask").hide();
if($("input:checked").length !=0){
//注意過(guò)濾器選擇器中間不能存在空格$("input :checked")這樣是錯(cuò)誤的
$(".divShow").remove();//刪除某條數(shù)據(jù)
}
});
});<span style="white-space:pre">
需要說(shuō)明的是主要代買(mǎi)就是showDialog()的用于動(dòng)態(tài)的確定對(duì)話框的顯示位置。
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)首頁(yè)懸浮框
- 使用jQuery UI的tooltip函數(shù)修飾title屬性的氣泡懸浮框
- jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框效果(2)
- jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框效果(1)
- jQuery點(diǎn)擊按鈕彈出遮罩層且內(nèi)容居中特效
- jQuery實(shí)現(xiàn)點(diǎn)擊按鈕彈出可關(guān)閉層的浮動(dòng)層插件
- 基于Jquery的仿Windows Aero彈出窗(漂亮的關(guān)閉按鈕)
- jquery實(shí)現(xiàn)界面點(diǎn)擊按鈕彈出懸浮框
相關(guān)文章
jQuery插件HighCharts繪制2D餅圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制2D餅圖效果,結(jié)合完整實(shí)例形式分析了jQuery使用HighCharts插件繪制餅圖效果的操作步驟與相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
jQuery實(shí)現(xiàn)的簡(jiǎn)單懸浮層功能完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡(jiǎn)單懸浮層功能,結(jié)合完整實(shí)例形式分析了jQuery基于時(shí)間函數(shù)動(dòng)態(tài)修改css樣式實(shí)現(xiàn)窗口浮動(dòng)效果的相關(guān)技巧,需要的朋友可以參考下2017-01-01
jquery分頁(yè)插件jquery.pagination.js實(shí)現(xiàn)無(wú)刷新分頁(yè)
這篇文章主要介紹了jquery分頁(yè)插件jquery.pagination.js實(shí)現(xiàn)無(wú)刷新分頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-04-04
jQuery開(kāi)源組件BootstrapValidator使用詳解
這篇文章主要為大家詳細(xì)介紹了jQuery開(kāi)源組件BootstrapValidator的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
jQuery+json實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建復(fù)雜表格table的方法
這篇文章主要介紹了jQuery+json實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建復(fù)雜表格table的方法,涉及jQuery針對(duì)json數(shù)據(jù)的解析與表格動(dòng)態(tài)創(chuàng)建操作相關(guān)技巧,需要的朋友可以參考下2016-10-10
jQuery實(shí)現(xiàn)checkbox的簡(jiǎn)單操作
這篇文章主要介紹了jQuery實(shí)現(xiàn)checkbox的簡(jiǎn)單操作,對(duì)復(fù)選框組的全選、全不選、不全選,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

