I am developing the web site, and I want to make the custom popup message box with responsive design in pc, tablet and mobile, following are the requirement:
The popup message box can execute in everywhere
The popup message box can include the Confirm/Cancel options or just a message
The layout of message box should include the button(s) for options at the bottom of box, Image Container for show the message type (such as success, warning, failed, etc.) by image, a message text after the image container and before the button.
The popup message can be closing by click the background overlay if just a message exclude any options
If just a message, the message can be set the timeout for auto close the message box.
please based on the above requirement to write all requirement code in HTML, CSS and JavaScript
Assistant
User
Is it possible to add the pop animation when the popup box appear and close?
Assistant
User
Can you provide the full version code with update?
Assistant
Certainly! Below is a complete implementation of a custom popup message box meeting all your requirements:
Runs anywhere (desktop, tablet, mobile).
Supports Confirm/Cancel options or just a message.
Call customPopup.open({...options}) with the following options:
Option
Type
Description
type
string
One of 'success', 'warning', 'error', 'info' or null for no icon
message
string (required)
The message text to display
confirmCancel
boolean
Whether to show Confirm/Cancel buttons (true/false)
confirmText
string
Confirm button text (default 'Confirm')
cancelText
string
Cancel button text (default 'Cancel')
onConfirm
function
Callback executed when clicking "Confirm"
onCancel
function
Callback executed when clicking "Cancel"
autoClose
number
For message-only popup: auto close timeout in milliseconds
If confirmCancel is false:
No buttons are shown.
Clicking outside the popup closes it.
If autoClose is set, popup closes automatically after given milliseconds.
If confirmCancel is true:
Confirm/Cancel buttons appear.
Clicking outside does NOT close popup, user must click buttons explicitly.
Accessibility
The popup uses ARIA role="dialog"+aria-modal="true".
Focus is moved to overlay for keyboard users.
ESC key can close message-only popups.
Feel free to integrate and adapt as needed! Let me know if you want me to add animations or other features.
Yes! You can definitely add a smooth pop animation on open and close by using CSS transitions or keyframe animations. I'll show you how to do that by modifying the CSS and a little bit of JavaScript to trigger animations correctly.
What We’ll do:
Add CSS keyframe animations for pop-in (scale from 0.8 to 1 and fade in).
Add CSS keyframe animations for pop-out (scale down to 0.8 and fade out).
When the popup opens, add a class that triggers the pop-in animation.
When closing, trigger the pop-out animation and wait for it to finish before hiding the element, ensuring smooth closing.
Changes needed:
Add CSS keyframe animations.
Modify .popup-box to animate on open/close.
Modify JS openPopup/closePopup to handle animation classes and delays.
Here’s the updated code snippets with animation integrated.
1. CSS - Add animations and modify .popup-box
css
/* Add this inside the <style> block in your CSS: */
@keyframes popup-pop-in {
0% {
opacity: 0;
transform: scale(0.8);
}
100% {
opacity: 1;
transform: scale(1);
}
}
@keyframes popup-pop-out {
0% {
opacity: 1;
transform: scale(1);
}
100% {
opacity: 0;
transform: scale(0.8);
}
}
/* Popup box base */
.popup-box {
background: #fff;
border-radius: 8px;
max-width: 400px;
width: 100%;
max-height: 90vh;
overflow: auto;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
display: flex;
flex-direction: column;
/* Start invisible, will be animated in */
opacity: 0;
transform: scale(0.8);
animation-fill-mode: forwards;
}
/* When popup is opened add this class for animation */
.popup-box.pop-in {
animation-name: popup-pop-in;
animation-duration: 0.25s;
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
/* When popup is closing add this class */
.popup-box.pop-out {
animation-name: popup-pop-out;
animation-duration: 0.25s;
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
2. JavaScript - Modify open/close methods to add animation classes and wait for animation end before hiding overlay
Replace your existing open/close popup JS functions with these:
js
3. Small addition: define a variable for popupBox:
At the top of your JavaScript snippet (where you define consts):