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');
    })
}
相关推荐
小刘鸭地下城32 分钟前
优雅表格设计:CSS 美化技巧详解
css
虫虫rankourin34 分钟前
在 create-react-app (CRA) 创建的应用中使用 react-router-dom v7以及懒加载的使用方法
前端·react.js
小刘鸭地下城36 分钟前
Web安全必备:关键 HTTP 标头解析
前端
yddddddy38 分钟前
html基本知识
前端·html
荣达1 小时前
koa洋葱模型理解
前端·后端·node.js
小刘鸭地下城1 小时前
网页深色模式完整实现:从响应式设计到系统主题联动
css
reembarkation2 小时前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
KenXu2 小时前
F2C-PTD工具将需求快速转换为代码实践
前端
给月亮点灯|2 小时前
Vue3基础知识-setup()、ref()和reactive()
前端·javascript·vue.js
芜青2 小时前
【Vue2手录12】单文件组件SFC
前端·javascript·vue.js