分享一个小demo。
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>吐丝</title>
<style>
.toast {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0,0,0,.5);
padding: 5px 10px;
border-radius: 5px;
color: #fff;
transition: 1s all ease;
}
</style>
</head>
<body>
<input type="button" class="btn" value="Click Me">
</body>
</html>
<script>
(function() {
var target = document.querySelector('.btn')
target.addEventListener('click', function() {
toast('Hey,yo,This is Martin', 600)
})
function toast (text, t) {
var el = document.createElement('div')
el.innerHTML = text
el.className = 'toast'
document.body.appendChild(el)
setTimeout(() => {
el.style.opacity = 0
document.body.removeChild(el)
}, t);
}
})()
</script>