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

模态层效果

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

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

modal.css

  1. html,body{
  2. font-size: 12px;
  3. font-family: "微软雅黑";
  4. }
  5. .modal{
  6. position: absolute;
  7. top:0px;
  8. left: 0px;
  9. border: 1px solid #000;
  10. background: #555;
  11. opacity: 0.4;
  12. }
  13. .infowin{
  14. border: 1px solid #777777;
  15. background: #fff;
  16. box-shadow: 0 0 0.75em #777777;
  17. -moz-box-shadow: 0 0 0.75em #777777;
  18. -webkit-box-shadow: 0 0 0.75em #777777;
  19. -o-box-shadow: 0 0 0.75em #777777;
  20. border-radius: 5px;
  21. -moz-border-radius: 5px;
  22. -webkit-border-radius: 5px;
  23. -o-border-radius: 5px;
  24. }
  25. .title{
  26. border-bottom: 1px solid #777777;
  27. }
  28. .title_content{
  29. padding: 5px;
  30. padding-left: 10px;
  31. font-size: 14px;
  32. font-family: "微软雅黑";
  33. font-weight: bold;
  34. }
  35. .close{
  36. background: url(close.png) no-repeat;
  37. width: 25px;
  38. height: 25px;
  39. float: right;
  40. }
  41. .close:hover{
  42. cursor: pointer;
  43. }
  44. .content{
  45. padding-left: 10px;
  46. padding-top: 10px;
  47. }

jquery.modal.js

  1. (function($){
  2. $.fn.modalInfowindow = function(options){
  3. var defaults = {};
  4. var options = $.extend(defaults, options);
  5. var container=$(this);
  6. var width=options.width, height=options.height, title=options.title, content=options.content;
  7. //模态层容器
  8. var modal=$("<div id='modal'></div>");
  9. modal.css("width","100%");
  10. modal.css("height","100%");
  11. //模态层
  12. var modal_div=$("<div class='modal'></div>");
  13. modal_div.css("width","100%");
  14. modal_div.css("height","100%");
  15. //信息框
  16. var infoWin=$("<div class='infowin'></div>");
  17. infoWin.css("width",width+"px");
  18. infoWin.css("height",height+"px");
  19. infoWin.css("position","absolute");
  20. infoWin.css("top",(container.height()-height)/2+"px");
  21. infoWin.css("left",(container.width()-width)/2+"px");
  22. //标题
  23. var infoWin_title=$("<div class='title'></div>");
  24. var infoWin_title_close=$("<div class='close'></div>")
  25. infoWin_title_close.on("click",function(){
  26. console.log("Close Modal!");
  27. modal.hide();
  28. });
  29. var infoWin_title_content=$("<div class='title_content'></div>")
  30. infoWin_title_content.append(title);
  31. infoWin_title.append(infoWin_title_close);
  32. infoWin_title.append(infoWin_title_content);
  33. //内容
  34. var infoWin_content=$("<div class='content'></div>");
  35. infoWin_content.append(content);
  36. //信息框加入标题和内容
  37. infoWin.append(infoWin_title);
  38. infoWin.append(infoWin_content);
  39. //模态层容器加入模态层和信息框
  40. modal.append(modal_div);
  41. modal.append(infoWin);
  42. //将模态层加入到容器
  43. container.append(modal);
  44. }
  45. })(jQuery);

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

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

页面端涉及到的样式:

  1. <style type="text/css">
  2. .container{
  3. width: 600px;
  4. height: 300px;
  5. position: relative;
  6. border: 1px solid #777777;
  7. }
  8. .button{
  9. position: absolute;
  10. left: 15%;
  11. top: 15%;
  12. background: #eee;
  13. padding: 5px 10px ;
  14. }
  15. .button:hover{
  16. background: #aaa;
  17. cursor: pointer;
  18. }
  19. </style>

调用modal插件:

  1. <script type="text/javascript" src="jquery-1.8.3.js"></script>
  2. <script type="text/javascript" src="jquery.modal.js"></script>
  3. <script type="text/javascript">
  4. function ShowModal(){
  5. $('#container').modalInfowindow({
  6. width:300,
  7. height:150,
  8. title:"中国",
  9. content:"中国是中华人名共和国的简称"
  10. });
  11. }
  12. </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. Activity具体解释(生命周期、以各种方式启动Activity、状态保存,全然退出等)

    一.什么是Activity? 简单的说:Activity就是布满整个窗体或者悬浮于其它窗体上的交互界面.在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Ac ...

  2. mysql中DES加密解密

      DES_DECRYPT(crypt_str[,key_str]) 使用DES_ENCRYPT()加密一个字符串.若出现错误,这个函数会返回 NULL. 注意,这个函数只有当MySQL在SSL 的支 ...

  3. Qt 域名转成IP

    #include <stdio.h>#include <stdlib.h>#include <netdb.h>#include <arpa/inet.h> ...

  4. XML的DOM、SAX、DEMO4J及DEMO4J整合Path的代码例子

    1.DMO解析 package cn.itcast.xml.dom; import java.io.File; import javax.xml.parsers.DocumentBuilder; im ...

  5. OD: Exploit Me - Overwrite Return Address

    修改邻接变量的方法对代码环境限制比较多,更通用.更强大的方法是修改 EBP.返回地址等状态值. 为了方便调试,修改之前的代码如下: #include<stdio.h> #include&l ...

  6. HTML基础总结<文本格式>

    HTML 文本格式化标签 标签 描述 <b> 定义粗体文本 <em> 呈现为被强调的文本 <i> 定义斜体字 <small> 定义小号字 <str ...

  7. 让 asp.net mvc 支持 带有+ _ 等特殊字符的路由

    最近配置微信 业务域名 时,需要在服务器的根目录中上传一个文本文件,而这个文本文件需要放这样的目录中: 于在就在 服务器目录中创建了对应的文件夹,并将kuPv.txt上传,但是访问时,却怎么也访问不到 ...

  8. 一步步学会使用SeaJS(转)

    原文出处:一步步学会使用SeaJS 2.0 本文分为以下8步,熟悉之后就能够熟练使用SeaJS,从此之后你的生活会变得更加轻松愉悦! 1.SeaJS是什么? 2.下载并检阅SeaJS 3.建立工程和各 ...

  9. php 之 文件上传(0523)

    如何上传图片: 上传页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  10. rsync相关整理

    第一部分 rsync服务端配置 1.下载安装 a. yum安装.  yum install rsync    b. 下载rsync安装文件安装        #tar zxvf rsync-2.6.9 ...