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');
    })
}
相关推荐
摆烂工程师3 分钟前
今天 Cloudflare 全球事故,连 GPT 和你的网站都一起“掉线”了
前端·后端·程序员
拉不动的猪6 分钟前
一文搞懂:localhost和局域网 IP 的核心区别与使用场景
前端·javascript·面试
亿元程序员33 分钟前
你支持游戏内显示电量、信号或时间吗?
前端
阿珊和她的猫1 小时前
HTTP:Web 世界的基石协议详解
前端·网络协议·http
未来之窗软件服务1 小时前
自建开发工具IDE(二)文件托拽读取——东方仙盟炼气期
开发语言·前端·javascript·仙盟创梦ide·东方仙盟
conkl1 小时前
构建健壮的前端请求体系:从 HTTP 状态码到 Axios 实战
前端·网络协议·http
g***B7381 小时前
前端组件设计模式,复用与扩展
前端·设计模式
chxii1 小时前
第六章:MySQL DQL 表之间的关系 自连接 一对一、一对多、多对一、多对多
java·前端·mysql
U***49831 小时前
前端性能优化插件,图片压缩与WebP转换
前端
c***V3232 小时前
前端构建工具发展,esbuild与swc性能
前端