pixel-roll-up / index.html
alababy's picture
现在像素收起至右下角,我需要收起至左上角,过渡效果先慢后快再慢。且像素缩小10倍 - Follow Up Deployment
cb05c27 verified
Raw History Blame Contribute Delete
8.63 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>像素点卷动效果</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@keyframes pixelBounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
.pixel {
width: 0.08px;
height: 0.08px;
display: inline-block;
margin: 0 0.01px;
border-radius: 0.01px;
transition: transform 0.6s cubic-bezier(0.45, 0, 0.55, 1), opacity 0.4s ease;
}
.container {
perspective: 1000px;
}
@media (min-width: 768px) {
.pixel {
width: 12px;
height: 12px;
margin: 0 2px;
}
}
</style>
</head>
<body class="bg-gray-900 min-h-screen flex flex-col items-center justify-center p-4">
<div class="container w-full max-w-4xl">
<div id="pixelWall" class="grid grid-cols-20 gap-1 md:gap-2 mb-10"></div>
<iframe id="huggingfaceFrame" src="https://huggingface.co" class="w-full h-0 opacity-0 transition-all duration-500 rounded-lg bg-white" frameborder="0" allowfullscreen></iframe>
<div class="text-center">
<button id="toggleBtn" class="px-8 py-3 bg-indigo-600 hover:bg-indigo-700 text-white font-bold rounded-full shadow-lg transform transition-all hover:scale-105 focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-opacity-75">
<span id="btnText">卷起像素</span>
<svg id="btnIcon" class="w-5 h-5 inline-block ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7"></path>
</svg>
</button>
</div>
<div class="mt-10 text-center text-gray-400">
<p class="text-sm">点击按钮体验像素动画效果</p>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const pixelWall = document.getElementById('pixelWall');
const toggleBtn = document.getElementById('toggleBtn');
const btnText = document.getElementById('btnText');
const btnIcon = document.getElementById('btnIcon');
let isRolledUp = false;
let pixels = [];
// 创建像素网格
function createPixelWall() {
// 根据宽度动态计算列数
const containerWidth = pixelWall.clientWidth;
const pixelSizeWithGap = window.innerWidth < 768 ? 10 : 16;
const cols = Math.floor(containerWidth / pixelSizeWithGap) - 2;
pixelWall.innerHTML = '';
pixelWall.className = `grid gap-1 md:gap-2 mb-10`;
pixelWall.style.gridTemplateColumns = `repeat(${cols}, minmax(0, 1fr))`;
// 创建像素点
const rows = Math.floor(window.innerHeight * 0.4 / (window.innerWidth < 768 ? 10 : 16));
const totalPixels = cols * rows;
for (let i = 0; i < totalPixels; i++) {
const pixel = document.createElement('div');
pixel.className = 'pixel';
// 随机颜色,但保持在紫色/蓝色色调范围内
const hue = 240 + Math.random() * 30;
const saturation = 70 + Math.random() * 20;
const lightness = 50 + Math.random() * 20;
pixel.style.backgroundColor = `hsl(${hue}, ${saturation}%, ${lightness}%)`;
pixel.dataset.index = i;
pixel.dataset.col = i % cols;
pixel.dataset.row = Math.floor(i / cols);
pixelWall.appendChild(pixel);
pixels.push(pixel);
}
}
// 卷起/展开动画
function togglePixelWall() {
isRolledUp = !isRolledUp;
const frame = document.getElementById('huggingfaceFrame');
if (isRolledUp) {
btnText.textContent = '展开像素';
btnIcon.style.transform = 'rotate(180deg)';
// 显示iframe
frame.style.height = '600px';
frame.style.opacity = '1';
frame.style.marginBottom = '2rem';
// 向中心收起效果
pixels.forEach(pixel => {
const col = parseInt(pixel.dataset.col);
const row = parseInt(pixel.dataset.row);
const delay = (col + row) * 0.01;
// Calculate distance to top-left corner (0,0)
const targetX = 0;
const targetY = 0;
const currentX = col * (window.innerWidth < 768 ? 1 : 1.6);
const currentY = row * (window.innerWidth < 768 ? 1 : 1.6);
pixel.style.transition = `transform 0.6s cubic-bezier(0.45, 0, 0.55, 1) ${delay}s, opacity 0.4s ease ${delay}s`;
pixel.style.transform = `translate(${currentX}px, ${currentY}px) scale(0.1)`;
pixel.style.opacity = '0';
});
} else {
btnText.textContent = '卷起像素';
btnIcon.style.transform = 'rotate(0deg)';
// 隐藏iframe
frame.style.height = '0';
frame.style.opacity = '0';
frame.style.marginBottom = '0';
// 从中心展开效果
const shuffledPixels = [...pixels].sort(() => Math.random() - 0.5);
shuffledPixels.forEach((pixel, index) => {
const delay = index * 0.002;
pixel.style.transition = `transform 0.6s cubic-bezier(0.45, 0, 0.55, 1) ${delay}s, opacity 0.4s ease ${delay}s`;
pixel.style.transform = 'translate(0, 0) scale(1)';
pixel.style.opacity = '1';
});
}
}
// 添加悬停动画
function addHoverEffect() {
pixels.forEach(pixel => {
pixel.addEventListener('mouseenter', function() {
if (!isRolledUp) {
this.style.animation = 'pixelBounce 0.5s ease';
// 动画结束后清除
setTimeout(() => {
this.style.animation = '';
}, 500);
}
});
});
}
// 初始化
createPixelWall();
addHoverEffect();
// 窗口大小改变时重新创建像素墙
window.addEventListener('resize', function() {
createPixelWall();
addHoverEffect();
});
// 按钮点击事件
toggleBtn.addEventListener('click', togglePixelWall);
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=alababy/pixel-roll-up" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>