欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

原生JS结合CSS还有HTML实现页面弹框(代码教程)

程序员文章站 2023-10-29 14:37:22
原生js结合css还有html实现页面弹框(代码教程)

原生js结合css还有html实现页面弹框(代码教程)

<!doctype html>
 <html>
 <head>
 <meta charset=""/>
 <title>opacity</title>
 <style>
 *{
     padding: 0;
     margin: 0;
}
body{
    padding: 0px;
    background: url() 0 0 no-repeat;
    background-size: cover;
}

.demo{
    width: 100%;
    height: 100%;
    position: relative;
}
.demo-bg{
    position: absolute;
    left: 0;
    top: 0;
    z-index: 0;
    width: 100%;
    height: 671px;
    /*filter:alpha(opacity=50);*/
    background-color: rgba(0,0,0,0.4); /*实现透明背景*/
    display: none;
}
#button{
    width: 70px;
    height: 30px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    background: yellow;
    margin: 20px 0 0 40px;
    position: relative;
    border: 0;
    box-shadow:  2px 2px 10px red;
    -webkit-box-shadow:  2px 2px 10px red;
    -moz-box-shadow:  2px 2px 10px red;
}
.demo-txt{
    position: relative;
    z-index: 1;
    color: #000;
    background: white;
    width: 30%;
    height: 200px;
    margin:auto;
    padding: auto;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    display: none;
    padding: 10px;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
}
#btn{
    float: right;
}
</style>
</head>
<body>   

<p class='demo'>
    <button id="button" onclick="opendialog()">打开弹窗</button>
    <p class='demo-bg'></p>
    <p class='demo-txt'>
        <button id="btn" onclick="closedialog()">关闭弹窗</button>
    </p>
</p>
<script>
    var demobg = document.queryselector(".demo-bg");
    var demotxt = document.queryselector(".demo-txt");
    function opendialog() {
        demotxt.style.display = "block";
        demobg.style.display = "block";
    }

    function closedialog() {
        demotxt.style.display = "none";
        demobg.style.display = "none";
    }
</script>
 </body>
</html>