css-50 Projects in 50 Days(1)

改变背景图

html

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>改变背景</title>
    <link rel="stylesheet" href="index.css">
</head>

<body>
    <div class="container">
        <div class="pannel active" style="background-image: url('https://picsum.photos/800/400')">
            <span>图1</span>
        </div>
        <div class="pannel" style="background-image: url('https://picsum.photos/800/401')">
            <span>图2</span>
        </div>
        <div class="pannel" style="background-image: url('https://picsum.photos/800/402')">
            <span>图2</span>
        </div>
        <div class="pannel" style="background-image: url('https://picsum.photos/800/403')">
            <span>图4</span>
        </div>
        <div class="pannel" style="background-image: url('https://picsum.photos/800/404')">
            <span>图5</span>
        </div>
    </div>
    <script src="./indes.js"></script>
</body>

</html>

css

css 复制代码
* {
    margin: 0;
    padding: 0;

}

:root {
    --w100: 100%;
    --w100px: 100px;
}

body {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}




.container {

    width: 90vw;
    height: 80vh;
    gap: 24px;
    display: flex;

    align-items: center;
    cursor: pointer;
    transition: all 700ms ease-in;



    .pannel {
        flex: 0.5;
        height: 100%;
        border-radius: 50px;
        position: relative;
        transition: all 700ms ease-in;

        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;

        span {
            position: absolute;
            bottom: 40px;
            left: 30px;
            color: #fff;
            font-weight: bold;
            opacity: 0;
            transition: opacity 0.3s ease-in 0.4s;
        }
    }

    .active {
        transition: all 0.5s;
        flex: 5;

        span {
            opacity: 1;
        }
    }
}

js

javascript 复制代码
const pannels = document.querySelectorAll('.pannel')
console.log(pannels, 'pannels')


pannels.forEach((pannel, index) => {
    pannel.addEventListener('click', () => {
        removeActiveClasses()
        pannel.classList.add('active')
    })
})

function removeActiveClasses() {
    pannels.forEach((pannel, index) => {
        pannel.classList.remove('active');
    })
}
相关推荐
ZC跨境爬虫2 小时前
跟着 MDN 学CSS day_16:(深入掌握背景与边框的艺术)
前端·css·ui·html·tensorflow
道里5 小时前
花了 5 万刀用 AI 写代码之后,这是我的全部经验
前端·人工智能
Royzst5 小时前
xml知识点
java·服务器·前端
IT_陈寒5 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
kyriewen6 小时前
推行AI写代码一年后,Code Review变成了新的加班理由
前端·ai编程·cursor
前端环境观察室6 小时前
给 Agent Browser Workflow 加一层可观测性:Trace、Snapshot 和 Review Queue
前端
柒瑞7 小时前
Superpowers结合Claude code浅实战
前端
Nian.Baikal7 小时前
从零搭建离线地图服务:Nginx + Cesium/Leaflet 实战指南
运维·前端·nginx
前端毕业班7 小时前
uniapp web 灵活控制 style scoped
前端·javascript·vue.js
lichenyang4537 小时前
鸿蒙业务需求实战:AI 问题走马灯卡片实现复盘
前端