博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js+css实现模态层效果
阅读量:6910 次
发布时间:2019-06-27

本文共 2732 字,大约阅读时间需要 9 分钟。

在做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=$("
"); modal.css("width","100%"); modal.css("height","100%"); //模态层 var modal_div=$("
"); modal_div.css("width","100%"); modal_div.css("height","100%"); //信息框 var infoWin=$("
"); 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=$("
"); var infoWin_title_close=$("
") infoWin_title_close.on("click",function(){ console.log("Close Modal!"); modal.hide(); }); var infoWin_title_content=$("
") infoWin_title_content.append(title); infoWin_title.append(infoWin_title_close); infoWin_title.append(infoWin_title_content); //内容 var infoWin_content=$("
"); infoWin_content.append(content); //信息框加入标题和内容 infoWin.append(infoWin_title); infoWin.append(infoWin_content); //模态层容器加入模态层和信息框 modal.append(modal_div); modal.append(infoWin); //将模态层加入到容器 container.append(modal); }})(jQuery);
将之封装成一个jquery插件。提高可重用性,在页面短的调用方式例如以下:

页面端涉及到的样式:

调用modal插件:

        
当中,content可为html代码。

你可能感兴趣的文章
hug and Compression Resistance
查看>>
sql server 2008分页
查看>>
lintcode:Pow(x, n)
查看>>
WebService中使用Aspose.Cells.dll
查看>>
Android菜鸟的成长笔记(28)——Google官方对Andoird 2.x提供的ActionBar支持
查看>>
【转载】装饰模式与代理模式的区别
查看>>
Persona——Web人物角色介绍
查看>>
一个三年工作经验的软件工程师的经验之谈
查看>>
Keepalived+Redis高可用部署(第二版)
查看>>
理解Linux中断 (3)【转】
查看>>
3 hql语法及自定义函数(含array、map讲解) + hive的java api
查看>>
欢迎各位技术牛人增加Swift QQ群:343549891
查看>>
Linux使用imagemagick的convert命令压缩图片、节省服务器空间
查看>>
selenium测试(Java)-- 显式等待(九)
查看>>
MySQL 5.7 mysqlpump 备份工具说明
查看>>
Entity Framework Core 之数据库迁移
查看>>
ssh的用户配置文件config管理ssh会话
查看>>
安全过滤函数
查看>>
C#学习记录二:高级数据存储方式
查看>>
【162】一个程序只能运行一个
查看>>