美文网首页
前端新手项目练习50projects50days——展开卡片(e

前端新手项目练习50projects50days——展开卡片(e

作者: 简单一点点 | 来源:发表于2021-12-14 08:40 被阅读0次

本项目介绍一个展开图片的特效。

expanding-cards.png

HTML部分主要是5张图片,这里直接在html中添加了CSS样式,用来添加背景图片,其中每张图片还有一个标题。

<!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="style.css" />
        <title>Expanding Cards</title>
    </head>
    <body>
        <div class="container">
            <div class="panel active" style="background: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
                <h3>Explore The World</h3>
            </div>
            <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
                <h3>Wild Forest</h3>
            </div>
            <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80')">
                <h3>Sunny Beach</h3>
            </div>
            <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')">
                <h3>City on Winter</h3>
            </div>
            <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
                <h3>Mountains - Clouds</h3>
            </div>
        </div>

        <script src="script.js"></script>
    </body>
</html>

JS部分为每个图片添加点击监听事件,当点击图片时为图片所在的元素添加active类,并移除其它图片的active类。

const panels = document.querySelectorAll('.panel')

panels.forEach(panel => {
    panel.addEventListener('click', () => {
        removeActiveClasses()
        panel.classList.add('active')
    })
})

function removeActiveClasses() {
    panels.forEach(panel => {
        panel.classList.remove('active')
    })
}

通过CSS部分我们可以发现,整个页面采用流式布局,普通图片占据0.5,active类所在图片占据5,这样就实现了点击查看当前图片,收缩其他图片。另外最后还做了设备兼容,针对宽度较小的设备隐藏最后两个图片。

* {
    box-sizing: border-box;
}

body {
    font-family: sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
}

.container {
    display: flex;
    width: 90vw;
}

.panel {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    height: 80vh;
    border-radius: 50px;
    color: #fff;
    cursor: pointer;
    flex: 0.5;
    margin: 10px;
    position: relative;
    transition: all 700ms 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: 100vw;
    }

    .panel:nth-of-type(4),
    .panel:nth-of-type(5) {
        display: none;
    }
}
expanding-cards.gif

相关文章

网友评论

      本文标题:前端新手项目练习50projects50days——展开卡片(e

      本文链接:https://www.haomeiwen.com/subject/fwrefrtx.html