发布时间:2022-08-11 文章分类:编程知识 投稿人:王小丽 字号: 默认 | | 超大 打印

?前言

?分析

[前端小项目] 扩展卡片 Expanding Cards(50projects50days)

?布局

?文字样式

?点击事件

?代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>expanding-cards</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body{
            min-height: 100vh;
        }
        .container{
            min-height: 100vh;
            width: 100%;
            /* 使用flex布局 */
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0 60px;
        }
        .item{
            width: 10%;             /* 小卡片统一的宽度 */
            height: 80vh;
            margin: 0 1rem;
            position: relative;     /* 卡片设置相对定位 */
            border-radius: 2rem;
            background-repeat: no-repeat;
            background-size: cover;
            cursor: pointer;
            transition: all ease-in-out 0.5s;
        }
        /* 卡片的背景图片源自原项目 */
        .item:nth-of-type(1){
            background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
        }
        .item:nth-of-type(2){
            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');
        }
        .item:nth-of-type(3){
            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');
        }
        .item:nth-of-type(4){
            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');
        }
        .item:nth-of-type(5){
            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');
        }
        /* 大卡片的宽度 */
        .focus{
            margin: 0 1rem;
            width: 50%;
        }
        .item p{
            /* 小卡片内部文字是不可见的 */
            opacity: 0;
        }
        .focus p{
            /* 大卡片内部文字可见 */
            opacity: 1;
            transition: all ease-in-out 0.5s;
            color: #fff;
            font-size: 32px;
            font-weight: bold;
            /* 文字使用绝对定位,并设置bottom和left,将文字置于卡片左下角 */
            position: absolute;
            bottom: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item focus"><p>Explore the world</p></div>
        <div class="item"><p>Wild Forest</p></div>
        <div class="item"><p>Sunny Beach</p></div>
        <div class="item"><p>City on Winter</p></div>
        <div class="item"><p>Mountains - Clouds</p></div>
    </div>
</body>
<script>
    window.onload = function(){
        // 获取DOM元素
        const container  = document.getElementsByClassName('container')[0];
        const item = document.getElementsByClassName('item');
        //绑定点击事件(事件委托)
        container.onclick = function(e){
            e = window.event || e;      //兼容firefox
            const target = e.target;    //从target属性获取到被点击的元素
            //首先将所有卡片设置为小卡片
            for(let i=0;i<item.length;i++){
                item[i].classList.remove('focus');
            }
            //被点击的卡片额外设置成大卡片
            target.classList.add("focus");
        }
    }
</script>
</html>

参考文章