在javascript中将鼠标移出时,模态消失

时间:2019-03-12 04:28:19

标签: javascript modal-dialog

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

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

1 个答案:

答案 0 :(得分:0)

您的问题尚不清楚,因此任何人都不了解您的要求。但是即使如此,我也为您准备了解决方案。也许此示例对您来说足够了。

此代码有2个选项:

  • 按下按钮即可打开模态。
  • ,并且当在modal上选择任意位置时,modal将关闭。

    您可以检查它:

// 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>