javascript欢迎模式只会部分消失

时间:2017-11-06 12:29:06

标签: javascript html css

我在页面加载时打开了一个模态窗口。

它开得很好,然而,当我关闭它时,它不会完全消失,因为灰色的身体背景停留,加上后面的链接被禁用。

我必须在代码中出错。



$(document).ready(function (){
  $('#modal-container').removeAttr('class').addClass('four');
  $('body').addClass('modal-active');
})

$('#modal-container').click(function(){
  $(this).addClass('out');
});

html.modal-active, body.modal-active {
  overflow: hidden;
}

#modal-container {
  position: fixed;
  display: table;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  transform: scale(0);
  z-index: 5;
}

#modal-container.four {
  z-index: 4;
  transform: scale(1);
}
#modal-container.four .modal-background {
  background: rgba(0, 0, 0, 0.8);;
}
#modal-container.four .modal-background .modal {
  animation: blowUpModal 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
#modal-container.four + .content-formodal {
  z-index: 5;
  animation: blowUpContent 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
#modal-container.four.out .modal-background .modal {
  animation: blowUpModalTwo 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
#modal-container.four.out + .content-formodal {
  animation: blowUpContentTwo 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modal-container .modal-background {
  display: table-cell;
  background: rgba(0, 0, 0, 0.8);
  text-align: center;
  vertical-align: middle;
}
#modal-container .modal-background .modal {
  background: white;
  padding: 50px;
  display: inline-block;
  border-radius: 3px;
  font-weight: 300;
  position: relative;
}
#modal-container .modal-background .modal h2 {
  font-size: 25px;
  line-height: 25px;
  margin-bottom: 15px;
}
#modal-container .modal-background .modal p {
  font-size: 18px;
  line-height: 22px;
}
#modal-container .modal-background .modal .modal-svg {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  border-radius: 3px;
}
#modal-container .modal-background .modal .modal-svg rect {
  stroke: #fff;
  stroke-width: 2px;
  stroke-dasharray: 778;
  stroke-dashoffset: 778;
}


@keyframes blowUpContent {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  99.9% {
    transform: scale(2);
    opacity: 0;
  }
  100% {
    transform: scale(0);
  }
}
@keyframes blowUpContentTwo {
  0% {
    transform: scale(2);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
@keyframes blowUpModal {
  0% {
    transform: scale(0);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes blowUpModalTwo {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }

.linkclass {

  color: #283446;
  display: inline-block;
  font-size: 18px;
  text-transform: uppercase;
  font-family: 'geomanistregular';
  letter-spacing: 2.5px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body style="background-color: white">

<div id="modal-container">
  <div class="modal-background">
    <div class="modal">
      <h2>I'm a Modal</h2>
      <p>Hear me roar.</p>
      <svg class="modal-svg" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" preserveAspectRatio="none">
                <rect x="0" y="0" fill="none" width="226" height="162" rx="3" ry="3"></rect>
              </svg>
    </div>
  </div>
</div>

<div class="content-formodal">

 <a href="#"><p class="linkclass">LINK</p></a>   

</div> <!-- content-formodal -->

</body>
&#13;
&#13;
&#13;

基本上,当我点击任何地方时,我希望模态完全消失,并为其背后的其他一切留出空间。现在,事实并非如此。

请告知

3 个答案:

答案 0 :(得分:1)

你需要让灰色的身体背景消失。

根据您当前的代码,有一种使用纯CSS的解决方案。

因为你是动画,display: none无法满足你的需要。因此,将z-index设置为-1#modal-container.four可以解决您的问题。

#modal-container.out .modal-background {
  opacity: 0;
  transition: 0.7s;
}
#modal-container.four {
  z-index: -1;
}

您可以自己扭转它以适应您的实施。

以下工作代码:

$(document).ready(function (){
  $('#modal-container').removeAttr('class').addClass('four');
  $('body').addClass('modal-active');
})

$('#modal-container').click(function(){
  $(this).addClass('out');
});

$('a').click(function(){
    alert('link is clicked');
});
html.modal-active, body.modal-active {
  overflow: hidden;
}

#modal-container {
  position: fixed;
  display: table;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  transform: scale(0);
  z-index: 5;
}

/* ADDED THIS BLOCK */
#modal-container.out .modal-background {
  opacity: 0;
  transition: opacity 0.7s;
}

#modal-container.four {
  z-index: -1; /* CHANGED THIS FROM 4 TO -1 */
  transform: scale(1);
}
#modal-container.four .modal-background {
  background: rgba(0, 0, 0, 0.8);;
}
#modal-container.four .modal-background .modal {
  animation: blowUpModal 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
#modal-container.four + .content-formodal {
  z-index: 5;
  animation: blowUpContent 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
#modal-container.four.out .modal-background .modal {
  animation: blowUpModalTwo 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
#modal-container.four.out + .content-formodal {
  animation: blowUpContentTwo 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modal-container .modal-background {
  display: table-cell;
  background: rgba(0, 0, 0, 0.8);
  text-align: center;
  vertical-align: middle;
}
#modal-container .modal-background .modal {
  background: white;
  padding: 50px;
  display: inline-block;
  border-radius: 3px;
  font-weight: 300;
  position: relative;
}
#modal-container .modal-background .modal h2 {
  font-size: 25px;
  line-height: 25px;
  margin-bottom: 15px;
}
#modal-container .modal-background .modal p {
  font-size: 18px;
  line-height: 22px;
}
#modal-container .modal-background .modal .modal-svg {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  border-radius: 3px;
}
#modal-container .modal-background .modal .modal-svg rect {
  stroke: #fff;
  stroke-width: 2px;
  stroke-dasharray: 778;
  stroke-dashoffset: 778;
}


@keyframes blowUpContent {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  99.9% {
    transform: scale(2);
    opacity: 0;
  }
  100% {
    transform: scale(0);
  }
}
@keyframes blowUpContentTwo {
  0% {
    transform: scale(2);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
@keyframes blowUpModal {
  0% {
    transform: scale(0);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes blowUpModalTwo {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }

.linkclass {

  color: #283446;
  display: inline-block;
  font-size: 18px;
  text-transform: uppercase;
  font-family: 'geomanistregular';
  letter-spacing: 2.5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body style="background-color: white">

<div id="modal-container">
  <div class="modal-background">
    <div class="modal">
      <h2>I'm a Modal</h2>
      <p>Hear me roar.</p>
      <svg class="modal-svg" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" preserveAspectRatio="none">
                <rect x="0" y="0" fill="none" width="226" height="162" rx="3" ry="3"></rect>
              </svg>
    </div>
  </div>
</div>

<div class="content-formodal">

 <a href="#"><p class="linkclass">LINK</p></a>   

</div> <!-- content-formodal -->

</body>

答案 1 :(得分:1)

display:none添加到.out会解决问题,因为您正在玩opacity,这会隐藏但显示区域仍然占用!请查看下面的代码段以供参考。

<强>更新

 #modal-container.out{
   display: none;
 }

$(document).ready(function() {
  $('#modal-container').removeAttr('class').addClass('four');
  $('body').addClass('modal-active');
})

$('#modal-container').click(function() {
  $(this).addClass('out');
});
  html.modal-active,
body.modal-active {
  overflow: hidden;
}

#modal-container {
  position: fixed;
  display: table;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  transform: scale(0);
  z-index: 5;
}

#modal-container.out{
  display: none;
}

#modal-container.four {
  z-index: 4;
  transform: scale(1);
}

#modal-container.four .modal-background {
  background: rgba(0, 0, 0, 0.8);
  ;
}

#modal-container.four .modal-background .modal {
  animation: blowUpModal 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modal-container.four+.content-formodal {
  z-index: 5;
  animation: blowUpContent 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modal-container.four.out .modal-background .modal {
  animation: blowUpModalTwo 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modal-container.four.out+.content-formodal {
  animation: blowUpContentTwo 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}

#modal-container .modal-background {
  display: table-cell;
  background: rgba(0, 0, 0, 0.8);
  text-align: center;
  vertical-align: middle;
}

#modal-container .modal-background .modal {
  background: white;
  padding: 50px;
  display: inline-block;
  border-radius: 3px;
  font-weight: 300;
  position: relative;
}

#modal-container .modal-background .modal h2 {
  font-size: 25px;
  line-height: 25px;
  margin-bottom: 15px;
}

#modal-container .modal-background .modal p {
  font-size: 18px;
  line-height: 22px;
}

#modal-container .modal-background .modal .modal-svg {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  border-radius: 3px;
}

#modal-container .modal-background .modal .modal-svg rect {
  stroke: #fff;
  stroke-width: 2px;
  stroke-dasharray: 778;
  stroke-dashoffset: 778;
}

@keyframes blowUpContent {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  99.9% {
    transform: scale(2);
    opacity: 0;
  }
  100% {
    transform: scale(0);
  }
}

@keyframes blowUpContentTwo {
  0% {
    transform: scale(2);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes blowUpModal {
  0% {
    transform: scale(0);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes blowUpModalTwo {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
  .linkclass {
    color: #283446;
    display: inline-block;
    font-size: 18px;
    text-transform: uppercase;
    font-family: 'geomanistregular';
    letter-spacing: 2.5px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body style="background-color: white">

  <div id="modal-container">
    <div class="modal-background">
      <div class="modal">
        <h2>I'm a Modal</h2>
        <p>Hear me roar.</p>
        <svg class="modal-svg" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" preserveAspectRatio="none">
                <rect x="0" y="0" fill="none" width="226" height="162" rx="3" ry="3"></rect>
              </svg>
      </div>
    </div>
  </div>

  <div class="content-formodal">

    <a href="#">
      <p class="linkclass">LINK</p>
    </a>

  </div>
  <!-- content-formodal -->

</body>

答案 2 :(得分:0)

添加以下CSS以隐藏模态容器:

$("input[type='file'][name='answer_video']").change(function(){
    var id = $(this).attr("id");
    var thisVal = document.getElementById(id).files[0].name;
    var nameBox = '<div class="fileContainer"><span class="docFileName">'+thisVal+'</span><span class="glyphicon glyphicon-remove" onclick=removeAttachment("'+id+'")>X</span></div>';
    $(this).before(nameBox).hide();
});

function removeAttachment(id){
    $("#"+id).val("").show();
    $(".fileContainer").remove();
}