這篇文章主要介紹jQuery如何實現彈出窗口,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
今天講了Jquery的彈出窗口的組成和用法:
先把引用文件的代碼寫好:
// 每個彈窗的標識
var x =0;
var idzt = new Array();
var Window = function(config){
//ID不重復
idzt[x] = "zhuti"+x; //彈窗ID
//初始化,接收參數
this.config = {
width : config.width || 300, //寬度
height : config.height || 200, //高度
buttons : config.buttons || '', //默認無按鈕
title : config.title || '標題', //標題
content : config.content || '內容', //內容
isMask : config.isMask == false?false:config.isMask || true, //是否遮罩
isDrag : config.isDrag == false?false:config.isDrag || true, //是否移動
};
//加載彈出窗口
var w = ($(window).width()-this.config.width)/2;
var h = ($(window).height()-this.config.height)/2;
var nr = "<div class='zhuti' id='"+idzt[x]+"' bs='"+x+"' style='width:"+this.config.width+"px; height:"+this.config.height+"px; background-color:white; left:"+w+"px; top:"+h+"px;'></div>";
$("body").append(nr);
//加載彈窗標題
var content ="<div id='title"+x+"' class='title' bs='"+x+"'>"+this.config.title+"<div id='close"+x+"' class='close' bs='"+x+"'>×</div></div>";
//加載彈窗內容
var nrh = this.config.height - 75;
content = content+"<div id='content"+x+"' bs='"+x+"' class='content' style='width:100%; height:"+nrh+"px;'>"+this.config.content+"</div>";
//加載按鈕
content = content+"<div id='btnx"+x+"' bs='"+x+"' class='btnx'>"+this.config.buttons+"</div>";
//將標題、內容及按鈕添加進窗口
$('#'+idzt[x]).html(content);
//創建遮罩層
if(this.config.isMask)
{
var zz = "<div id='zz'></div>";
$("body").append(zz);
$("#zz").css('display','block');
}
//最大最小限制,以免移動到頁面外
var maxX = $(window).width()-this.config.width;
var maxY = $(window).height()-this.config.height;
var minX = 0,
minY = 0;
//窗口移動
if(this.config.isDrag)
{
//鼠標移動彈出窗
$(".title").bind("mousedown",function(e){
var n = $(this).attr("bs"); //取標識
//使選中的到最上層
$(".zhuti").css("z-index",3);
$('#'+idzt[n]).css("z-index",4);
//取初始坐標
var endX = 0, //移動后X坐標
endY = 0, //移動后Y坐標
startX = parseInt($('#'+idzt[n]).css("left")), //彈出層的初始X坐標
startY = parseInt($('#'+idzt[n]).css("top")), //彈出層的初始Y坐標
downX = e.clientX, //鼠標按下時,鼠標的X坐標
downY = e.clientY; //鼠標按下時,鼠標的Y坐標
//綁定鼠標移動事件
$("body").bind("mousemove",function(es){
endX = es.clientX - downX + startX; //X坐標移動
endY = es.clientY - downY + startY; //Y坐標移動
//最大最小限制
if(endX > maxX)
{
endX = maxX;
} else if(endX < 0)
{
endX = 0;
}
if(endY > maxY)
{
endY = maxY;
} else if(endY < 0)
{
endY = 0;
}
$('#'+idzt[n]).css("top",endY+"px");
$('#'+idzt[n]).css("left",endX+"px");
window.getSelection ? window.getSelection().removeAllRanges():document.selection.empty(); //取消選中文本
});
});
//鼠標按鍵抬起,釋放移動事件
$("body").bind("mouseup",function(){
$("body").unbind("mousemove");
});
}
//關閉窗口
$(".close").click(function(){
var m = this.getAttribute("bs"); //找標識
$('#'+idzt[m]).remove(); //移除彈窗
$('#zz').remove(); //移除遮罩
})
x++; //標識增加
}這個JS文件把彈出窗口的內容,樣式,位置,按鈕,以及遮罩層都做了處理,在引用前好好看看里面的代碼,最好都能弄懂,里面也做了詳細的注釋,希望可以幫的你。
下面是CSS樣式表:
.zhuti
{
position:absolute;
z-index:3;
font-size:14px;
border-radius:5px;
box-shadow:0 0 5px white;
overflow:hidden;
color:#333;
}
.title
{
background-color:#3498db;
vertical-align:middle;
height:35px;
width:100%;
line-height:35px;
text-indent:1em;
}
.close{
float:right;
width:35px;
height:35px;
font-weight:bold;
line-height:35px;
vertical-align:middle;
color:white;
font-size:18px;
}
.close:hover
{
cursor:pointer;
}
.content
{
text-indent:1em;
padding-top:10px;
}
.btnx
{
height:30px;
width:100%;
text-indent:1em;
}
.btn
{
height:28px;
width:80px;
float:left;
margin-left:20px;
color:#333;
}
#zz
{
width:100%;
height:100%;
opacity:0.15;
display:none;
background-color:#ccc;
z-index:2;
position:absolute;
top:0px;
left:0px;
}這個樣式表把每個標簽和所需要的樣式都寫好了,這樣就能節省主要頁面的代碼量,并且讓主頁面看起來非常的整齊,如果要改,只需要在CSS樣式表中修改即可,注意:不管要引用什么文件,必須把Jquery文件放在最前面?。?!
下面是主頁面代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script type="text/javascript" src="jquery-1.11.2.min.js">
</script>
<script type="text/javascript" src="tanchuang.js">
</script>
<link href="tanchuang.css" rel="external nofollow" rel="stylesheet" type="text/css" />
<style type="text/css">
*{
margin: 0px auto;
}
</style>
</head>
<body >
<div >
<input type="button" value="彈出窗口" id="btntc" />
</div>
</body>
<script type="text/javascript">
$(document).ready(function(e) {
$('#btntc').click(function(){
var html = "<div style='color:red'>這是測試的彈窗</div>";
var button ="<input type='button' value='確定' /><input type='button' value='取消' />";
var win = new Window({
width : 400, //寬度
height : 300, //高度
title : '測試彈窗', //標題
content : html, //內容
isMask : false, //是否遮罩
buttons : button, //按鈕
isDrag:true, //是否移動
});
})
});
</script>
</html>同樣的,主頁面里面也加了詳細的注釋,這樣便于日后的理解,希望可以幫的自己和大家。讓我們看看效果吧:

點擊彈出窗口之后的效果:


我們可以看到每個彈出窗口都可以移動,并且可以彈出無數個窗口,如果把遮罩層改成true,那樣就不能再出現第二個彈出窗口了。
一定要記住遮罩層的實用,這樣可以避免很多BUG如果要引用彈出窗口一定要測試好了再使用,以防出現問題,切記!
以上是“jQuery如何實現彈出窗口”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。