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');
    })
}
相关推荐
用户69371750013845 小时前
DeepSeek 调价正式生效:一夜涨 11 倍,靠低价薅羊毛的日子结束了
前端·人工智能·后端
半仙er5 小时前
第一周01天:this 指向与 call / apply / bind
前端
小帅不太帅5 小时前
给大家推荐一个特别好用的专为 AI Agent 打造的最快浏览器
前端·agent·浏览器
半仙er5 小时前
第一周03天 事件循环与宏任务 / 微任务
前端
做前端的娜娜子5 小时前
#浏览器存储方案:localStorage、sessionStorage 与 Cookie
前端·面试·掘金·金石计划
用户921080262865 小时前
1. Ant Design X Vue 项目介绍:结构、组件和启动方式
前端
摸鱼研究员5 小时前
neverthrow,ts 中优雅的异常处理方案
前端·javascript
jingchao19985 小时前
Cannot read properties of null (reading ‘insertBefore‘)
前端·javascript·vue.js
计算机魔术师5 小时前
新文章
前端
爱跳舞的烤冷面5 小时前
自学嵌入式第N天(Linux篇——文件编程)
java·开发语言·前端