提问人:Farah Anany 提问时间:9/25/2023 最后编辑:Farah Anany 更新时间:9/25/2023 访问量:62
如何防止在显示子类别时将类别推高?
How do I stop the category from getting pushed up whenever I display the subcategory?
问:
我有 4 个列表形式的类别;所有,城市化,建筑和室内设计。城市规划、建筑和室内设计都以列表的形式具有子类别。该代码既可以过滤图像,也可以显示子图像。我遇到的问题是,每当我单击“城市规划”、“建筑”或“室内设计”以显示子类别时;整个街区被推高。
我希望所有城市化、所有、建筑和室内设计都保持在同一条水平线上,即使我单击任何类别以显示子类别。我希望子类别在单击时低于其父类别。您的帮助将不胜感激。以下是我的代码:
<div class="about_section">
<br />
<h1>projects</h1>
<ul class="switcher">
<li data-cat=".all" class="active">All</li>
<li data-cat=".urb" id="show">
Urbanism
<ul class="switcher" id="content" style="display: none">
<li data-cat=".cityscale">City Scale</li>
<li data-cat=".residential">Residential</li>
<li data-cat=".entertainment">Entertainment</li>
</ul>
</li>
<li data-cat=".arch" id="showarch">
Architecture
<ul class="switcher" id="contentarch" style="display: none">
<li data-cat=".adminstrative">Adminstrative</li>
<li data-cat=".commercial">Commercial</li>
<li data-cat=".hospitality">Hospitality</li>
<li data-cat=".residential">Residential</li>
<li data-cat=".educational">Educational</li>
<li data-cat=".cultural">Cultural</li>
<li data-cat=".entertainment">Entertainment</li>
<li data-cat=".medical">Medical</li>
<li data-cat=".religious">Religious</li>
<li data-cat=".restoration">Restoration</li>
<li data-cat=".infrastructure">Infrastructure</li>
</ul>
</li>
<li data-cat=".int" id="showint">
Interior Design
<ul class="switcher" id="contentint" style="display: none">
<li data-cat=".residential">Residential</li>
<li data-cat=".commercial">Commercial</li>
<li data-cat=".cultural">Cultural</li>
</ul>
</li>
</ul>
</div>
let switcherLi = document.querySelectorAll(".switcher li");
let imgs = Array.from(document.images);
switcherLi.forEach((li) => {
li.addEventListener("click", removeActive);
li.addEventListener("click", filter);
});
function removeActive() {
switcherLi.forEach((li) => {
li.classList.remove("active");
this.classList.add("active");
});
}
function filter() {
imgs.forEach((img) => {
if (!img.classList.contains("logo")) {
img.style.display = "none";
}
});
document.querySelectorAll(this.dataset.cat).forEach((el) => {
el.style.display = "block";
});
}
var urbanismElement = document.getElementById("show");
var contentElement = document.getElementById("content");
urbanismElement.onclick = function (event) {
if (event.target === urbanismElement) {
if (contentElement.style.display === "none") {
contentElement.style.display = "block";
archcontent.style.display = "none"; // Hide architecture subcategories
intcontent.style.display = "none"; // Hide interior design subcategories
} else {
contentElement.style.display = "none";
}
}
};
var archelement = document.getElementById("showarch");
var archcontent = document.getElementById("contentarch");
archelement.onclick = function (event) {
if (event.target === archelement) {
if (archcontent.style.display === "none") {
archcontent.style.display = "block";
contentElement.style.display = "none"; // Hide urbanism subcategories
intcontent.style.display = "none"; // Hide interior design subcategories
} else {
archcontent.style.display = "none";
}
}
};
var intelement = document.getElementById("showint");
var intcontent = document.getElementById("contentint");
intelement.onclick = function (event) {
if (event.target === intelement) {
if (intcontent.style.display === "none") {
intcontent.style.display = "block";
contentElement.style.display = "none"; // Hide urbanism subcategories
archcontent.style.display = "none"; // Hide architecture subcategories
} else {
intcontent.style.display = "none";
}
}
};
var allElement = document.querySelector('.switcher li[data-cat=".all"]');
var categoryElements = document.querySelectorAll(".switcher li[data-cat]");
allElement.onclick = function (event) {
if (event.target === allElement) {
for (var i = 0; i < categoryElements.length; i++) {
categoryElements[i].classList.remove("active");
}
allElement.classList.add("active");
contentElement.style.display = "none";
archcontent.style.display = "none";
intcontent.style.display = "none";
}
};
@import url("https://fonts.googleapis.com/css2?family=Advent+Pro&family=Amatic+SC&family=Bricolage+Grotesque:[email protected]&family=Cardo&family=Comfortaa:wght@300&family=Domine&family=Gruppo&family=Indie+Flower&family=Kodchasan:wght@300;500&family=Lato&family=Oswald&family=Playfair+Display+SC&family=Playfair+Display:wght@500&family=Roboto:wght@100&family=Rubik:ital,wght@1,300&family=Syncopate:wght@400;700&family=Urbanist&display=swap");
* {
font-family: "laluxesserifw05-regularRg";
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: "MADEMirage-Thin";
src: url("MADEMirage-Thin.ttf.woff") format("woff"),
url("MADEMirage-Thin.ttf.svg#MADEMirage-Thin") format("svg"),
url("MADEMirage-Thin.ttf.eot"),
url("MADEMirage-Thin.ttf.eot?#iefix") format("embedded-opentype");
font-weight: normal;
font-style: normal;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
p {
font-family: "made_miragethin";
}
section {
padding-top: 30px;
height: 100vh;
display: flex;
justify-content: center;
scroll-snap-align: start;
}
.container {
scroll-snap-type: y mandatory;
overflow-y: scroll;
height: 100vh;
}
.navbar-brand {
margin-right: 0;
}
.navbar-nav {
align-items: center;
}
.navbar .navbar-nav .nav-link {
color: black;
font-size: 1.1em;
padding: 0.5 1em;
}
.nav-item {
font-size: 14px;
font-weight: Regular;
}
.about_section {
padding-top: 80px;
padding-left: 30px;
padding-right: 30px;
text-align: center;
justify-content: center;
}
.about_section h1 {
text-align: center;
}
.footer {
text-align: center;
}
.gallery {
--s: 150px; /* control the size */
--g: 10px; /* control the gap */
--f: 1.5; /* control the scale factor */
width: 100vw;
height: 100vh;
display: grid;
gap: var(--g);
width: calc(7 * var(--s) + 6 * var(--g));
aspect-ratio: 1;
grid-template-columns: repeat(3, auto);
}
.gallery > a {
display: contents;
}
.gallery img {
width: 0;
height: 0;
min-height: 100%;
min-width: 100%;
object-fit: cover;
cursor: pointer;
filter: grayscale(80%);
transition: 0.35s linear;
}
.gallery img:hover {
filter: grayscale(0);
width: calc(var(--s) * var(--f));
height: calc(var(--s) * var(--f));
}
body {
margin: 0;
display: grid;
place-content: center;
}
.container {
display: flex;
align-items: center;
justify-content: center;
}
@media screen and (max-width: 700px) {
div.large {
display: none;
}
}
/***** for mobiles *****/
@media (min-width: 699px) {
.cont {
display: none;
}
}
@media (min-width: 699px) {
div.small {
display: none;
}
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: 20px;
align-items: stretch;
}
.grid img {
border: 1px solid #ccc;
box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.3);
max-width: 100%;
margin-left: 5px;
}
.grid {
display: grid;
grid-template-columns: repeat(1, 1fr);
justify-content: center;
gap: 10px;
}
.grid div {
max-height: 300px;
}
.switcher {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
list-style: none;
width: fit-content;
padding: 10px 10px;
}
.switcher li {
border-radius: 4px;
padding: 10px 10px;
background-color: white;
cursor: pointer;
transition: 0.2s linear;
font-size: 12px;
user-select: none;
margin: 5px;
justify-content: center;
align-items: center;
display: flex;
display: inline-block;
cursor: pointer;
}
.content div {
display: none;
margin-top: 10px;
}
答:
0赞
EzT
9/25/2023
#1
我看不出这个问题与 php 有什么关系,如果我理解正确的话,它更像是一个 css 问题。无论如何,您有一个额外的 .switcher 类的 css 规则。只需注释掉对齐项中心,它就可以工作了。
.switcher {
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
/* align-items: center; */
list-style: none;
width: fit-content;
padding: 10px 10px;
}
评论
.switcher .switcher { position: absolute; z-index: 9999; }
.switcher
.switcher .switcher
width: 300px
left: -100px