溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

jQuery如何實(shí)現(xiàn)彈出窗口

發(fā)布時(shí)間:2021-07-06 14:18:26 來(lái)源:億速云 閱讀:241 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹jQuery如何實(shí)現(xiàn)彈出窗口,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

今天講了Jquery的彈出窗口的組成和用法:

先把引用文件的代碼寫(xiě)好:

// 每個(gè)彈窗的標(biāo)識(shí)
var x =0;

var idzt = new Array();

var Window = function(config){
 
 //ID不重復(fù)
 idzt[x] = "zhuti"+x; //彈窗ID
 
 //初始化,接收參數(shù)
 this.config = {
  width : config.width || 300, //寬度
  height : config.height || 200, //高度
  buttons : config.buttons || '', //默認(rèn)無(wú)按鈕
  title : config.title || '標(biāo)題', //標(biāo)題
  content : config.content || '內(nèi)容', //內(nèi)容
  isMask : config.isMask == false?false:config.isMask || true, //是否遮罩
  isDrag : config.isDrag == false?false:config.isDrag || true, //是否移動(dòng)
  };
 
 //加載彈出窗口
 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);
 
 //加載彈窗標(biāo)題
 var content ="<div id='title"+x+"' class='title' bs='"+x+"'>"+this.config.title+"<div id='close"+x+"' class='close' bs='"+x+"'>×</div></div>";
 //加載彈窗內(nèi)容
 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>";
 
 //將標(biāo)題、內(nèi)容及按鈕添加進(jìn)窗口
 $('#'+idzt[x]).html(content);
 
 
 //創(chuàng)建遮罩層
 if(this.config.isMask)
 {
  var zz = "<div id='zz'></div>";
  $("body").append(zz);
  $("#zz").css('display','block');
 }
 
 //最大最小限制,以免移動(dòng)到頁(yè)面外
 var maxX = $(window).width()-this.config.width;
 var maxY = $(window).height()-this.config.height;
 var minX = 0,
  minY = 0;
 
 //窗口移動(dòng)
 if(this.config.isDrag)
 {
  //鼠標(biāo)移動(dòng)彈出窗
  $(".title").bind("mousedown",function(e){
    
    var n = $(this).attr("bs"); //取標(biāo)識(shí)
    
    //使選中的到最上層
    $(".zhuti").css("z-index",3);
    $('#'+idzt[n]).css("z-index",4);
    
    //取初始坐標(biāo)
    var endX = 0, //移動(dòng)后X坐標(biāo)
     endY = 0, //移動(dòng)后Y坐標(biāo)
     startX = parseInt($('#'+idzt[n]).css("left")), //彈出層的初始X坐標(biāo)
     startY = parseInt($('#'+idzt[n]).css("top")), //彈出層的初始Y坐標(biāo)
     downX = e.clientX, //鼠標(biāo)按下時(shí),鼠標(biāo)的X坐標(biāo)
     downY = e.clientY; //鼠標(biāo)按下時(shí),鼠標(biāo)的Y坐標(biāo)
     
    //綁定鼠標(biāo)移動(dòng)事件
    $("body").bind("mousemove",function(es){
     
     endX = es.clientX - downX + startX; //X坐標(biāo)移動(dòng)
     endY = es.clientY - downY + startY; //Y坐標(biāo)移動(dòng)
     
     //最大最小限制
     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(); //取消選中文本
     
     });
   });
  //鼠標(biāo)按鍵抬起,釋放移動(dòng)事件
  $("body").bind("mouseup",function(){
   
    $("body").unbind("mousemove");
   
   });
 }
 
 //關(guān)閉窗口
 $(".close").click(function(){
  
   var m = this.getAttribute("bs"); //找標(biāo)識(shí)
   $('#'+idzt[m]).remove(); //移除彈窗
   $('#zz').remove(); //移除遮罩 
  
  })
  
  x++; //標(biāo)識(shí)增加
  
}

這個(gè)JS文件把彈出窗口的內(nèi)容,樣式,位置,按鈕,以及遮罩層都做了處理,在引用前好好看看里面的代碼,最好都能弄懂,里面也做了詳細(xì)的注釋?zhuān)M梢詭偷哪恪?/p>

下面是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;
}

這個(gè)樣式表把每個(gè)標(biāo)簽和所需要的樣式都寫(xiě)好了,這樣就能節(jié)省主要頁(yè)面的代碼量,并且讓主頁(yè)面看起來(lái)非常的整齊,如果要改,只需要在CSS樣式表中修改即可,注意:不管要引用什么文件,必須把Jquery文件放在最前面?。。?/p>

下面是主頁(yè)面代碼:

<!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>無(wú)標(biāo)題文檔</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'>這是測(cè)試的彈窗</div>";
   var button ="<input type='button' value='確定' /><input type='button' value='取消' />";

   var win = new Window({
    
    width : 400, //寬度
    height : 300, //高度
    title : '測(cè)試彈窗', //標(biāo)題
    content : html, //內(nèi)容
    isMask : false, //是否遮罩
    buttons : button, //按鈕
    isDrag:true, //是否移動(dòng)
    
    });
  
  })
});
</script>
</html>

同樣的,主頁(yè)面里面也加了詳細(xì)的注釋?zhuān)@樣便于日后的理解,希望可以幫的自己和大家。讓我們看看效果吧:

jQuery如何實(shí)現(xiàn)彈出窗口

點(diǎn)擊彈出窗口之后的效果:

jQuery如何實(shí)現(xiàn)彈出窗口

jQuery如何實(shí)現(xiàn)彈出窗口

我們可以看到每個(gè)彈出窗口都可以移動(dòng),并且可以彈出無(wú)數(shù)個(gè)窗口,如果把遮罩層改成true,那樣就不能再出現(xiàn)第二個(gè)彈出窗口了。

一定要記住遮罩層的實(shí)用,這樣可以避免很多BUG如果要引用彈出窗口一定要測(cè)試好了再使用,以防出現(xiàn)問(wèn)題,切記!

以上是“jQuery如何實(shí)現(xiàn)彈出窗口”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI