Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Welcome modal only disappears partially

I have a modal window opening when the page loads.

It opens well, however, when I close it, it does not disappear completely, as the grey body-background stays, plus the link in the back become disabled.

I must have got something wrong in the code.

$(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>

Basically, when I click anywhere, I would like the modal to disappear completely and leave room to everything else behind it. Right now, it is not the case.

Please advise

like image 735
mattvent Avatar asked Dec 02 '25 20:12

mattvent


2 Answers

You need to make the grey body-background disappear too.

According to your current code, there is one solution using purely CSS.

Because you an animation, display: none will not satisfy your need. Therefore, setting z-index to -1 to #modal-container.four can solve your problem.

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

You can twist it around yourself to suit your implementation.

Working code below:

$(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>
like image 196
yqlim Avatar answered Dec 05 '25 10:12

yqlim


Add display:none to .out will solve the issue, as you are playing with opacity which will hide but the display area still occupied! Check below snippet for reference.

Updated:

 #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>
like image 22
RaJesh RiJo Avatar answered Dec 05 '25 10:12

RaJesh RiJo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!