如何在CSS中垂直扩展卡片?



我最近开始学习web开发,想自己做一个项目。我学会了做一个很酷的桌面网站,但我不能够理解如何转换水平卡,垂直。

这是主页

我只是想让所有的面板堆叠成一列,并在智能手机上垂直展开。我确实尝试使用媒体查询和改变flex方向列,但它最终像这样。


const panels = document.querySelectorAll(".panel");
panels.forEach((panels) => {
panels.addEventListener("click", () => {
removeActiceClasses();
panels.classList.add("active");
});
});
function removeActiceClasses() {
panels.forEach((panels) => {
panels.classList.remove("active");

});
}
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
background-color: #1a1a2e;
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container {
display: flex;
width: 90vw;
}
.panel {
background-size: auto 100%;
background-position: center;
background-repeat: no-repeat;
height: 80vh;
border-radius: 50px;
color: white;
cursor: pointer;
flex: 0.5;
margin: 10px;
position: relative;
transition: flex 0.7s ease-in;
}
.panel h3 {
font-size: 24px;
position: absolute;
bottom: 20px;
left: 20px;
margin: 0;
opacity: 0;
}
.panel.active {
flex: 5;
}
.panel.active h3 {
opacity: 1;
transition: opacity 0.3s ease-in 0.4s;
}
@media(max-width: 480px) {
.container {
width: 90vw;
}
.panel:nth-of-type(4),
.panel:nth-of-type(5) {
display: none;
}
.abilities {
background-position: right;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<title>God of War</title>
<link rel="icon" href="favicon/favicon.ico" type="image/icon">
</head>
<body>
<div class="container">
<div class="panel world active" style="background-image: url(images/God-of-war-world.jpg);">
<h3>Explore The World</h3>
</div>
<div class="panel characyers" style="background-image: url(images/God-of-War-characters.jpeg);">
<h3>Main Characters</h3>
</div>
<div class="panel creatures" style="background-image: url(images/God-of-War-creatures.jpg);">
<h3>Mythical Creatures</h3>
</div>
<div class="panel enemies" style="background-image: url(images/God-of-War-enemies.jpg);">
<h3>Enemies</h3>
</div>
<div class="panel abilities" style="background-image: url(images/God-of-War-abilities.jpg);">
<h3>Items and Abilities</h3>
</div>
</div>
<script src="script.js"></script>
</body>
</html>

您可以将flex-direction: column添加到容器中,并为其提供高度,例如根据您的视图端口单位-100vh的使用情况。当你正在使用视图单元时,看到全屏输出会更好。

const panels = document.querySelectorAll(".panel");
panels.forEach((panels) => {
panels.addEventListener("click", () => {
removeActiceClasses();
panels.classList.add("active");
});
});
function removeActiceClasses() {
panels.forEach((panels) => {
panels.classList.remove("active");

});
}
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
background-color: #1a1a2e;
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container {
display: flex;
flex-direction: column; /* Code added */
width: 90vw;
height: 100vh; /* Code added */
}
.panel {
background-size: auto 100%;
background-position: center;
background-repeat: no-repeat;
height: 80vh;
border-radius: 50px;
color: white;
cursor: pointer;
flex: 0.5;
margin: 10px;
position: relative;
transition: flex 0.7s ease-in;
}
.panel h3 {
font-size: 24px;
position: absolute;
bottom: 20px;
left: 20px;
margin: 0;
opacity: 0;
}
.panel.active {
flex: 2;
}
.panel.active h3 {
opacity: 1;
transition: opacity 0.3s ease-in 0.4s;
}
@media(max-width: 480px) {
.container {
width: 90vw;
}
.panel:nth-of-type(4),
.panel:nth-of-type(5) {
display: none;
}
.abilities {
background-position: right;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<title>God of War</title>
<link rel="icon" href="favicon/favicon.ico" type="image/icon">
</head>
<body>
<div class="container">
<div class="panel world active" style="background-image: url(https://oyster.ignimgs.com/wordpress/stg.ign.com/2020/03/god-of-war-fallen-god-cover-1211969.jpg);">
<h3>Explore The World</h3>
</div>
<div class="panel characyers" style="background-image: url(https://oyster.ignimgs.com/wordpress/stg.ign.com/2020/03/god-of-war-fallen-god-cover-1211969.jpg);">
<h3>Main Characters</h3>
</div>
<div class="panel creatures" style="background-image: url(https://oyster.ignimgs.com/wordpress/stg.ign.com/2020/03/god-of-war-fallen-god-cover-1211969.jpg);">
<h3>Mythical Creatures</h3>
</div>
<div class="panel enemies" style="background-image: url(https://oyster.ignimgs.com/wordpress/stg.ign.com/2020/03/god-of-war-fallen-god-cover-1211969.jpg);">
<h3>Enemies</h3>
</div>
<div class="panel abilities" style="background-image: url(https://oyster.ignimgs.com/wordpress/stg.ign.com/2020/03/god-of-war-fallen-god-cover-1211969.jpg);">
<h3>Items and Abilities</h3>
</div>
</div>
<script src="script.js"></script>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新