Issue
I'm building a version 2 of my own website and I'm trying to make an popup slide-out animation. The problem is that I have tried making one but I always mess up and nothing makes it work. I did an slide-in animation which works but now I need an slide-out animation.
How do I make an slide-out popup animation for my popup. Would appreciate if someone would help!
Code can be seen down below:
function openPopup() {
  document.getElementById("popup").style.display = "block";
}
function closePopup() {
  document.getElementById("popup").style.display = "none";
}
.popup {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  align-items: center;
  justify-content: center;
  z-index: 9999;
}
.popup-content {
  background-color: #ffffff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
  text-align: center;
  width: 400px;
  height: 400px;
  position: relative;
  right: -600px;
  top: 200px;
  animation: popUPAnimation 1s forwards;
}
@keyframes popUPAnimation {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}
.popup-content h1 {
  color: #333333;
  font-size: 2.5rem;
  text-align: center;
  text-decoration: underline;
}
.popup-content h2 {
  color: #333333;
  font-size: 1.3rem;
  text-align: center;
  margin-top: 10px;
}
.solid-popup {
  position: absolute;
  border-top: 1px solid #bbb;
  width: 70%;
  margin-top: 20px;
  margin-left: 40px;
}
.popup-content button {
  background: #EEEEEE;
  border: none;
  border-radius: 5px;
  padding: 5px 10px;
  margin: 10px;
  position: relative;
  top: 30px;
  cursor: pointer;
  transition: all .45s ease;
}
.popup-content button:hover {
  background: #dedede;
}
.popup-close {
  position: absolute;
  top: -10px;
  right: 5px;
  font-size: 3rem;
  cursor: pointer;
  color: #333333;
  transition: all .45s ease;
}
.popup-close:hover {
  transform: scale(1.1);
  color: #0f0f0f;
}
<button onclick="openPopup()">Open popup</button>
<div id="popup" class="popup">
  <div class="popup-content">
    <span class="popup-close" onclick="closePopup()">×</span>
    <h1>Sort products</h1>
    <h2>Categories</h2>
    <hr class="solid-popup">
    <div class="popup-buttons">
      <div>
        <button onclick="window.location.href='#';">General Websites</button>
      </div>
      <div>
        <button onclick="window.location.href='#';">Fivem Websites</button>
      </div>
      <div>
        <button onclick="window.location.href='#';">Custom made websites</button>
      </div>
      <div>
        <button onclick="window.location.href='#';">Ecommerce Websites</button>
      </div>
      <div>
        <button onclick="window.location.href='#';">All Websites</button>
      </div>
    </div>
  </div>
</div>
Solution
HTML5 has evolved, you should use <dialog> elements,
it saves a lot of code.
const
  btShowDial  = document.querySelector('#bt-show_mDialog')
, dialogBox   = document.querySelector('#dialog-box')
, btCloseDial = dialogBox.querySelector('b.popup-close')
  ;
btShowDial.onclick =_=> { dialogBox.showModal() }
  ;
btCloseDial.onclick =_=>
  { 
  dialogBox.classList.add('close'); 
  setTimeout(()=>
    {
    dialogBox.close()
    dialogBox.classList.remove('close'); 
    }, 1500);
  };
dialog[open] {
  animation: popUPAnim 700ms forwards;
  }
dialog.close {
  animation: popOutAnim 700ms forwards;
  }
@keyframes popUPAnim {
    0% { transform : scale(0);   }
   80% { transform : scale(1.1); }
  100% { transform : scale(1);   }
}
@keyframes popOutAnim{
    0% { transform : scale(1);   }
   20% { transform : scale(1.1); }
  100% { transform : scale(0);   }
}
dialog  {
  position      : relative;
  border        : 1px solid black;
  padding       : 0;
  border-radius : 5px;
  text-align    : center;
  min-width     : 24em;
  }
dialog::backdrop {
  background    : #0af3cc5d;
  }
dialog > b.popup-close {
  position    : absolute;
  top         : -12px;
  right       : 1px;
  font-size   : 3rem;
  cursor      : pointer;
  color       : #333333;
  transition  : all .45s ease;
  caret-color : transparent;
  }
dialog > b.popup-close:hover {
  transform : scale(1.1);
  color     : #0f0f0f;
  }
#dialog-box button {
  display : block;
  margin  : 0.5em auto;
  }
<button id="bt-show_mDialog">show modal dialog..</button>
<dialog id="dialog-box">
  <b class="popup-close">×</b>
  <h1>Sort products</h1>
  <h3>Categories</h3>
  <hr>
  <button> General Websites </button>
  <button> Fivem Websites </button>
  <button> Custom made websites </button>
  <button> Ecommerce Websites </button>
  <button>All Websites</button>
</dialog>
for a full compatibility with your code, the same with :
    <button data-link="#a"> General Websites     </button>
    <button data-link="#b"> Fivem Websites       </button>
    <button data-link="#c"> Custom made websites </button>
    <button data-link="#d"> Ecommerce Websites   </button>
    <button data-link="#e"> All Websites         </button>
BUT : following hash-links may cause some glitch on "close" event,
so you need to use animationend event
const
  btShowDial  = document.querySelector('#bt-show_mDialog')
, dialogBox   = document.querySelector('#dialog-box')
, closeEnding = { folowLink : '' }
  ;
btShowDial.onclick =_=> 
  { 
  dialogBox.showModal() 
  };
  
dialogBox.addEventListener('click', ({target}) =>  // event delegation
  {
  if (!target.matches('dialog > b.popup-close, button[data-link]')) 
    return
  closeEnding.folowLink = target.dataset.link || '';    
  dialogBox.classList.add('close'); 
  });
  
dialogBox.addEventListener('animationend', () => //...
  {
  if (dialogBox.classList.contains('close'))
    {
    dialogBox.close();
    dialogBox.classList.remove('close');
    if (!!closeEnding.folowLink)
      window.location.href = closeEnding.folowLink;
      
    closeEnding.folowLink = '';
    }
  });
dialog[open] {
  animation : popUPAnim 700ms forwards;
  }
dialog.close {
  animation: popOutAnim 700ms forwards;
  }
@keyframes popUPAnim {
    0% { transform : scale(0);   }
   80% { transform : scale(1.1); }
  100% { transform : scale(1);   }
}
@keyframes popOutAnim{
    0% { transform : scale(1);   }
   20% { transform : scale(1.1); }
  100% { transform : scale(0);   }
}
dialog::backdrop {
  background    : #0af3cc5d;
  }
#dialog-box {
  position      : relative;
  border        : 1px solid black;
  padding       : 0;
  border-radius : 5px;
  text-align    : center;
  min-width     : 24em;
  }
dialog > b.popup-close {
  position    : absolute;
  top         : -12px;
  right       : 1px;
  font-size   : 3rem;
  cursor      : pointer;
  color       : #333333;
  transition  : all 200ms ease;
  caret-color : transparent;
  }
dialog > b.popup-close:hover {
  transform : scale(1.1);
  color     : #0f0f0f;
  }
#dialog-box button {
  display : block;
  margin  : 0.5em auto;
  width   : 14em;
  }
<button id="bt-show_mDialog">show modal dialog..</button>
<dialog id="dialog-box">
  <b class="popup-close" >×</b>
  <h1>Sort products</h1>
  <h3>Categories</h3>
  <hr>
  <button data-link="#a"> General Websites     </button>
  <button data-link="#b"> Fivem Websites       </button>
  <button data-link="#c"> Custom made websites </button>
  <button data-link="#d"> Ecommerce Websites   </button>
  <button data-link="#e"> All Websites         </button>
</dialog>
Answered By - Mister Jojo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.