【问题标题】:html Modal popuphtml 模态弹出窗口
【发布时间】:2010-01-02 17:11:02
【问题描述】:

如何为以下代码制作一个简单的模式弹出窗口。点击背景时,模式弹出窗口不应该消失。

<html>
<input type="textarea"></input>
</html>

【问题讨论】:

    标签: html popup modal-dialog


    【解决方案1】:

    这是一个纯 JavaScript 示例:

    var modal = document.getElementById('modal');
    var shade = document.getElementById('shade');
    document.getElementById('start').onclick = function() {
      modal.style.display = shade.style.display = 'block';
    };
    document.getElementById('close').onclick = function() {
      modal.style.display = shade.style.display = 'none';
    };
    
    // This code is a workaround for IE6's lack of support for the
    // position: fixed style.
    //
    if (!('maxHeight' in document.body.style)) {
      function modalsize() {
        var top = document.documentElement.scrollTop;
        var winsize = document.documentElement.offsetHeight;
        var docsize = document.documentElement.scrollHeight;
        shade.style.height = Math.max(winsize, docsize) + 'px';
        modal.style.top = top + Math.floor(winsize / 3) + 'px';
      };
      modal.style.position = shade.style.position = 'absolute';
      window.onscroll = window.onresize = modalsize;
      modalsize();
    }
    body {
      margin: 0;
    }
    
    #shade,
    #modal {
      display: none;
    }
    
    #shade {
      position: fixed;
      z-index: 100;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
    
    #modal {
      position: fixed;
      z-index: 101;
      top: 33%;
      left: 25%;
      width: 50%;
    }
    
    #shade {
      background: silver;
      opacity: 0.5;
      filter: alpha(opacity=50);
    }
    <div id="shade"></div>
    <div id="modal">
      <textarea rows="5" cols="25"></textarea>
      <button id="close">Close</button>
    </div>
    
    <p>
      <button id="start">Start</button>
    </p>

    您可以从那里进行各种改进,例如修复 IE z-indexing 的 iframe hack,或将其封装在可重复使用的对象中,但这是完成的基本方式。

    【讨论】:

    • @bobince.. 您好先生,我在我的应用程序中使用了这个示例,我的应用程序包含视口和一个背景图像。每当我单击开始按钮时,不应该有滚动,并且模态应该在滚动后位于中心,模态也应该在中心。但我只是在顶部获得模态......
    • 太棒了,终于看到了非 jquery 的答案,我冒昧地将您的代码放入 javascript 对象中,jsfiddle.net/orwellophile/yWMdv
    【解决方案2】:

    您也可以使用原生 HTML5.1 dailog。 目前仅 Chrome 37+、Safari 6+ 和 Opera 24+ 支持对话框元素。

    var dailog = document.getElementById("dialog"); 
    
    function openModal() { 
       // dailog.show(); 
          dailog.showModal();
    } 
    
    function closeModal() { 
        dailog.close(); 
    } 
    #dialog{width:300px;}
    .right{float:right}
    <button onclick="openModal()">Show dialog</button>
    
    
    <dialog id="dialog">This is a dialog window<br/><br/><br/>
    <button onclick="closeModal()" class="right">Close</button>
    </dialog>

    【讨论】:

      【解决方案3】:

      jQueryUI 有一个模态对话框插件。它不会按照您的要求通过单击背景来释放控制:http://jqueryui.com/demos/dialog/#modal

      <a href="#" class="showModal">Show Modal Box</a>
      <div id="modalContents" style="display:none;">
        <textarea>Hello World</textarea>
      </div>
      

      --

      $(".showModal").click(function(e){
        e.preventDefault();
        $("#modalContents").dialog({bgiframe: true, height: 140, modal: true});
      });
      

      【讨论】:

      • 注意:bgiframe 是另一个用于规避 IE6 的一些表单渲染问题的插件。
      【解决方案4】:

      根据定义,模态窗口需要用户执行操作才能返回到主窗口。所以你要找的不是模态,而是一个窗口。

      【讨论】:

      • 他需要一个文本区域在模式 - 弹出
      【解决方案5】:

      chrome 上的 html5 具有 &lt;dialog&gt; 元素。用它做实验

      【讨论】:

        【解决方案6】:

        我认为下面的代码会帮助你

        <!DOCTYPE html>
        <html>
        <title>login modal</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
        <body>
        
        <div class="w3-container">
          <h2>Login Modal</h2>
          <button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-green w3-large">Login</button>
        
          <div id="id01" class="w3-modal">
            <div class="w3-modal-content w3-card-4 w3-animate-zoom" style="max-width:600px">
        
              <div class="w3-center"><br>
                <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-xlarge w3-hover-red w3-display-topright" title="Close Modal">&times;</span>
                
              </div>
        
              <form class="w3-container" action="/action_page.php">
                <div class="w3-section">
                  <label><b>Username</b></label>
                  <input class="w3-input w3-border w3-margin-bottom" type="text" placeholder="Enter Username" name="usrname" required>
                  <label><b>Password</b></label>
                  <input class="w3-input w3-border" type="password" placeholder="Enter Password" name="psw" required>
                  <button class="w3-button w3-block w3-green w3-section w3-padding" type="submit">Login</button>
                  <input class="w3-check w3-margin-top" type="checkbox" checked="checked"> Remember me
                </div>
              </form>
        
              <div class="w3-container w3-border-top w3-padding-16 w3-light-grey">
                <button onclick="document.getElementById('id01').style.display='none'" type="button" class="w3-button w3-red">Cancel</button>
                <span class="w3-right w3-padding w3-hide-small">Forgot <a href="#">password?</a></span>
              </div>
        
            </div>
          </div>
        </div>
                    
        </body>
        </html>

        【讨论】:

        • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
        猜你喜欢
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多