我正在尝试在我的网站中制作一个部分来显示图片和带有标题的段落,具体取决于所选按钮。例如,通过单击午餐,午餐选项显示,默认早餐选项消失。
我一直在使用 CSS 网格和 javascript 来实现这一点,但是,当我单击按钮时,CSS 网格丢失了,我不确定为什么。我正在使用 display none 和 block 来显示和隐藏每个部分。
let breakfastButton = document.getElementById('breakfastButton').addEventListener('click', showBreakfast);
let lunchButton = document.getElementById('lunchButton').addEventListener('click', showLunch);
// breakfast
function showBreakfast() {
document.getElementById('breakfast').style.display = 'block';
document.getElementById('lunch').style.display = 'none';
}
// lunch
function showLunch() {
document.getElementById('lunch').style.display = 'block';
document.getElementById('breakfast').style.display = 'none';
}
* {
padding: 0;
margin: 0;
font-family: 'Roboto', sans-serif;
margin: 0 auto;
}
.container {
padding: 1em;
line-height: 2em;
max-width: 1200px
}
h2 {
font-size: 2em;
font-weight: 300;
padding: 10px 0
}
p {
font-size: 22px;
color: #707070;
font-weight: 300;
}
:root {
--main--color: #FF4E4E;
--main--color--hover: rgb(250, 0, 0);
--nav--size: 1.5em;
--footer--size: 1.125em;
}
/* when to eat */
.meals {
margin-top: 80px;
text-align: left;
/* grid */
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 20px;
}
#lunch {
display: none;
}
.button-container {
margin-top: 30px;
width: 60%;
/* grid */
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
grid-gap: 20px;
}
.button-basics {
background-color: var(--main--color);
width: 100px;
height: 30px;
border-radius: 20px;
color: white;
padding: 5px;
text-align: center;
}
.button-basics:hover {
background-color: var(--main--color--hover);
cursor: pointer;
}
.meals>img {
width: 100%;
}
<!-- meal times -->
<!-- breakfast -->
<div id="breakfast" class='meals'>
<img src="images/breakfast.jpg" alt="">
<div class="description">
<h2>Breakfast</h2>
<p>The most important meal of the day, right? Not
exactly. Since you are an athlete training and
eating constantly, breakfast can possibly mean
twice a day depending on your workouts. However,
it is still hugely important to refuel after any
early morning workouts with a filling and hearty
meal
</p>
</div>
</div>
<!-- lunch -->
<div id="lunch" class='meals'>
<img src="images/lunch.jpg" alt="">
<div class="description">
<h2>Lunch</h2>
<p>The most important meal of the day, right? Not
exactly. Since you are an athlete training and
eating constantly, breakfast can possibly mean
twice a day depending on your workouts. However,
it is still hugely important to refuel after any
early morning workouts with a filling and hearty
meal
</p>
</div>
</div>
<!-- meal buttons -->
<div class='button-container'>
<div id="breakfastButton" class='button-basics'>Breakfast</div>
<div id="lunchButton" class='button-basics'>Lunch</div>
<div class='button-basics'>Dinner</div>
<div class='button-basics'>Snacks</div>
</div>
</div>
如您所见,上面的图片是我想要的最终结果,而下面的图片是我点击午餐按钮时发生的情况,而不是我想要实现的结果。
最佳答案
好的,这应该很容易修复。查看更新后的 showBreakfast()
和 showLunch()
函数。在节目中,您没有将显示属性更改为 block
,而是想将它们更改为 grid
,以保持所需的布局。通过将 display
属性更改为 block
,您正在破坏您的布局。运行该代码段,您会微笑。
let breakfastButton = document.getElementById('breakfastButton').addEventListener('click', showBreakfast);
let lunchButton = document.getElementById('lunchButton').addEventListener('click', showLunch);
// breakfast
function showBreakfast() {
document.getElementById('breakfast').style.display = 'grid';
document.getElementById('lunch').style.display = 'none';
}
// lunch
function showLunch() {
document.getElementById('lunch').style.display = 'grid';
document.getElementById('breakfast').style.display = 'none';
}
* {
padding: 0;
margin: 0;
font-family: 'Roboto', sans-serif;
margin: 0 auto;
}
.container {
padding: 1em;
line-height: 2em;
max-width: 1200px
}
h2 {
font-size: 2em;
font-weight: 300;
padding: 10px 0
}
p {
font-size: 22px;
color: #707070;
font-weight: 300;
}
:root {
--main--color: #FF4E4E;
--main--color--hover: rgb(250, 0, 0);
--nav--size: 1.5em;
--footer--size: 1.125em;
}
/* when to eat */
.meals {
margin-top: 80px;
text-align: left;
/* grid */
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 20px;
}
#lunch {
display: none;
}
.button-container {
margin-top: 30px;
width: 60%;
/* grid */
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
grid-gap: 20px;
}
.button-basics {
background-color: var(--main--color);
width: 100px;
height: 30px;
border-radius: 20px;
color: white;
padding: 5px;
text-align: center;
}
.button-basics:hover {
background-color: var(--main--color--hover);
cursor: pointer;
}
.meals>img {
width: 100%;
}
<!-- meal times -->
<!-- breakfast -->
<div id="breakfast" class='meals'>
<img src="images/breakfast.jpg" alt="">
<div class="description">
<h2>Breakfast</h2>
<p>The most important meal of the day, right? Not
exactly. Since you are an athlete training and
eating constantly, breakfast can possibly mean
twice a day depending on your workouts. However,
it is still hugely important to refuel after any
early morning workouts with a filling and hearty
meal
</p>
</div>
</div>
<!-- lunch -->
<div id="lunch" class='meals'>
<img src="images/lunch.jpg" alt="">
<div class="description">
<h2>Lunch</h2>
<p>The most important meal of the day, right? Not
exactly. Since you are an athlete training and
eating constantly, breakfast can possibly mean
twice a day depending on your workouts. However,
it is still hugely important to refuel after any
early morning workouts with a filling and hearty
meal
</p>
</div>
</div>
<!-- meal buttons -->
<div class='button-container'>
<div id="breakfastButton" class='button-basics'>Breakfast</div>
<div id="lunchButton" class='button-basics'>Lunch</div>
<div class='button-basics'>Dinner</div>
<div class='button-basics'>Snacks</div>
</div>
</div>
关于javascript - 如何使用 CSS Grid 和 Javascript 制作幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55369045/