HTML `dialog` element: scroll content independently of background

前端 未结 3 721
南旧
南旧 2021-01-21 18:33

I am trying to use the dialog element.

When the dialog/modal is closed, the body should be scrollable.

When the dialog/modal is open, if it has larg

3条回答
  •  孤城傲影
    2021-01-21 18:41

    Simple solution is : Once the mnodel is displayed make a one more DIV as overlay which covers full screen, in that place css { pointer-events:none} and model will be placed on top of that. user can not click on body content other than model data.

    I have created sample: http://jsfiddle.net/z3sgvnox/

    
    
    
    

    CSS

    #container {
      height: 100vh;
      width: 100vw;
      position: fixed;
      top: 0;
      left: 0;
      background: #ccc;
    }
    
    #my-dialog {
      margin-top: 1rem;
      margin-bottom: 3rem;
      width: 50%;
      overflow-y: auto;
          max-height: 80%;
    }
    .hideScroll{
      overflow:hidden;
      pointer-events:none;
    }
    
    #my-dialog__content {
      display: flex;
      flex-direction: column;
      height: 200vh;
    }
    
    menu {
      width: 100%;
      padding: 0;
      margin: 0 auto;
    }
    
    #cancel-button {
      width: 100%
    }
    

    JS:

    (function() {
      var openBtn = document.getElementById('open-dialog');
      var myDialog = document.getElementById('my-dialog');
    var bodyData = document.getElementById('content-body');
      openBtn.addEventListener('click', function() {
        if (typeof myDialog.showModal === "function") {
          myDialog.showModal();
          bodyData.classList.add("hideScroll");
        } else {
          alert("Dialog API not supported by browser");
        }
      });
    
    })();
    

提交回复
热议问题