JSFiddle - React, Tailwind, and code Playground
by kidastudio
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lottie Animation Grid</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Lottie Animation Grid</h1>
<div class="upload-form">
<input type="file" id="fileInput" accept=".json" multiple>
<button onclick="handleFileUpload()">Upload JSON</button>
</div>
<div class="grid-container" id="gridContainer"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.7.6/lottie.min.js"></script>
<script src="script.js"></script>
</body>
</html>
CSS
h1 {
text-align: center;
}
.upload-form {
text-align: center;
margin-bottom: 20px;
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
padding: 20px;
}
.animation-card {
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
overflow: hidden;
position: relative;
}
.animation-card:hover {
box-shadow: 0 8px 12px rgba(0, 0, 0, 0.2);
}
.animation {
position: relative;
cursor: pointer;
width: 100%;
height: 100%;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
opacity: 0;
transition: opacity 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 18px;
}
/* ... (existing code) ... */
.empty-box {
width: 200px; /* Adjust the size as needed */
height: 200px; /* Make it a square */
background-color: #eee; /* or any other desired color */
border: 1px solid #ccc;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: #333;
}
/* ... (existing code) ... */
JavaScript
var animations = [];
function loadAnimations() {
var gridContainer = document.getElementById('gridContainer');
gridContainer.innerHTML = '';
// Check if there are animations to load
if (animations.length === 0) {
var emptyBox = document.createElement('div');
emptyBox.classList.add('animation-card', 'empty-box');
gridContainer.appendChild(emptyBox);
emptyBox.appendChild(createOverlay('No Animations Uploaded'));
} else {
animations.forEach(function (animationPath) {
var animationCard = document.createElement('div');
animationCard.classList.add('animation-card');
gridContainer.appendChild(animationCard);
var animationContainer = document.createElement('div');
animationContainer.classList.add('animation');
animationCard.appendChild(animationContainer);
var animationData = {
container: animationContainer,
renderer: 'svg',
loop: true,
autoplay: false, // Set autoplay to false
path: animationPath
};
var anim = lottie.loadAnimation(animationData);
animationCard.addEventListener('mouseenter', function () {
anim.play();
});
animationCard.addEventListener('mouseleave', function () {
anim.pause();
});
animationCard.appendChild(createOverlay('Hover to Play'));
});
}
}
function createOverlay(text) {
var overlay = document.createElement('div');
overlay.classList.add('overlay');
overlay.textContent = text;
return overlay;
}
function handleFileUpload() {
var fileInput = document.getElementById('fileInput');
var files = fileInput.files;
for (var i = 0; i < files.length; i++) {
if (files[i].type === 'application/json') {
animations.push(URL.createObjectURL(files[i]));
...