本文實例為大家分享了js實現彈框效果的具體代碼,供大家參考,具體內容如下
利用display來控制彈窗的現實和隱藏
1
2
3
4
5
6
7
8
9
10
|
<!-- 彈出層 --> < div id = "popLayer" ></ div > <!--黑色蒙版 --> < div id = "popBox" > < div class = "close" > X </ div > < div > <!-- 內容 --> </ div > </ div > |
js:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//點擊關閉按鈕 var close = document.querySelector( ".close" ) close.onclick = function () { console.log( "點擊" ) var popBox = document.getElementById( "popBox" ); var popLayer = document.getElementById( "popLayer" ); popBox.style.display = "none" ; popLayer.style.display = "none" ; } //需要顯示時調用 var popLayer = document.getElementById( "popLayer" ); popBox.style.display = "block" ; popLayer.style.display = "block" ; |
CSS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/* 彈出層 */ #popLayer { display : none ; background-color : #000 ; position : absolute ; top : 0 ; right : 0 ; bottom : 0 ; left : 0 ; z-index : 10 ; opacity: 0.6 ; } /*彈出層*/ #popBox { display : none ; background-color : #FFFFFF ; z-index : 11 ; width : 220px ; height : 300px ; position : fixed ; top : 0 ; right : 0 ; left : 0 ; bottom : 0 ; margin : auto ; } /*關閉按鈕*/ #popBox .close { width : 20px ; height : 20px ; border-radius: 50% ; position : absolute ; border : 1px solid #fff ; color : #fff ; text-align : center ; line-height : 20px ; right : 8px ; top : 8px ; z-index : 50 ; } #popBox .close a { text-decoration : none ; color : #2D2C3B ; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/youngKing0615/article/details/114867921