<html><head><base href="https://webbox-ai.vercel.app/card-filtering/" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Enhanced Card Filtering Functionality</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
padding: 20px;
background-color: #f0f0f0;
}
h1 {
text-align: center;
margin-bottom: 30px;
color: #333;
}
.filter-buttons-wrapper {
display: flex;
justify-content: center;
gap: 15px;
margin: 20px 0px;
flex-wrap: wrap;
}
.filter-button {
padding: 12px 20px;
font-size: 18px;
font-weight: bold;
border: none;
background-color: #3498db;
color: white;
cursor: pointer;
border-radius: 5px;
transition: all 0.3s ease;
}
.filter-button:hover {
background-color: #2980b9;
}
.filter-button.active {
background-color: #e74c3c;
}
.content-wrapper {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
.card {
width: calc(25% - 20px);
padding: 20px;
background-color: white;
font-size: 18px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
display: flex;
flex-direction: column;
align-items: center;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
.card-image {
width: 100%;
height: 150px;
background-size: cover;
background-position: center;
border-radius: 5px;
margin-bottom: 15px;
}
.hide {
display: none;
}
@media (max-width: 1024px) {
.card {
width: calc(33.33% - 20px);
}
}
@media (max-width: 768px) {
.card {
width: calc(50% - 20px);
}
}
@media (max-width: 480px) {
.card {
width: 100%;
}
}
</style>
</head>
<body>
<h1>Filter Cards By Category</h1>
<div class="filter-buttons-wrapper"></div>
<div class="content-wrapper"></div>
<script>
const categories = ["All", "Men", "Women", "Kids"];
const content = [
{
id: "Men",
label: "Men Shirt 1",
image: "https://via.placeholder.com/150x150.png?text=Men+Shirt+1"
},
{
id: "Men",
label: "Men Shirt 2",
image: "https://via.placeholder.com/150x150.png?text=Men+Shirt+2"
},
{
id: "Men",
label: "Men Shirt 3",
image: "https://via.placeholder.com/150x150.png?text=Men+Shirt+3"
},
{
id: "Men",
label: "Men Shirt 4",
image: "https://via.placeholder.com/150x150.png?text=Men+Shirt+4"
},
{
id: "Men",
label: "Men Shirt 5",
image: "https://via.placeholder.com/150x150.png?text=Men+Shirt+5"
},
{
id: "Women",
label: "Women Shirt 1",
image: "https://via.placeholder.com/150x150.png?text=Women+Shirt+1"
},
{
id: "Women",
label: "Women Shirt 2",
image: "https://via.placeholder.com/150x150.png?text=Women+Shirt+2"
},
{
id: "Women",
label: "Women Shirt 3",
image: "https://via.placeholder.com/150x150.png?text=Women+Shirt+3"
},
{
id: "Women",
label: "Women Shirt 4",
image: "https://via.placeholder.com/150x150.png?text=Women+Shirt+4"
},
{
id: "Women",
label: "Women Shirt 5",
image: "https://via.placeholder.com/150x150.png?text=Women+Shirt+5"
},
{
id: "Kids",
label: "Kids Shirt 1",
image: "https://via.placeholder.com/150x150.png?text=Kids+Shirt+1"
},
{
id: "Kids",
label: "Kids Shirt 2",
image: "https://via.placeholder.com/150x150.png?text=Kids+Shirt+2"
},
{
id: "Kids",
label: "Kids Shirt 3",
image: "https://via.placeholder.com/150x150.png?text=Kids+Shirt+3"
},
{
id: "Kids",
label: "Kids Shirt 4",
image: "https://via.placeholder.com/150x150.png?text=Kids+Shirt+4"
},
{
id: "Kids",
label: "Kids Shirt 5",
image: "https://via.placeholder.com/150x150.png?text=Kids+Shirt+5"
},
];
const filterButtons = document.querySelector(".filter-buttons-wrapper");
const contentWrapper = document.querySelector(".content-wrapper");
function createCategory() {
categories.forEach((category) => {
const buttonEle = document.createElement("button");
buttonEle.innerText = category;
buttonEle.classList.add("filter-button");
buttonEle.setAttribute("data-filter", category);
if (category === "All") {
buttonEle.classList.add("active");
}
filterButtons.appendChild(buttonEle);
});
}
function createContent() {
content.forEach((contentItem) => {
const singleContentItem = document.createElement("div");
singleContentItem.classList.add("card", contentItem.id);
const imageDiv = document.createElement("div");
imageDiv.classList.add("card-image");
imageDiv.style.backgroundImage = `url(${contentItem.image})`;
const labelDiv = document.createElement("div");
labelDiv.textContent = contentItem.label;
singleContentItem.appendChild(imageDiv);
singleContentItem.appendChild(labelDiv);
contentWrapper.appendChild(singleContentItem);
});
}
createCategory();
createContent();
const allFilterButtons = document.querySelectorAll(".filter-button");
const allCards = document.querySelectorAll(".card");
function filterCardsByCategory(extractCurrentCategory, allCards) {
allCards.forEach((item) => {
const isShowAllCards = extractCurrentCategory.toLowerCase() === "all";
const isItemFiltered = !item.classList.contains(extractCurrentCategory);
if (isItemFiltered && !isShowAllCards) {
item.classList.add("hide");
} else {
item.classList.remove("hide");
}
});
}
allFilterButtons.forEach((singleFilterbuttonItem) => {
singleFilterbuttonItem.addEventListener("click", () => {
const extractCurrentCategory = singleFilterbuttonItem.dataset.filter;
// Remove active class from all buttons
allFilterButtons.forEach(btn => btn.classList.remove("active"));
// Add active class to clicked button
singleFilterbuttonItem.classList.add("active");
filterCardsByCategory(extractCurrentCategory, allCards);
});
});
// Add animation to cards
function addCardAnimation() {
allCards.forEach((card, index) => {
card.style.animation = `fadeIn 0.5s ease ${index * 0.1}s forwards`;
});
}
addCardAnimation();
// Add this CSS for the animation
const style = document.createElement('style');
style.textContent = `
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card {
opacity: 0;
}
`;
document.head.appendChild(style);
</script>
</body>
</html>