<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3相框摇晃背景动画特效</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(45deg, #ff9a9e, #fad0c4);
overflow: hidden;
font-family: 'Arial', sans-serif;
}
.wall {
position: relative;
width: 100%;
height: 100vh;
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect width="100" height="100" fill="none" stroke="rgba(0,0,0,0.05)" stroke-width="1"/></svg>');
display: flex;
justify-content: center;
align-items: center;
}
.photo-frame {
position: relative;
width: 300px;
height: 400px;
background: #fff;
padding: 20px;
box-shadow: 0 30px 50px rgba(0, 0, 0, 0.2);
transform-style: preserve-3d;
animation: swing 8s ease-in-out infinite;
z-index: 10;
}
.photo-frame::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(135deg, transparent 0%, rgba(255,255,255,0.3) 50%, transparent 100%);
z-index: 2;
}
.photo-frame::after {
content: '';
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
border: 2px solid rgba(0, 0, 0, 0.1);
z-index: 1;
}
.photo-content {
width: 100%;
height: 100%;
background: url('https://images.unsplash.com/photo-1506744038136-46273834b3fb') center/cover;
position: relative;
overflow: hidden;
}
.photo-content::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(to bottom, rgba(255,255,255,0.1) 0%, rgba(0,0,0,0.1) 100%);
}
.nail {
position: absolute;
top: -10px;
left: 50%;
width: 20px;
height: 20px;
background: radial-gradient(circle, #d4af37, #a67c00);
border-radius: 50%;
transform: translateX(-50%);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
z-index: 20;
}
.nail::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 6px;
height: 6px;
background: rgba(0, 0, 0, 0.3);
border-radius: 50%;
transform: translate(-50%, -50%);
}
.shadow {
position: absolute;
bottom: -30px;
left: 50%;
width: 200px;
height: 20px;
background: rgba(0, 0, 0, 0.2);
border-radius: 50%;
filter: blur(10px);
transform: translateX(-50%);
animation: shadow 8s ease-in-out infinite;
}
@keyframes swing {
0%, 100% {
transform: rotate3d(0.5, 1, 0, -5deg) translateY(0);
}
25% {
transform: rotate3d(0.5, 1, 0, 5deg) translateY(-10px);
}
50% {
transform: rotate3d(0.5, 1, 0, -3deg) translateY(5px);
}
75% {
transform: rotate3d(0.5, 1, 0, 3deg) translateY(-5px);
}
}
@keyframes shadow {
0%, 100% {
transform: translateX(-50%) scale(1);
opacity: 0.6;
}
25% {
transform: translateX(-50%) scale(0.9);
opacity: 0.8;
}
50% {
transform: translateX(-50%) scale(1.1);
opacity: 0.5;
}
75% {
transform: translateX(-50%) scale(0.95);
opacity: 0.7;
}
}
.floating-elements {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.floating-element {
position: absolute;
background: rgba(255, 255, 255, 0.2);
border-radius: 50%;
animation: float 15s linear infinite;
}
@keyframes float {
0% {
transform: translateY(0) rotate(0deg);
opacity: 0;
}
10% {
opacity: 0.5;
}
100% {
transform: translateY(-1000px) rotate(720deg);
opacity: 0;
}
}
.credit {
position: absolute;
bottom: 20px;
left: 0;
width: 100%;
text-align: center;
color: rgba(255, 255, 255, 0.8);
font-size: 14px;
z-index: 100;
}
.credit a {
color: #d4af37;
text-decoration: none;
transition: color 0.3s;
}
.credit a:hover {
color: #fff;
}
</style>
</head>
<body>
<div class="wall">
<div class="floating-elements" id="floatingElements"></div>
<div class="photo-frame">
<div class="nail"></div>
<div class="photo-content"></div>
<div class="shadow"></div>
</div>
<div class="credit">
</div>
</div>
<script>
// 创建浮动元素
document.addEventListener('DOMContentLoaded', function() {
const container = document.getElementById('floatingElements');
const colors = ['rgba(255,255,255,0.3)', 'rgba(255,255,255,0.2)', 'rgba(255,255,255,0.1)'];
for (let i = 0; i < 20; i++) {
const element = document.createElement('div');
element.classList.add('floating-element');
// 随机大小
const size = Math.random() * 30 + 10;
element.style.width = `${size}px`;
element.style.height = `${size}px`;
// 随机位置
element.style.left = `${Math.random() * 100}%`;
element.style.top = `${Math.random() * 100 + 100}%`;
// 随机颜色
element.style.background = colors[Math.floor(Math.random() * colors.length)];
// 随机动画延迟和持续时间
element.style.animationDelay = `${Math.random() * 15}s`;
element.style.animationDuration = `${Math.random() * 10 + 10}s`;
container.appendChild(element);
}
});
</script>
</body>
</html>