jQuery代码轻松实现DIV弹出效果

开发 前端
现在很多网站都流行DIV弹出式的对话框或是信息显示框,因为使用jQuery可以不费大力气实现这种效果,这里就向大家描述一下jQuery DIV弹出效果实现代码。

这里和大家分享一下jQuery DIV弹出效果实现代码,这种DIV弹出式的对话框或是信息显示框现在在很多网站都比较流行,因为使用jQuery可以不费大力气实现这种效果,请看本文的详细介绍。

jQuery DIV弹出效果实现代码

现在很多网站都流行这种DIV弹出式的对话框或是信息显示框,很想将这个流行元素加入到自己的项目中。使用jQuery可以不费大力气实现这种效果。将其记录到我的Blog中,与业界朋友们起分享。

先上个效果图,可以点击Close按钮或是在遮罩层上任意处点击,就可以关闭DIV弹出层。

HTML代码

代码如下: 

<dividdivid='pop-div'style="width:300px"class="pop-box"> 
<h4>标题位置</h4> 
<divclassdivclass="pop-box-body"> 
<p> 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

 正文内容 

</p> 
</div> 
<divclassdivclass='buttonPanel'style="text-align:right"style="text-align:right"> 
<inputvalueinputvalue="Close"id="btn1"onclick="hideDiv('pop-div');"type="button"/> 
</div> 
</div> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 代码很简洁。最外层是一个大的DIV作为弹出层的容器,H4作为DIV弹出层的标题,又一个DIV用于弹出层正文内容显示,再一个Div用于放置一些按钮。

CSS代码

代码如下: 

.pop-box{  
z-index:9999;/*这个数值要足够大,才能够显示在最上层*/  
margin-bottom:3px;  
display:none;  
position:absolute;  
background:#FFF;  
border:solid1px#6e8bde;  
}  
 
.pop-boxh4{  
color:#FFF;  
cursor:default;  
height:18px;  
font-size:14px;  
font-weight:bold;  
text-align:left;  
padding-left:8px;  
padding-top:4px;  
padding-bottom:2px;  
background:url("../images/header_bg.gif")repeat-x00;  
}  
 
.pop-box-body{  
clear:both;  
margin:4px;  
padding:2px;  
}  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

 JS代码

代码如下: 

functionpopupDiv(div_id){  
vardiv_obj=$("#"+div_id);  
varwindowWidth=document.documentElement.clientWidth;  
varwindowHeight=document.documentElement.clientHeight;  
varpopupHeight=div_obj.height();  
varpopupWidth=div_obj.width();  
//添加并显示遮罩层  
$("<dividdivid='mask'></div>").addClass("mask")  
.width(windowWidth*0.99)  
.height(windowHeight*0.99)  
.click(function(){hideDiv(div_id);})  
.appendTo("body")  
.fadeIn(200);  
div_obj.css({"position":"absolute"})  
.animate({left:windowWidth/2-popupWidth/2,  
top:windowHeight/2-popupHeight/2,opacity:"show"},"slow");  
}  
 
functionhideDiv(div_id){  
$("#mask").remove();  
$("#"+div_id).animate({left:0,top:0,opacity:"hide"},"slow");  
}  
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

 详细出处参考:http://www.jb51.net/article/18950.htm

【编辑推荐】

  1. 两种方式解决DIV弹出窗口问题
  2. CSS样式表特点及嵌入网页的四种途径
  3. CSS中DIV弹出层问题解决方案
  4. CSS外边距设置属性margin用法
  5. 探究CSS高级语法中选择器分组和CSS继承用法
责任编辑:佚名 来源: jb51.net
相关推荐

2010-08-30 10:09:07

JavaScriptDIV

2010-09-14 12:58:41

DIV+CSS圆角

2010-08-26 15:59:38

DIV圆角

2011-03-30 14:33:57

jQueryJavaScript

2015-07-23 15:15:06

动态弹出

2012-06-07 15:14:52

jQuery

2011-09-01 13:17:46

JQuery滚动

2022-09-21 14:51:21

ArkUI信件弹出

2010-08-24 14:10:44

div style

2010-09-10 09:57:27

DIV样式

2010-09-14 09:18:28

DIVCSS

2010-10-09 11:09:32

JavaScript弹

2009-09-23 17:19:19

jQuery页面渐显效

2012-01-17 10:43:26

Java

2015-03-24 13:10:49

javascript中关村论坛提示效果

2012-04-10 10:41:59

jQueryASP.NET

2013-07-29 14:28:43

JQueryJQuery实现分页分页程序代码

2020-05-06 19:57:09

代码开发编码

2010-09-07 09:18:16

DIV弹出窗口

2011-03-02 13:15:26

HTML 5jQuery
点赞
收藏

51CTO技术栈公众号