切换按钮组动画效果

html 复制代码
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按钮切换动画</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }

        .container {
            display: flex;
            /* align-items: center; */
            position: relative;
            width: 300px;
            height: 50px;
            border-radius: 5px;
            /* 圆角更小 */
            overflow: hidden;
            background-color: #EBEBEB;
            /* 背景稍暗 */
            /* padding: 5px; 内边距 */
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }

        .button {
            width: 65px;
            margin: 5px;
            display: block;
            text-align: center;
            height: calc(100% - 10px);
            line-height: 40px;
            border: none;
            border-radius: 5px;
            background-color: transparent;
            cursor: pointer;
            font-size: 14px;
            color: #3B3B3B;
            /* 默认字体颜色为白色 */
            transition: background-color 0.3s, color 0.3s;
            position: relative;
            z-index: 2;
            /* background-color: red; */
        }

        .button:hover {
            background-color: rgba(255, 255, 255, 0.3);
            /* 悬停背景 */
        }

        .indicator {
            position: absolute;
            /* padding: 5px; */
            width: 65px;
            height: calc(100% - 10px);
            margin: 5px;
            background-color: #fff;
            /* 指示器颜色 */
            transition: transform 0.3s ease-in-out;
            z-index: 1;
            border-radius: 10px;
            /* 确保指示器在最下层 */
        }

        /* 为每个按钮添加对应的选中效果 */
        .button.selected {
            /* 选中时背景为白色 */
            color: black;
            /* 选中时字体为黑色 */
        }
    </style>
</head>

<body>

    <div class="container">
        <div class="indicator" id="indicator"></div>
        <span class="button selected" onclick="moveIndicator(0)">按钮 1</span>
        <span class="button" onclick="moveIndicator(1)">按钮 2</span>
        <span class="button" onclick="moveIndicator(2)">按钮 3</span>
        <span class="button" onclick="moveIndicator(3)">按钮 4</span>
    </div>

    <script>
        function moveIndicator(index) {
            const indicator = document.getElementById('indicator');
            const buttons = document.querySelectorAll('.button');
            const offset = index * 75; // 每个按钮的宽度为 75px
            indicator.style.transform = `translateX(${offset}px)`;

            // 更新按钮状态
            buttons.forEach((button, i) => {
                if (i === index) {
                    button.classList.add('selected'); // 添加选中样式
                } else {
                    button.classList.remove('selected'); // 移除其他按钮的选中样式
                }
            });
        }
    </script>

</body>

</html>
相关推荐
读心悦2 小时前
CSS结构性伪类、UI伪类与动态伪类全解析:从文档结构到交互状态的精准选择
css·ui·交互
码农黛兮_464 小时前
CSS3 基础知识、原理及与CSS的区别
前端·css·css3
(((φ(◎ロ◎;)φ)))牵丝戏安5 小时前
根据输入的数据渲染柱形图
前端·css·css3·js
逍遥德6 小时前
CSS可以继承的样式汇总
前端·css·ui
读心悦6 小时前
CSS3 选择器完全指南:从基础到高级的元素定位技术
前端·css·css3
_龙衣7 小时前
将 swagger 接口导入 apifox 查看及调试
前端·javascript·css·vue.js·css3
crazyme_69 小时前
前端自学入门:HTML 基础详解与学习路线指引
前端·学习·html
为美好的生活献上中指11 小时前
java每日精进 5.11【WebSocket】
java·javascript·css·网络·sql·websocket·网络协议
海拥✘11 小时前
CodeBuddy终极测评:中国版Cursor的开发革命(含安装指南+HTML游戏实战)
前端·游戏·html
asqq812 小时前
CSS 中的 ::before 和 ::after 伪元素
前端·css