JSFiddle - React, Tailwind, and code Playground

by 81ue_lan

HTML

<button onclick="toastSuccess()">成功訂購商品</button>
<hr>
<button onclick="toastError()">訂購商品失敗</button>

<div class="toast toast-success">
  <p>訂購成功!可以查看 Email 確認訂單細節!</p>
</div>
<div class="toast toast-error">
  <p>抱歉,訂購失敗!請稍後重新嘗試</p>
</div>

CSS

.toast{
  display: none;
  position: relative;
  box-sizing: border-box;
  width: 345px;
  padding: 5px 3px;
  border: 1px solid;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  text-align: center;
  overflow: hidden;
}
.toast::before{
  content: '';
  position: absolute;
  width: 10px;
  height: 100%;
  top: 0;
  left: 0;
}
.toast-success::before{
    background-color: #83C786;
}
.toast-error::before{
    background-color: #FE8963;
}
.toast-success{
   border-color: #CAE9D1;
   background-color: #E9F6ED;
}
.toast-error{
  border-color: #EACFC8;
  background-color: #FCEDE9;
}

JavaScript

const successToast = document.querySelector('.toast-success');

const ErrorToast = document.querySelector('.toast-error');

function toastSuccess(){
		successToast.style.display = "block";
    
   setTimeout(() => {
      successToast.style.display = "none";
    },5000)
}

function toastError(){
		ErrorToast.style.display = "block";
    
   setTimeout(() => {
      ErrorToast.style.display = "none";
    },5000)
}