在做web前端的时候,有些时候会涉及到模态层,在此提供一种实现思路。希望对大家实用。先贴效果吧:

模态层效果

以下说说在写模态层的时候的思路:通过可配置的參数width,height,title以及content用来设定弹出的信息框显示的内容,并通过可配置參数container用来设定模态层显示的区域。

思路非常easy,主要是一些css样式和js处理。详见源代码:

modal.css

html,body{
font-size: 12px;
font-family: "微软雅黑";
}
.modal{
position: absolute;
top:0px;
left: 0px;
border: 1px solid #000;
background: #555;
opacity: 0.4;
}
.infowin{
border: 1px solid #777777;
background: #fff;
box-shadow: 0 0 0.75em #777777;
-moz-box-shadow: 0 0 0.75em #777777;
-webkit-box-shadow: 0 0 0.75em #777777;
-o-box-shadow: 0 0 0.75em #777777;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-o-border-radius: 5px;
}
.title{
border-bottom: 1px solid #777777;
}
.title_content{
padding: 5px;
padding-left: 10px;
font-size: 14px;
font-family: "微软雅黑";
font-weight: bold;
}
.close{
background: url(close.png) no-repeat;
width: 25px;
height: 25px;
float: right;
}
.close:hover{
cursor: pointer;
}
.content{
padding-left: 10px;
padding-top: 10px;
}

jquery.modal.js

(function($){
$.fn.modalInfowindow = function(options){
var defaults = {};
var options = $.extend(defaults, options);
var container=$(this);
var width=options.width, height=options.height, title=options.title, content=options.content;
//模态层容器
var modal=$("<div id='modal'></div>");
modal.css("width","100%");
modal.css("height","100%");
//模态层
var modal_div=$("<div class='modal'></div>");
modal_div.css("width","100%");
modal_div.css("height","100%");
//信息框
var infoWin=$("<div class='infowin'></div>");
infoWin.css("width",width+"px");
infoWin.css("height",height+"px");
infoWin.css("position","absolute");
infoWin.css("top",(container.height()-height)/2+"px");
infoWin.css("left",(container.width()-width)/2+"px");
//标题
var infoWin_title=$("<div class='title'></div>");
var infoWin_title_close=$("<div class='close'></div>")
infoWin_title_close.on("click",function(){
console.log("Close Modal!");
modal.hide();
});
var infoWin_title_content=$("<div class='title_content'></div>")
infoWin_title_content.append(title);
infoWin_title.append(infoWin_title_close);
infoWin_title.append(infoWin_title_content);
//内容
var infoWin_content=$("<div class='content'></div>");
infoWin_content.append(content);
//信息框加入标题和内容
infoWin.append(infoWin_title);
infoWin.append(infoWin_content);
//模态层容器加入模态层和信息框
modal.append(modal_div);
modal.append(infoWin);
//将模态层加入到容器
container.append(modal);
}
})(jQuery);

将之封装成一个jquery插件。提高可重用性,在页面短的调用方式例如以下:

<div class="container" id="container">
<a class="button" onclick="ShowModal()">弹出窗体</a>
</div>

页面端涉及到的样式:

<style type="text/css">
.container{
width: 600px;
height: 300px;
position: relative;
border: 1px solid #777777;
}
.button{
position: absolute;
left: 15%;
top: 15%;
background: #eee;
padding: 5px 10px ;
}
.button:hover{
background: #aaa;
cursor: pointer;
}
</style>

调用modal插件:

 <script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="jquery.modal.js"></script>
<script type="text/javascript">
function ShowModal(){
$('#container').modalInfowindow({
width:300,
height:150,
title:"中国",
content:"中国是中华人名共和国的简称"
});
}
</script>

当中,content可为html代码。

源代码下载

js+css实现模态层效果的更多相关文章

  1. 使用bootstrap的JS插件实现模态框效果

    在上一篇文章中,我们使用 js+css 实现了模态框效果,在理解了模态框的基本实现方法和实现效果后,我们就要寻找更快捷的方法,又快又好的来完成模态框开发需求,从而节约时间,提高效率.一个好的轮子,不仅 ...

  2. 实用js+css多级树形展开效果导航菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. js+css实现带缓冲效果右键弹出菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. js 实现弹出层效果

    代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  5. JS /CSS 实现模态框(注册和登录组件)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. JS实现弹出层效果

    很多时候我们想去某某网站干点什么的时候,就会让我们先注册登录后才可以访问内容,而现在很多网站注册登录的时候都会有一种遮罩层的效果,就是背景是带有透明度的黑色遮罩,盖满整个网站,然后登录框弹出固定在屏幕 ...

  7. (JS+CSS)实现图片放大效果

    代码很简单,在这里就不过多阐述,先上示例图: 实现过程: html部分代码很简单 <div id="outer"> <p>点击图片</p> &l ...

  8. 纯js+css实现loading等待效果

    此插件是基于jqueryUI的widget,下面是具体实现代码 第一部分css: /***loading***/ .loading-box{ position:absolute; text-align ...

  9. js+html实现遮罩层效果(收藏哦)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script ty ...

随机推荐

  1. 百度之星资格赛 hdu 4826 Labyrinth 动态规划

    /********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上 ...

  2. HDU 1015 Safecracker 解决问题的方法

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is locked in a Kl ...

  3. view动画库

    Android View Animations这个项目收集了各种有趣的动画效果. 所有效果: Attension Flash, Pulse, RubberBand, Shake, Swing, Wob ...

  4. SQL Server系统表讲解

    1. sysobjects http://www.cnblogs.com/atree/p/SQL-Server-sysobjects.html   2.syscomments http://www.c ...

  5. Zookeeper+Kafka+Storm+HDFS实践

    Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. Hadoop一般用在离线的分析计算中,而storm区别于hadoop,用在实时的流式计算中,被广泛用来 ...

  6. 关于R文件丢失的一个问题

    android studio在编辑布局文件时,一般为了省事,如TextView控件中的text属性这样写 android:text="<500",编译不会报错,但是运行时会出 ...

  7. lib,dll区别 及 VS中如何添加lib,dll

    1.加载lib/头文件 分两种方法: (1)适用于当前项目 第一步:项目->属性->C/C++->常规->附加包含目录(浏览.h文件的路径) 添加包含文件 第二步:项目-> ...

  8. mysql索引之唯一索引

    mysql 的唯一索引一般用于不重复的字段,一般会把表中的id设为唯一索引,创建唯一索引的目的不是为了提高查询速度,而是为了避免数据重复,注意:唯一索引可以有多个,但是列值必须唯一,创建唯一索引使用关 ...

  9. Mysql中natural join和inner join的区别

    假设有如下两个表TableA,TableB TableA TableB Column1 Column2 Column1 Column3 1 2 1 3 TableA的Column1列名和TableB的 ...

  10. gcd - b- 201611302317

    谈到iOS多线程,一般都会谈到四种方式:pthread.NSThread.GCD和NSOperation.其中,苹果推荐也是我们最经常使用的无疑是GCD.对于身为开发者的我们来说,并发一直都很棘手,如 ...