html点击出现表单弹窗

15,591次阅读
没有评论

共计 2855 个字符,预计需要花费 8 分钟才能阅读完成。

html 点击出现表单弹窗

  • 项目场景:
  • 源码展示:
  • 全部源码展示:

内含全部源码

项目场景:

在系统中,点击某个按钮如何弹出一个 表单弹窗 并且设置 遮罩层,如下图所示。
html 点击出现表单弹窗

在上图中,我们实现了点击按钮跳出弹窗的效果,主要用到的是 css 中 position固定定位 和利用 js 改变 display 的值来进行显示和隐藏页面。
编译器:idea


源码展示:

首先讲到我们的 遮罩层(灰色部分),它是由点击事件来进行触发
css

 
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            display: none; 
        }

html

div class="overlay" id="overlay">/div>

可以看到我们设置的 display 是默认隐藏的,那么通过点击事件来改变它原有的 display 就可以做到显示和隐藏效果。
弹窗效果
css

 .popup {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            display: none; 
        }

html

div class="popup" id="popup">
  label>序号:/label>
    input type="text" value="" placeholder=" 请输入 ID"/>br>br>
    label>名称:/label>
    input type="text" value="" placeholder=" 请输入名称 " />br>br>
    label>类型/label>
    select>
        option>1/option>
        option>2/option>
    /select>br>br>
    button id="closebtn()">关闭/button>
    button type="submit">提交/button>/div>

上面的效果也是通过 display 来进行改变样式。

表单效果
css

 label {
            width: 100px;
            margin-bottom: 10px;
        }

        input[type="text"], select {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
            color: #555;
        }

        select option {
            color: #555;
        }
        button {
            padding: 5px 10px;
            border-radius: 3px;
            border: none;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        button:hover {
            background-color: #0069d9;
        }

html

label>序号:/label>
    input type="text" value="" placeholder=" 请输入 ID"/>br>br>
    label>名称:/label>
    input type="text" value="" placeholder=" 请输入名称 " />br>br>
    label>类型/label>
    select>
        option>1/option>
        option>2/option>
    /select>br>br>
    button id="closebtn()">关闭/button>
    button type="submit">提交/button>

全部源码展示:

!DOCTYPE html>
html lang="en">
head>
    meta charset="UTF-8">
    title>表单弹窗/title>
    style>
        
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            display: none; 
        }

        
        .popup {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            display: none; 
        }
        label {
            width: 100px;
            margin-bottom: 10px;
        }

        input[type="text"], select {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
            color: #555;
        }

        select option {
            color: #555;
        }
        button {
            padding: 5px 10px;
            border-radius: 3px;
            border: none;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        button:hover {
            background-color: #0069d9;
        }

    /style>
/head>
body>
button id="openbtn">添加/button>
div class="overlay" id="overlay">/div>
div class="popup" id="popup">
form>
    label>
        序号:/label>
    input type="text" value="" placeholder=" 请输入 ID"/>br>br>
    label>名称:/label>
    input type="text" value="" placeholder=" 请输入名称 " />br>br>
    label>类型/label>
    select>
        option>1/option>
        option>2/option>
    /select>br>br>
    button id="closebtn()">关闭/button>
    button type="submit">提交/button>
/form>
/div>
script>
    const openbtn=document.getElementById('openbtn');
    const closebtn=document.getElementById('closebtn');
    const popup=document.getElementById('popup');
    const overlay=document.getElementById('overlay');
    openbtn.addEventListener('click',function(){
        popup.style.display='block';
        overlay.style.display='block';
    })
/script>
/body>
/html>

原文地址: html 点击出现表单弹窗

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-29发表,共计2855字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)