弹出弹框

效果展示

实现原理

  1. html结构比较简单,即:

    <div>遮罩层
    <div>弹框</div>
    </div>
  2. 先写覆盖显示窗口的遮罩层div.box,因为要在整个窗口显示固定,所以position要设为fixed,background设为灰色半透明,由于要遮住整个显示屏,width和height都设为100%(body和html的width和height也都设为100%);

  3. 在遮罩层的div.box里写弹框的div.container,位置相对于父级定位

代码实现

HTML:

 <div>
<button class="open">点我弹出弹框</button>
</div>
<div class="box">
<div class="container">
<button class="close">关闭弹框</button>
</div>
</div>
 

CSS:

 html, body {
width:100%;
height:100%;
}
.box {
display: none;
width: 100%;
height: 100%;
position: fixed;
top:;
left:;
background: rgba(51,51,51,0.5);
z-index:;//根据自己页面情况设置
}
.container {
width: 500px;
height: 200px;
position: absolute;
top: 50%;//以下3行设置弹框居中页面,根据自己页面情况选择
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
z-index:;//根据自己页面情况设置
}
 

JavaScript:

 $(document).ready(function(){
$(".open").click(function(){
$(".box").show();
})
$(".close").click(function(){
$(".box").hide();
})
})

原页面内容不可滚动弹框内容可滚动

效果展示

实现原理

  1. 弹框内容需要滚动一定是内容超出弹框的高度,所以要给弹框内想滚动的部分设overflow: auto;

  2. 然后是Jq实现部分。当弹框弹出时原页面内容不能滚动,即将body样式设为overflow: hidden;原页面的内容就不会动了;当弹框关闭后再将body样式改为默认的overflow: auto;

  3. 2中的JS写一个函数,再在弹框的JQ中调用函数。

代码实现

CSS:

.container中增加一条

 .container {
overflow: auto;
}
 

JavaScript:

 function toggleBody(isPopup) {
if (isPopup) {
document.body.style.height = '100%';
document.body.style.overflow = 'hidden';
}else {
document.body.style.height = 'auto';
document.body.style.overflow = 'auto';
}
}
 

JQ中需要增加调用toggleBody()函数

 $(".open").click(function(){
$(".box").show();
toggleBody(1);//增加部分
})
$(".close").click(fundtion(){
$(".box").hide();
toggleBody(0);//增加部分
})
 

*有错误的地方欢迎指正

*转载请注明出处

简单实现弹出弹框页面背景半透明灰,弹框内容可滚动原页面内容不可滚动的效果(JQuery)的更多相关文章

  1. android 弹出的软键盘遮挡住EditText文本框的解决方案

    1.android 弹出的软键盘遮挡住EditText文本框的解决方案: 把Activit对应的布局文件filename.xml文件里的控件用比重设置布局.(例如:android:layout_wei ...

  2. JS弹出层遮罩,隐藏背景页面滚动条细节优化

    做过弹层组件的童鞋应该都考虑过特殊情况下取消页面滚动条,让其不能滚动,这样用户体验会好很多,当弹层内容超出屏幕展现范围的时候在弹层上面增加滚动条来查看全部内容. 一.去除滚动条方法给body添加ove ...

  3. 页面单击按钮弹出modaldialog然后调用ajax处理程序获取数据,给父级页面控件赋值

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RefTopicList.asp ...

  4. Dynamics CRM2013 编辑视图时弹出尚未保存所做的更改警示框

    CRM2013中当对视图进行自定义编辑时,总会弹出如下图所示的警示框,一般我们都会选择离开此页来保存我们所做的更改,显而易见的是这又是CRM2013的一个bug 在UR2 for  Dynamics ...

  5. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  6. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  7. 弹出层layer演示 以及在编写弹出层时遇到的错误

    实现的功能: 首先第一步 在官方下载layer的文件.layUI官网:http://layer.layui.com/     http://layer.layui.com/ layer文件的下载步骤如 ...

  8. layui弹出层处理(获取、操作弹出层数据等)

    要点: 字符串被渲染为弹窗层之后,回自动转换为DOM,可以使用jq进行各种操作 <!DOCTYPE html> <html> <head> <meta cha ...

  9. ionic3 调用摄像头 当键盘弹出时候 出现摄像头 背景

    iOS 端毫无 bug,Android 端却出现了问题.当软键盘弹出后,Android 端的 tabs 移到了软键盘的上面,再仔细一看,整个界面都被压扁了,输入框也不知道去哪儿了. 于是去翻 Ioni ...

随机推荐

  1. DCOS实践分享(6):基于DCOS的大数据应用分享

    Open DC/OS大中华区官方发布会在京隆重召开   DCOS领域诞生了一个100%开源的企业级Datacenter Operating System版本,即DC/OS.Linker Network ...

  2. 【分享】【原创开源应用第4期】给ili9488,RA8875类显示屏的emWin底层增加DMA加速方案

    说明:1.emWin底层中最重要的一个优化就是16bpp绘制,特此为其增加DMA加速,已经支持RA8875和ili9488.2.使用中务必将emWin任务设置为除了空闲任务,统计任务以外的最低优先级, ...

  3. HTTP长连接和短连接 + Websocket

    HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问题,T ...

  4. S-CMS企建v3二次SQL注入

    S-CMS企建v3二次SQL注入 0x01 前言 继上一篇的S-CMS漏洞再来一波!首发T00ls 0x2 目录 Sql注入二次SQL注入 0x03 Sql注入 漏洞文件:\scms\bbs\bbs. ...

  5. JDK 8 之 Stream sorted() 示例

    原文链接:http://www.concretepage.com/java/jdk-8/java-8-stream-sorted-example 国外对Java8一系列总结的不错, 翻译过来给大家共享 ...

  6. [Swift]LeetCode124. 二叉树中的最大路径和 | Binary Tree Maximum Path Sum

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  7. [Swift]LeetCode322. 零钱兑换 | Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  8. [Swift]LeetCode918. 环形子数组的最大和 | Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  9. [Swift]LeetCode1012. 至少有 1 位重复的数字 | Numbers With 1 Repeated Digit

    Given a positive integer N, return the number of positive integers less than or equal to N that have ...

  10. [Swift]LeetCode1029. 两地调度 | Two City Scheduling

    There are 2N people a company is planning to interview. The cost of flying the i-th person to city A ...