π Copy Success Toast Notification
This template displays a toast notification in the bottom right corner when a copy action is successful.
π Copy Function Demo
Fully Functional Copy + Toast Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Copy Function with Toast Notification</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 2rem;
text-align: center;
}
#toast {
visibility: hidden;
min-width: 200px;
margin: 0 auto;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 5px;
padding: 10px;
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
z-index: 1;
opacity: 0;
transition: opacity 0.5s, visibility 0.5s;
}
#toast.show {
visibility: visible;
opacity: 1;
}
button {
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
}
</style>
</head>
<body>
<h2>π Copy Function Demo</h2>
<button onclick="copyText()">π Try Copying</button>
<div id="toast">β
Copied!</div>
<script>
function copyText() {
const text = "This is the text to be copied";
navigator.clipboard.writeText(text).then(() => {
const toast = document.getElementById("toast");
toast.classList.add("show");
setTimeout(() => {
toast.classList.remove("show");
}, 2000);
}).catch(err => {
alert("Copy failed: " + err);
});
}
</script>
</body>
</html>
β Use Cases and Benefits of This Template
- Provides visual feedback to users that the copy was successful
- Enhances UX by enabling intuitive interaction
- Makes it clear when the copy action has completed
- Notification visibility is automatically managed
- Can be reused in many UIs with copy buttons
π§© Use and Customization Tips for Toast Notifications
This template uses a toast notification to instantly inform users of a successful copy action, greatly improving user experience (UX) with minimal effort.
With navigator.clipboard.writeText(...), you can copy text to the clipboard. Upon success, classList.add("show") makes the toast appear, and setTimeout(...) hides it automatically.
Such notifications can be used not only for copy actions, but also for form submissions, error messages, save confirmations, etc. By dynamically changing the message with innerText, this notification can be reused in various scenarios.
π§ Customization Tips
The display position is fixed with right: 30px; bottom: 30px; but can be changed to other corners or centered with CSS. The display duration can also be adjusted by changing the number in setTimeout(..., 2000).
Animations are handled with @keyframes fadein and fadeout, allowing for a smooth and lightweight visual effect using only CSS. With minimal code, you get maximum effect by combining JavaScript and CSS.