鼠标在javascript中输出时模态消失

问题描述 投票:1回答:1

当鼠标悬停在按钮上时,模态应该出现。当我尝试选择模态内的任何值时,模态立即消失。

 <button class="modalShow" onMouseOver={this.showModal} onMouseOut={this.closeModal}  >
          Show Modal
          </button>

            <div class="modal modalView"  id="myModal"   >
            <div class="modalDialog modalClass">
                <div class="modalContent">
                    <div class="header">
                        <ul class="drop">  
                            <li>
                              <input type="radio" id="first" name="first" value="1"   />
                              <label>first</label>
                            </li>
                             <li>
                              <input type="radio" id="second" name="second" value="2"   />
                              <label>second</label>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            </div>

     showModal= event => {    
        document.getElementById("myModal").style.display = "block"; 

        /* logic***/
    }

关闭鼠标按钮时关闭模态功能。

closeModal= event => {  
        document.getElementById("myModal").style.display  = "none";  
    }
javascript modal-dialog
1个回答
0
投票

你的问题不明确。所以有人不明白你问的是什么。但我也为你准备了一个解决方案。可能这个样本对你来说已经足够了。

此代码有2个选项:

  • 按下按钮打开模态。
  • 当选择模态上的任何位置时,模态将关闭。 你可以检查一下:

// 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";
}

function closeModal() {

  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";
  }
}
      body {
        font-family: Arial, Helvetica, sans-serif;
      }

      /* 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;
      }
<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
 
  </head>

  <body>

    <h2>Modal Example</h2>

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

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

      <!-- Modal content -->
      <div onClick="closeModal()" class="modal-content">
        <span class="close">&times;</span>
        <p>Some text in the Modal..</p>
      </div>

    </div>



  </body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.