css-50 Projects in 50 Days(2)

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="steps_wrapper ">
            <div class="step step_active">1</div>
            <div class="step">2</div>
            <div class="step">3</div>
            <div class="step">4</div>
            <div class="line transition400" id="line"></div>
        </div>
        <div class="btn_wrapper transition400">
            <button class="btn " disabled id="pre">上一步</button>
            <button class="btn " id="next">下一步</button>

        </div>
    </div>
    <script src="./index.js"></script>
</body>

</html>

css

$primary: #24acf2;

@mixin center {
    display: flex;
    align-items: center;
    justify-content: center;
}

.transition400 {
    transition: all 400ms ease;
}

.transition500 {
    transition: all 500ms ease;
}


.container {
    @include center();
    flex-direction: column;
    height: 100vh;

    .steps_wrapper {
        display: flex;
        gap: 50px;
        position: relative;

        &::after,
        .line {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 100%;
            height: 3px;
            background-color: #eee;
            z-index: -2;
        }

        .line {
            width: 0;
            background-color: $primary;
            z-index: -1;
        }


        .step {
            border-radius: 50%;
            border: 2px solid #e0e0e0;
            box-sizing: border-box;
            @include center();
            width: 30px;
            height: 30px;
            font-size: 14px;
            color: #999;
            background-color: #fff;
        }

        .step_active {
            background-color: $primary;
            color: #fff;
            border-color: $primary;
            transition: 0.4s 0.2s all ease;
        }
    }

    .btn_wrapper {
        display: flex;
        gap: 20px;
        margin-top: 36px;

        .btn {
            width: 90px;
            height: 32px;
            background-color: $primary;
            border: 1px solid $primary;
            border-radius: 8px;
            color: #fff;
            cursor: pointer;
        }

        .btn:active {
            transform: scale(0.98);
            opacity: 0.9;
        }

        .btn:disabled {
            cursor: not-allowed;
            background-color: #eee;
            border-color: #eee;
        }
    }
}

js

const state = {
    pre: document.getElementById('pre'),
    next: document.getElementById('next'),
    line: document.getElementById('line'),
    steps: document.querySelectorAll(`.step`),
    len: 4,
    cur: 0,
    process: {
        pre: () => {
            if (state.cur > 0) {
                state.cur--;
            }
        },
        next: () => {
            if (state.cur < state.len) {
                state.cur++;
            }
        }
    }
}


state.pre.onclick = () => update('pre')
state.next.onclick = () => update('next')
function update(type) {
    // 增减操作
    state.process[type]()
    // 禁用上一步,下一步
    state.pre.disabled = false;
    state.next.disabled = false;
    if (state.cur == 0) {
        state.pre.disabled = true
    }
    if (state.cur + 1 == state.len) {
        state.next.disabled = true
    }
    // 设置样式
    setStyle()
}
function setStyle() {
    const step_actives = document.querySelectorAll('.step_active');
    step_actives.forEach(item => item.classList.remove('step_active'));
    state.line.style.width = `${(state.cur) * 33}%`;
    state.steps.forEach((item, i) => {
        if (i <= state.cur) {
            item.classList.add('step_active');
        }
    })
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS-in-CSS是一种前端开发技术,也被称为CSS嵌套或CSS模块化。它的主要思想是将CSS样式定义在组件级别,使得样式与组件的逻辑和结构紧密关联,提高代码的可维护性和可重用性。 在传统的CSS开发中,样式是全局共享的,容易造成样式冲突和难以维护。而CSS-in-CSS通过将样式封装在组件内部,实现了样式的局部作用域。这样一来,每个组件都可以拥有自己独立的样式规则,不会相互干扰。 CSS-in-CSS有多种实现方式,其中比较常见的有以下几种: 1. CSS Modules:CSS Modules是一种使用Webpack等构建工具实现的CSS-in-CSS方案。它通过给每个CSS类名添加唯一的哈希值,实现了样式的局部作用域。在使用时,可以通过import语法引入CSS文件,并在组件中使用对应的类名。 2. CSS-in-JS:CSS-in-JS是一种将CSS样式直接写在JavaScript代码中的方式。它通过使用JavaScript对象来描述样式规则,并在运行时动态生成对应的CSS。常见的CSS-in-JS库有styled-components、Emotion等。 3. CSS Nesting:CSS Nesting是一种提案,旨在原生CSS中实现嵌套样式的语法。它使用类似于Sass的嵌套语法,可以更方便地描述组件内部的样式关系。目前,CSS Nesting还处于实验阶段,尚未得到所有浏览器的广泛支持。 总的来说,CSS-in-CSS是一种将CSS样式与组件紧密结合的开发方式,可以提高代码的可维护性和可重用性。不同的实现方式适用于不同的项目需求和开发环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值