使用css和jquery创建模态弹窗

今天自己尝试做了一下模态弹窗的样式,把代码贴在这里供以后复习。
1、布局主要使用flex布局。
2、毛玻璃效果和条纹背景的代码参考《css揭秘》。
3、弹窗的样式参考jquery-ui的弹窗样式。

一、模态弹窗

1、html部分

<!-- 这是一个普通的按钮,用来触发弹窗显示。 -->
<button id="alert-window" class="btn" type="button">弹窗</button>
<!-- 这是一个普通的文本框,用来验证模态效果。 -->
<input>
<!-- **************下面是弹窗的主体代码************* -->
<!-- dialog-1: 在按钮触发时通过id指定触发哪个弹窗。overlay: 指示遮罩层元素 -->
  <div id="dialog-1" class="overlay">
    <!-- dialog:指示弹窗窗体。 -->
  	<div class="dialog">
  		<!-- dialog-header:指示弹窗头部。 -->
	  	<div class="dialog-header">
	  	    <!-- dialog-title:指示弹窗标题。 -->
	  		<div class="dialog-title">这是弹框标题</div>
	  		<!-- dialog-close-icon:指示关闭按钮样式。dialog-close:指示关闭弹窗触发器 -->
	  		<div class="dialog-close-icon dialog-close"></div>
	  	</div>
	  	<!-- dialog-content:指示弹窗内容。 -->
	  	<div class="dialog-content">
	  		这是弹框内容
	  	</div>
	  	<!-- dialog-footer:指示弹窗底部。 -->
	  	<div class="dialog-footer">
	  	    <!-- btn:指示普通按钮样式。dialog-close:指示关闭弹窗触发器。 -->
	  		<button class="btn dialog-close">OK</button>
	  		<button class="btn">cancel</button>
	  	</div>
	</div>
  </div>

2、css部分

	<style type="text/css">
		/* 遮罩层 */
		.overlay {
			position: fixed; /* 将遮罩层铺满整个屏幕 */
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			background: #abababcc; /* 设置遮罩层背景色为有点透明的浅灰色 */
			display: none; /* 设置遮罩层默认不显示 */
			justify-content: center; /* 设置弹窗左右居中 */
		}
		/* 弹出窗 */
		.dialog {
			z-index: 1; /* 为弹窗指定更高的z-index,以便绘制在遮罩层的上层 */
			width: 400px; /* 设置弹窗宽度 */
			height: 350px; /* 设置弹窗高度 */
			background: #fff; /* 设置弹窗背景色为白色 */
			align-self: center; /* 设置弹窗上下居中 */
			display: flex; /* 设置弹窗内布局为flex布局 */
			flex-direction: column; /* 设置弹窗内flex布局方向为垂直方向 */
			border-radius: 4px; /* 设置弹窗有4px的圆角 */
		}
		.dialog-header {
			background: #ccc; /* 设置弹窗头部的背景色为灰色 */
			display: flex; /* 设置弹窗头部的布局为flex布局 */
			margin: .2em; /* 设置弹窗头部的外边距 */
			padding: .2em .2em .2em .5em; /* 设置弹窗头部的内边距 */
			height: 2.5em; /* 设置弹窗头部的高度 */
			border-radius: 3px; /* 设置弹窗头部的圆角 */
			align-items: center; /* 设置弹窗头部的内容上下居中 */
		}
		.dialog-header .dialog-title {
			flex-grow: 1; /* 设置弹窗头部的标题部分充满剩余空间 */
		}
		.dialog-header .dialog-close-icon {
			background: #e6e6e6; /* 设置弹窗头部的关闭按钮背景色 */
			padding: .1em .35em; /* 设置弹窗头部的关闭按钮内边距 */
			margin-right: .2em; /* 设置弹窗头部的外边距 */
			cursor: pointer; /* 设置弹窗头部的关闭按钮的鼠标样式 */
			border-radius: 3px; /* 设置弹窗头部的关闭按钮的圆角 */
		}
		.dialog-header .dialog-close-icon:hover {
			background: #666; /* 设置弹窗头部的关闭按钮鼠标悬浮背景色 */
		}
		.dialog-header .dialog-close-icon:after { /* 设置弹窗头部的关闭按钮的内容(x) */
			content: "\d7";
			font-size: 1.2em;
			color: #666;
		}
		.dialog-header .dialog-close-icon:hover:after {
			color: #fff;
		}
		.dialog-content {
			flex-grow: 1; /* 设置弹窗内容部分充满剩余空间 */
			margin: 0 .2em; /* 设置弹窗内容部分的外边距 */
		}
		.dialog-footer {
			border-style: solid; /* 设置弹窗底部的边框样式为实线 */
			border-width: 1px 0 0; /* 设置弹窗底部只有上边框 */
			padding: .5em; /* 设置弹窗底部的内边距 */
			margin: 0 .2em; /* 设置弹窗底部的外边距 */
			display: flex; /* 设置弹窗底部的布局为flex布局 */
			justify-content: flex-end; /* 设置弹窗底部的内容右对齐 */
		}
		.dialog-footer .btn{ /* 设置弹窗底部的按钮样式 */
			font-size: medium;
			width: 5em;
			margin: .2em .5em;
			padding: .5em;
		}
		.btn { /* 设置按钮样式 */
			border: 0;
			border-radius: 3px;
			padding: 1em;
			font-size: large;
			background: #ccc;
			width: 8em;
		}
		.btn:hover { /* 设置按钮的悬浮样式 */
			background: #666;
			color: #fff;
			cursor: pointer;
		}
	</style>

3、js部分

下面是需要在初始时添加的js代码:

  <script type="text/javascript">
    // 弹出弹窗的方法,参数为class为overlay的元素id。
  	function openDialog(dialogId){
  	    //设置遮罩层为flex布局,显示遮罩层和弹窗。
  		$("#"+dialogId+"").css("display", "flex"); 
  	}
  	// 关闭弹窗的事件处理。为想要关闭弹窗的按钮添加'dialog'类即可。
  	$(".dialog-close").click(function(){
  		//找到当前关闭弹窗按钮所在的遮罩层,并隐藏。
  		$(this).parents(".overlay").hide(); 
  	});
  </script>

下面是需要在指定弹窗按钮时添加的js代码:

  <script type="text/javascript">
    // alert-window:点击后显示弹框的元素id。
    // dialog-1:弹框遮罩层元素的id。
  	$("#alert-window").click(function(){
  		openDialog("dialog-1");
  	});
  </script>

4、最终效果:

Alt

二、毛玻璃背景效果

1、html部分

  /* 注意要使用dialog-background元素把除了弹框部分的内容包裹起来。 */
  <div class="dialog-background">
  	<button id="alert-window" class="btn" type="button">弹窗</button>
  	<input>
  	/* 引入色彩丰富的图片来展示毛玻璃效果。 */
  	<img src="./bg.PNG">
  </div>
  
  	<div id="dialog-1" class="dialog">
	  	<div class="dialog-header">
	  		<div class="dialog-title">
	  			这是弹框标题
	  		</div>
	  		<div class="dialog-close-icon dialog-close"></div>
	  	</div>
	  	<div class="dialog-content">
	  		这是弹框内容
	  	</div>
	  	<div class="dialog-footer">
	  		<button class="btn dialog-close">OK</button>
	  		<button class="btn">cancel</button>
	  	</div>
	</div>

2、css部分

	<style type="text/css">
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}
		/* 毛玻璃效果的背景 */
		.dialog-background {
			transition: .1s filter; /* 毛玻璃效果的渐变时间 */
		}
		.dialog-background.de-emphasized {
			filter: blur(3px) contrast(.8) brightness(.8);/* 毛玻璃效果的滤镜 */
		}
		/* 弹出窗 */
		.dialog {
			z-index: 1;
			position: absolute;
			top: 80px;
			left: 35%;
			width: 400px;
			height: 350px;
			background: #fff;
			display: none;
			flex-direction: column;
			border-radius: 4px;
		}
		.dialog-header {
			background: #ccc;
			display: flex;
			margin: .2em;
			padding: .2em .2em .2em .5em;
			height: 2.5em;
			border-radius: 3px;
			align-items: center;
		}
		.dialog-header .dialog-title {
			flex-grow: 1;
		}
		.dialog-header .dialog-close-icon {
			background: #e6e6e6;
			padding: .1em .35em;
			margin-right: .2em;
			cursor: pointer;
			border-radius: 3px;
		}
		.dialog-header .dialog-close-icon:hover {
			background: #666;
		}
		.dialog-header .dialog-close-icon:after {
			content: "\d7";
			font-size: 1.2em;
			color: #666;
		}
		.dialog-header .dialog-close-icon:hover:after {
			color: #fff;
		}
		.dialog-content {
			flex-grow: 1;
			margin: 0 .2em;
		}
		.dialog-footer {
			border-style: solid;
			border-width: 1px 0 0;
			padding: .5em;
			margin: 0 .2em;
			display: flex;
			justify-content: flex-end;
		}
		.dialog-footer .btn{
			font-size: medium;
			width: 5em;
			margin: .2em .5em;
			padding: .5em;
		}
		.btn {
			border: 0;
			border-radius: 3px;
			padding: 1em;
			font-size: large;
			background: #ccc;
			width: 8em;
		}
		.btn:hover {
			background: #666;
			color: #fff;
			cursor: pointer;
		}
	</style>

3、js部分

  <script type="text/javascript">
    // 点击id为alert-window的按钮后显示弹窗。
  	$("#alert-window").click(function(){
  		openDialog("dialog-1");
  		$(".dialog-background").addClass("de-emphasized");
  	});
  	// 显示弹窗的方法。
  	function openDialog(dialogId){
  		$("#"+dialogId+"").css("display", "flex");
  	}
  	// 为所有dialog-close的元素添加关闭弹窗的事件。
  	$(".dialog-close").click(function(){
  		$(this).parents(".dialog").hide();
  		$(".dialog-background").removeClass("de-emphasized");
  	});
  </script>

4、实际效果

在这里插入图片描述

三、斜条纹效果

斜条纹效果的弹窗只是改变了一下模态框的背景色而已,所以把模态框的背景改成下面的代码就可以啦。(《css揭秘》提供的这个生成斜条纹的方法真是好用。)

			/* 设置斜条纹颜色 */
			background: #aab;
			/* 设置斜条纹样式 */
			background-image: repeating-linear-gradient(30deg,
							  	hsla(0, 0%, 100%, .1),
							  	hsla(0, 0%, 100%, .1) 15px,
							  	transparent 0, transparent 30px);

最终效果:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
软件功能: * 无限级弹出窗口 * Esc退出block弹出窗口 * 可拖动窗口 * 模态窗口 * 模态alert警告对话框 * 模态confirm对话框 * 页面局部模态 * 绑定按钮响应函数 * 弹出窗口加载iframe * 自定义背景样式 组件提供了六个函数: $.funkyUI // 弹出模态窗口 $.unfunkyUI // 关闭模态窗口 $.alert //警告提示对话框 $.confirm //确认和取消对话框 $.fn.block //块模态 $.fn.unblock//解除块模态 调用示例: $.blockUI({ url:"1.html",//弹出窗口显示的内容,使用iframe OKEvent:okEvent,//okEvent是自定义的确定按钮响应函数, css:{width:"700",height:"500"} }); $.alert("这是警告窗口"); $.confirm("这是个Boolean窗口"); $('#blocked').block();//id为blocked的元素设置为只读 $('#blocked').unblock();//解除 实现的思路: 我在主窗体中创建一个层,用来作为弹出窗体,包含一个iframe,通过参数指定URL,作为显示的内容,我觉得可能会有利于代码的重用,代码管理起来要方便一些 弹出窗口中含有的弹出窗口也在主窗体中创建,这样结构简单一点,比较容易管理。我在创建弹出窗口的时候在主窗体中声明一个栈,把当前iframe的window对象压入栈,这样不管我现在弹出了多少个窗口总能很容易的找到其中的任意一个的对象。 有感兴趣的把我的代码下载回去看看,然后给我提提意见,帮我提高。。。 觉得可用的随便用,有不明白的问我

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值