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');
    })
}
相关推荐
再学一点就睡4 小时前
前端网络实战手册:15个高频工作场景全解析
前端·网络协议
C_心欲无痕5 小时前
有限状态机在前端中的应用
前端·状态模式
C_心欲无痕5 小时前
前端基于 IntersectionObserver 更流畅的懒加载实现
前端
candyTong5 小时前
深入解析:AI 智能体(Agent)是如何解决问题的?
前端·agent·ai编程
柳杉5 小时前
建议收藏 | 2026年AI工具封神榜:从Sora到混元3D,生产力彻底爆发
前端·人工智能·后端
weixin_462446236 小时前
使用 Puppeteer 设置 Cookies 并实现自动化分页操作:前端实战教程
运维·前端·自动化
CheungChunChiu6 小时前
Linux 内核动态打印机制详解
android·linux·服务器·前端·ubuntu
GIS之路7 小时前
GDAL 创建矢量图层的两种方式
前端
小目标一个亿7 小时前
Windows平台Nginx配置web账号密码验证
linux·前端·nginx
rocky1918 小时前
网页版时钟
前端·javascript·html