Как сделать слайд-шоу с использованием CSS Grid и Javascript - PullRequest
3 голосов
/ 27 марта 2019

Я пытаюсь создать раздел на своем веб-сайте для отображения изображения и абзаца с заголовком в зависимости от выбранной кнопки. Например, при щелчке на ланч отображаются параметры ланча и исчезают параметры завтрака по умолчанию.

Я использовал CSS Grid и javascript для достижения этой цели, однако, когда я нажимаю на кнопки, CSS Grid теряется, и я не уверен, почему. Я использую 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>

enter image description here

enter image description here

Как видите, верхнее изображение - это мой желаемый конечный результат, тогда как нижнее изображение - это то, что происходит, когда я нажимаю кнопку обеда, а не то, чего я пытаюсь достичь.

1 Ответ

2 голосов
/ 27 марта 2019

Хорошо, это должно быть очень легко исправить.Проверьте обновленные функции showBreakfast() и showLunch().На шоу вместо того, чтобы изменять свойство display на 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>
...