纯CSS移动弹出框动画是指使用CSS来创建和控制一个弹出框的显示和隐藏动画效果。这种动画不依赖于JavaScript,完全通过CSS的动画和过渡属性来实现。
以下是一个简单的纯CSS移动弹出框动画示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Popup Animation</title>
<style>
.popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80%;
max-width: 300px;
padding: 20px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
z-index: 1000;
}
.popup.active {
display: block;
animation: fadeIn 0.3s ease-in-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: translate(-50%, -50%) scale(0.9); }
to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 999;
}
.overlay.active {
display: block;
}
</style>
</head>
<body>
<button onclick="showPopup()">Show Popup</button>
<div class="overlay" id="overlay"></div>
<div class="popup" id="popup">
<p>This is a popup!</p>
<button onclick="hidePopup()">Close</button>
</div>
<script>
function showPopup() {
document.getElementById('popup').classList.add('active');
document.getElementById('overlay').classList.add('active');
}
function hidePopup() {
document.getElementById('popup').classList.remove('active');
document.getElementById('overlay').classList.remove('active');
}
</script>
</body>
</html>
will-change
属性来提示浏览器提前优化动画元素。通过以上方法,可以有效地创建和控制纯CSS移动弹出框动画,提升用户体验。
领取专属 10元无门槛券
手把手带您无忧上云