我在我创建的菜单中写了一个过滤函数,但是除了“所有菜单”之外,还有一些错误。换句话说,它把一个菜单的两个元素中的一个放在最右边,另一个放在最左边。但是我想让它们并排放置。我该怎么做呢?我想我在css代码中犯了一个错误,但是我找不到我犯错误的地方。
const liItem = document.querySelectorAll('ul li');
const imgItem = document.querySelectorAll('.product img');
liItem.forEach(li => {
li.onclick = function(){
//active
liItem.forEach(li => {
li.className = '';
})
li.className = 'active';
//Filter
// console.log(li.textContent);
const value = li.textContent;
imgItem.forEach(img => {
img.style.display = 'none';
console.log(img.getAttribute('data-filter'));
if (img.getAttribute('data-filter') == value.toLowerCase() || value == 'All Menu'){
img.style.display = 'block';
}
})
}
});
body, h1, ul, li{
margin: 0;
padding: 0;
}
body{
background-color: rgb(181, 181, 181);
}
section{
width: 70%;
margin: 50px auto;
}
section h1{
text-align: center;
text-transform: uppercase;
letter-spacing: 1px;
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5) ;
font-family: Arial, Helvetica, sans-serif;
color: white;
font-size: 2em;
}
ul{
width: 100%;
display: flex;
justify-content: center;
margin-top: 10px;
border-bottom: 2px solid rgba(255,255,255, 0.5);
padding-bottom: 10px;
margin-bottom: 10px;
box-sizing: border-box;
}
ul li{
list-style: none;
background-color: rgba(255,255,255, 0.5);
padding: 5px 10px;
margin: 0 10px;
cursor: pointer;
font-size: 1.2em;
}
ul li.active{
background-color: rgb(73, 202, 73);
}
.product{
width: 100%;
padding: 0 10px;
box-sizing: border-box;
display: flex;
justify-content: space-between;
flex-wrap: wrap ;
}
.product img{
width: 170px;
height: 170px;
margin-bottom: 10px;
}
/* body,h1,ul,li{
margin: 0;
padding: 0;
}
body{
background-color: #667d8e;
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
section{
width: 85%;
margin: 50px auto;
}
section h1{
text-align: center;
text-transform: uppercase;
letter-spacing: 1px;
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
}
ul{
width: 100%;
display: flex;
justify-content: center;
margin-top: 10px;
border-bottom: 2px solid rgba(255, 255, 255, 0.57);
padding-bottom: 10px;
box-sizing: border-box;
margin-bottom: 10px;
}
ul li{
list-style: none;
background-color: rgba(255, 255, 255, 0.5);
padding: 5px 10px;
margin: 0 10px;
cursor: pointer;
font-size: 1.2em;
}
ul li.active{
background-color: rgba(41, 255, 41, 0.897);
}
.product{
width: 100%;
padding: 0 10px;
box-sizing: border-box;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.product img{
width: 200px;
height: 180px;
margin-bottom: 10px;
} */
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Filter Menu</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section>
<h1>Filter Image</h1>
<ul>
<li class="active">All Menu</li>
<li>Food</li>
<li>Car</li>
<li>House</li>
<li>Animal</li>
<li>History</li>
</ul>
<div class="product">
<img src="food_photos/food_1.jpeg" data-filter="food">
<img src="food_photos/food_2.jpeg" data-filter="food">
<img src="food_photos/food_3.jpeg" data-filter="car">
<img src="food_photos/food_4.jpeg" data-filter="car">
<img src="food_photos/food_5.jpeg" data-filter="house">
<img src="food_photos/food_6.jpeg" data-filter="house">
<img src="food_photos/food_1.jpeg" data-filter="animal">
<img src="food_photos/food_2.jpeg" data-filter="animal">
<img src="food_photos/food_3.jpeg" data-filter="history">
<img src="food_photos/food_4.jpeg" data-filter="history">
</div>
</section>
<script src="app.js"></script>
</body>
</html>
2条答案
按热度按时间uplii1fm1#
您的类
product
包含以下规则,该规则会产生意外行为:改用此规则集:
2wnc66cl2#
我将与你分享我的代码的最新版本。整个菜单选项没有问题,但是当我切换到其他菜单时,我仍然有问题显示它,因为其他菜单中的项目很少。如果有足够的图像不占满整行,我希望它们从左边开始排序。我该怎么办?
https://ibb.co/VWDxLQthttps://ibb.co/YPjfQP9(问题)