纯 JS 实现 Toast 提示框:HTML、CSS 和 JavaScript 代码
<div id='toast'></div>
<style>
#toast {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #000;
color: #fff;
padding: 10px 20px;
border-radius: 5px;
z-index: 9999;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
#toast.show {
opacity: 1;
}
</style>
<script>
function showToast(message, duration) {
var toast = document.getElementById('toast');
toast.innerHTML = message;
toast.classList.add('show');
setTimeout(function() {
toast.classList.remove('show');
}, duration);
}
// 示例,显示 'Hello World' 3秒钟
showToast('Hello World', 3000);
</script>
原文地址: https://www.cveoy.top/t/topic/mHHA 著作权归作者所有。请勿转载和采集!