欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實現(xiàn)彈出窗口彈出div層的實例代碼

 更新時間:2017年01月09日 16:32:38   作者:samruo  
這篇文章主要介紹了jQuery實現(xiàn)彈出窗口彈出div層的實例代碼,點擊頁面的鏈接,彈出一個div層,同時頁面的其他部分變灰并且不能點擊,,具體實現(xiàn)代碼大家參考下本文吧

通過今天的jquery實例學(xué)習(xí),我們要達(dá)到這樣的效果:點擊頁面的鏈接,彈出一個div層,同時頁面的其他部分變灰并且不能點擊;無論是改變?yōu)g覽器窗口大小還是下拉滾動條,這個彈出層都能始終保持居中;點擊頁面的關(guān)閉按鈕,彈出層消失,頁面恢復(fù)原樣。

這里借鑒之前的一篇文章《基于jQuery的固定飄浮層》,使彈出窗口可以始終固定在瀏覽器的正中間。在這里有一個要點,就是如何使頁面的其他地方在彈出窗口的同時變灰。我使用的方法就是在點擊鏈接彈出div層的時候,給頁面增加一個div層,這個層就“負(fù)責(zé)”使頁面變灰。點擊關(guān)閉后,刪除這個層就能使頁面恢復(fù)原樣。不知道有沒有更好的方法,有的話請告訴我哦。

其他應(yīng)該沒什么問題了,還是很簡單的,在這里順便貼上jQuery代碼:

$(function(){ 
  var screenwidth,screenheight,mytop,getPosLeft,getPosTop 
  screenwidth = $(window).width(); 
  screenheight = $(window).height(); 
  //獲取滾動條距頂部的偏移 
  mytop = $(document).scrollTop(); 
  //計算彈出層的left 
  getPosLeft = screenwidth/2 - 260; 
  //計算彈出層的top 
  getPosTop = screenheight/2 - 150; 
  //css定位彈出層 
  $("#box").css({"left":getPosLeft,"top":getPosTop}); 
  //當(dāng)瀏覽器窗口大小改變時... 
  $(window).resize(function(){ 
  <span style="white-space:pre">  </span>screenwidth = $(window).width(); 
  <span style="white-space:pre">  </span>screenheight = $(window).height(); 
  <span style="white-space:pre">  </span>mytop = $(document).scrollTop(); 
  <span style="white-space:pre">  </span>getPosLeft = screenwidth/2 - 260; 
  <span style="white-space:pre">  </span>getPosTop = screenheight/2 - 150; 
  <span style="white-space:pre">  </span>$("#box").css({"left":getPosLeft,"top":getPosTop+mytop}); 
  }); 
  //當(dāng)拉動滾動條時... 
  $(window).scroll(function(){ 
  <span style="white-space:pre">  </span>screenwidth = $(window).width(); 
  <span style="white-space:pre">  </span>screenheight = $(window).height(); 
  <span style="white-space:pre">  </span>mytop = $(document).scrollTop(); 
  <span style="white-space:pre">  </span>getPosLeft = screenwidth/2 - 260; 
  <span style="white-space:pre">  </span>getPosTop = screenheight/2 - 150; 
  <span style="white-space:pre">  </span>$("#box").css({"left":getPosLeft,"top":getPosTop+mytop}); 
  }); 
  //點擊鏈接彈出窗口 
  $("#popup").click(function(){ 
  <span style="white-space:pre">  </span>$("#box").fadeIn("fast"); 
  <span style="white-space:pre">  </span>//獲取頁面文檔的高度 
  <span style="white-space:pre">  </span>var docheight = $(document).height(); 
  <span style="white-space:pre">  </span>//追加一個層,使背景變灰 
  <span style="white-space:pre">  </span>$("body").append("<div id='greybackground'></div>"); 
  <span style="white-space:pre">  </span>$("#greybackground").css({"opacity":"0.5","height":docheight}); 
  <span style="white-space:pre">  </span>return false; 
  }); 
  //點擊關(guān)閉按鈕 
  $("#closeBtn").click(function() { 
  <span style="white-space:pre">  </span>$("#box").hide(); 
  <span style="white-space:pre">  </span>//刪除變灰的層 
  <span style="white-space:pre">  </span>$("#greybackground").remove(); 
  <span style="white-space:pre">  </span>return false; 
  }); 
}); 

html代碼:

<!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>jquery pop up</title> 
<script src=http://blog.soso.com/qz.q/"jquery.js" type="text/javascript"></script> 
<style type="text/css"> 
  * {margin:0;padding:0;} 
  #wrapper {height:1000px;} 
  #box {display:none;position:absolute;width:520px;height:300px;border:#f60 solid 2px;z-index:200;background:#fff;} 
  #closeBtn {position:absolute;right:10px;top:10px;cursor:pointer;} 
  #greybackground {background:#000;display:block;z-index:100;width:100%;position:absolute;top:0;left:0;} 
</style> 
</head> 
<body> 
 <div id="wrapper"> 
  <a href=http://blog.soso.com/qz.q/"#" id="popup">點擊彈出div窗口</a> 
 </div> 
 <div id="box"> 
 <span style="white-space:pre"> </span><span id="closeBtn">關(guān)閉</span> 
 </div> 
</body> 
</html> 

以上所述是小編給大家介紹的jQuery實現(xiàn)彈出窗口彈出div層的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論