切换按钮组动画效果

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>
相关推荐
kite01215 小时前
浏览器工作原理06 [#]渲染流程(下):HTML、CSS和JavaScript是如何变成页面的
javascript·css·html
超级土豆粉8 小时前
CSS3 的特性
前端·css·css3
我在北京coding10 小时前
6套bootstrap后台管理界面源码
前端·bootstrap·html
超级土豆粉14 小时前
CSS 预处理器与工具
前端·css
技术小丁16 小时前
使用 HTML + JavaScript 实现自定义富文本编辑器开发实践(附完整代码)
前端·javascript·html
代码搬运媛19 小时前
React 中 HTML 插入的全场景实践与安全指南
安全·react.js·html
lljss20201 天前
html文字红色粗体,闪烁渐变动画效果,中英文切换版本
css·html·css3
疯狂的沙粒1 天前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
小妖6661 天前
html 滚动条滚动过快会留下边框线
前端·html
CodeBlossom1 天前
javaweb -html -CSS
前端·javascript·html