【问题标题】:Modal is hidden from other divs模态对其他 div 隐藏
【发布时间】:2022-02-12 03:37:07
【问题描述】:

我对模态有一个小问题;它的一部分被右边的div隐藏了。

我有两个拆分,一个在显示表格的左侧,另一个在显示表单的右侧。

在左侧拆分还有一个按钮,“打开模式”,它允许我打开模式。

问题是,一旦模态框打开,它在左侧可见,而模态框最终在右侧拆分的部分被右侧拆分隐藏。

请参阅图以获得进一步说明。

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.split {
            height: 100%;
            position: fixed;
            z-index: 1;
            top: 30;
            overflow-x: hidden;
            padding-top: 20px;
        }

        .left {
            left: 0;
            width: 40%;
            background-color: whitesmoke;
        }

        .right {
            right: 0;
            width: 60%;
            background-color: ghostwhite;
        }

        .centered {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        .centered table {
            width: 300px;
        }

        table,
        th,
        td {
            border: 1px solid black;
        }

        .buttons {
            display: flex;
        }

        .buttons form {
            margin: 0 10px;
        }

        .part {
            width: 200px;
            padding: 5px;
            border: 1px solid gray;
            margin: 0;
            margin-bottom: 20px;
        }

        /* The Modal (background) */
        .modal {
            display: none;
            /* Hidden by default */
            position: fixed;
            /* Stay in place */
            z-index: 1;
            /* Sit on top */
            padding-top: 100px;
            /* Location of the box */
            left: 0;
            top: 0;
            width: 100%;
            /* Full width */
            height: 100%;
            /* Full height */
            overflow: auto;
            /* Enable scroll if needed */
            background-color: rgb(0, 0, 0);
            /* Fallback color */
            background-color: rgba(0, 0, 0, 0.4);
            /* Black w/ opacity */
        }

        /* Modal Content */
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }

        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }
<div class="title" id="title">
    </div>

    <div hr class="horiz">

        <div class="split left">
            <div class="centered">
                <div class="part">
                    <h3>Parti dell'articolo</h3>
                </div>
                <table id="my-table" width="90%">
                    <tr>
                        <th>Numero</th>
                        <th>Tipo</th>
                        <th>Seleziona</th>
                    </tr>

                </table>

                <br><br>

                <div class="buttons">
                    <form action="creaFrammento.html">
                        <input type="submit" value="Aggiungi parte articolo" />
                    </form>

                    <input type="button" value="Elimina parti articolo" onclick="confirmation()" />
                </div>

            </div>
            <div>
                <!-- Trigger/Open The Modal -->
                <button id="myBtn">Open Modal</button>

                <!-- The Modal -->
                <div id="myModal" class="modal">

                    <!-- Modal content -->
                    <div class="modal-content">
                        <span class="close">&times;</span>
                        
                        <div>
                            <h1 div class="title" id="title">
                        </div>

                        <div class="text-container">
                            <p class="txt1" id="txt1"> </p>
                        </div> <br>

                        <div class="img"> <img id="img" src onerror="this.onerror=null;" style="width:50%"> </div> <br>

                        <div class="text-container">
                            <p class="txt2" id="txt2"> </p>
                        </div> <br>

                        <div class="video"> <iframe id="idframe" width="600" height="400"> </iframe> </div> <br>

                    </div>

                </div>
            </div>
        </div>

        <div class="split right">
            <div class="centered"> <br>
                <div class="form">
                    <form method="post" id="save" action="javascript:myFunction()" class="formEdit">
                        <h1>Modifica frammento</h1>
                        <div class="field">
                            <label for="id">ID :</label>
                            <input type="number" id="id" name="id" />
                        </div>
                        <div class="field">
                            <label for="id_a">ID Articolo:</label>
                            <input type="text" id="id_a" name="id_a" />
                        </div>
                        <div class="field">
                            <label for="numero">Numero Frammento:</label>
                            <input type="number" id="numero" name="numero" />
                        </div>
                        <div class="field">
                            <label for="tipo">Tipo Frammento:</label>
                            <select name="tipo" id="tipo" form="tipoform">
                                <option value="Titolo">Titolo</option>
                                <option value="Testo">Testo</option>
                                <option value="Immagine">Immagine</option>
                                <option value="Video youtube">Video youtube</option>
                            </select>
                        </div>
                        <div class="field">
                            <label for="valore">Valore Frammento:</label>
                            <textarea id="valore" name="valore" rows="5" cols="30"> </textarea>
                        </div>
                        <div class="button">
                            <button type="submit" id="save-btn" class="full">Salva modifiche</button>
                            <input type="button" id="delete-btn" value="Annulla modifiche"
                                
                        </div>
                    </form>
                </div>
            </div>
        </div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    z-index 值较高的元素出现在z-index 值较低的元素之上。

    您需要从.left div 中取出包裹.modaldiv。还将z-index 添加到.modal,如下所示:

    .modal {
       z-index: 999;
    }
    
    <div hr="" class="horiz">
    
            <div>
                    <!-- Trigger/Open The Modal -->
                    <button id="myBtn">Open Modal</button>
    
                    <!-- The Modal -->
                    <div id="myModal" class="modal" style="display: block;Z-INDEX: 100;">
    
                        <!-- Modal content -->
                        <div class="modal-content">
                            <span class="close">×</span>
                            
                            <div>
                                <h1 div="" class="title" id="title">
                            </h1></div>
    
                            <div class="text-container">
                                <p class="txt1" id="txt1"> </p>
                            </div> <br>
    
                            <div class="img"> <img id="img" src="" onerror="this.onerror=null;" style="width:50%"> </div> <br>
    
                            <div class="text-container">
                                <p class="txt2" id="txt2"> </p>
                            </div> <br>
    
                            <div class="video"> <iframe id="idframe" width="600" height="400"> </iframe> </div> <br>
    
                        </div>
    
                    </div>
                </div><div class="split left">
                <div class="centered">
                    <div class="part">
                        <h3>Parti dell'articolo</h3>
                    </div>
                    <table id="my-table" width="90%">
                        <tbody><tr>
                            <th>Numero</th>
                            <th>Tipo</th>
                            <th>Seleziona</th>
                        </tr>
    
                    </tbody></table>
    
                    <br><br>
    
                    <div class="buttons">
                        <form action="creaFrammento.html">
                            <input type="submit" value="Aggiungi parte articolo">
                        </form>
    
                        <input type="button" value="Elimina parti articolo" onclick="confirmation()">
                    </div>
    
                </div>
                
            </div>
    
            <div class="split right">
                <div class="centered"> <br>
                    <div class="form">
                        <form method="post" id="save" action="javascript:myFunction()" class="formEdit">
                            <h1>Modifica frammento</h1>
                            <div class="field">
                                <label for="id">ID :</label>
                                <input type="number" id="id" name="id">
                            </div>
                            <div class="field">
                                <label for="id_a">ID Articolo:</label>
                                <input type="text" id="id_a" name="id_a">
                            </div>
                            <div class="field">
                                <label for="numero">Numero Frammento:</label>
                                <input type="number" id="numero" name="numero">
                            </div>
                            <div class="field">
                                <label for="tipo">Tipo Frammento:</label>
                                <select name="tipo" id="tipo" form="tipoform">
                                    <option value="Titolo">Titolo</option>
                                    <option value="Testo">Testo</option>
                                    <option value="Immagine">Immagine</option>
                                    <option value="Video youtube">Video youtube</option>
                                </select>
                            </div>
                            <div class="field">
                                <label for="valore">Valore Frammento:</label>
                                <textarea id="valore" name="valore" rows="5" cols="30"> </textarea>
                            </div>
                            <div class="button">
                                <button type="submit" id="save-btn" class="full">Salva modifiche</button>
                                <input type="button" id="delete-btn" value="Annulla modifiche" <="" div="">
                        
                    </div></form>
                </div>
            </div>
       </div>
    </div>
    

    【讨论】:

    • 我试过了,还是不行
    • 好的,请问您可以为您的代码创建一个可运行的 sn-p 吗?以便我们更好地帮助您。
    • 试试,我改了
    • 我正在尝试。我在 -1 处尝试了正确的 z-index,在 1 处尝试了模态 z-index。所以它可以工作。
    【解决方案2】:

    请将模态的位置从固定更改为相对。

    .modal {
                display: none;
                /* Hidden by default */
                position: relative;
    ..... rest keep the same only changed position to relative
    }
    

    【讨论】:

    • 不明白对html做了什么改动
    • 我以为您只需要将模态框放在右侧。如果现在请不要更改 html。让我知道这是否是您想要的,我可以更改我的答案以不更改 HTML。
    • 不,我的问题是右边的分割覆盖了同样分割的模态
    • 编辑了回复。如果这解决了您的问题,请将其标记为答案。
    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多