怎么实现模态窗口

html-css016

怎么实现模态窗口,第1张

用CSS和JS改变z-index的属性值就可以实现模态框

[html] view plain copy

<html>

<head>

<title>modal box</title>

<style type="text/css">

*{

margin:0px

padding:0px

}

</style>

</head>

<body>

<!--先在CSS里面把zindex的值设为负值让其在背景图片后面-->

<div style="background:url(http://pic.90sjimg.com/back_pic/00/04/27/49/5b1eee8bdba7b9aefc62fccafe72737c.jpg)width:100%height:800pxz-index:1">

<button id="modal-box">弹框</button>

<div id="modal-show" style="position:fixedz-index:-9999width:100%height:100%background-color:rgba(236, 214, 214, 0.2)">

</div>

<div id="modal-show2" style="position:fixedz-index:-9999width:30%height:30%margin:200px autoborder:1px solid red">

欢迎你登录

</div>

</div>

<script type="text/javascript">

document.getElementById("modal-box").onclick=function()//点击按钮改变zIndex的值为1让模态框在背景图的前面

{

document.getElementById("modal-show").style.zIndex = "1"

document.getElementById("modal-show2").style.zIndex = "1"

}

document.getElementById("modal-show").onclick=function()//点击模态框的透明背景时,模态框的zIndex值变为-9999,会隐藏在

{<span style="white-space:pre"></span>背景图片的后面,点击模态框本身是不会消失的<span style="white-space:pre">

</span>

this.style.zIndex = "-9999"

document.getElementById("modal-show2").style.zIndex = "-9999"

}

</script>

</body>

</html>

CSS:Cascading Style sheet层叠样式表或级联样式表,是一种样式设置规则,用于控制页面的外观的样式。使用CSS能够实现内容与样式的分离、方便团队开发,有助于样式复用、便于网站后期维护,实现页面的精准控制、让页面更精美。

CSS代码书写方式分为三种:嵌入式、外链式、行内式。

嵌入式:将CSS代码嵌入到HTML文件中,嵌入式是通过HTML中的

外链式:外链式是指单独写一个以.css为扩展名的文件,然后在标签中使用标签,将这个css文件链接到html文件中。(注意:css文件不能单独的运行,它必须要依赖于HTML文件)

语法规则:

<link rel="stylesheet" href="css文件的地址">

行内式:将CSS代码书写在HTML标签的style属性中。style是一个通用属性,每一个标签里面都拥有这个属性!

语法格式:

<标签名 style=”属性:值属性:值”>

初学CSS,你需要掌握这些使用技巧:

1、负边距的效果。注意左右负边距表现并不一致,左为负时是左移,右为负时是左拉。

2、BFC应用汇总:阻止外边距合并(margin collapsing)、消除浮动的影响。

3、flex布局:当flex-grow之和小于1时,只能按比例分配部分剩余空间,而不是全部。

4、并不是给元素设置display:block就会自动填充父元素宽度。input 就是个例外,其默认宽度取决于size特性的值。

5、对定位和固定定位时,同时设置left和right等同于隐式地设置宽度。

6、position:sticky,粘性定位要起作用需要设置最后滞留位置。chrome有bug,firefox完美。

7、要使模态框背景透明,用rgba是一种简单方式。

8、display:table实现多列等高布局。css实现定宽高比的原理:padding的百分比是相对于其包含块的宽度,而不是高度。

9、background-position百分比的正确理解方式:图片自身的百分比位置与容器同样的百分比位置重合。

10、background-repeat新属性值:round和space。前者表示凑个整,后者表示留点缝。

11、background-attachment指定背景如何附着在容器上,注意其属性值local和fixed的使用。

12、可以使用outline来描边,不占地方,它甚至可以在里面。

13、浏览器默认显示tab为8个空格,tab-size可以指定空格长度。

14、图片在指定尺寸后,可以设置object-fit为contain或cover保持比例。

15、设置宽度为fill-available,可以使inline-block像block那样填充整个空间。

<div class="page show-modal">

    <div class="container">主要内容</div>

    <div class="modal">浮层</div>

</div>

假设有这样的HTML结构,那么让 .modal 绝对定位在 .container 上面,.container 设置 blur 即可。

.modal {

    display: none

    position: fixed

    left: 0

    top: 0

    right: 0

    bottom: 0

    background: rgba(255,255,255,.8)

}

.show-modal .modal {

    display: block

}

.show-modal .container {

    -webkit-filter: blur(4px)

}

演示:http://jsbin.com/nesewayosa/1/