这或许是
一番大事业的开端!

HTML&CSS :如此丝滑优雅的导航栏

这段代码创建了一个动态的导航栏,通过 CSS 技术实现了按钮的激活和悬停效果,以及动态背景效果,为页面添加了视觉吸引力和用户交互体验。


演示效果

图片

HTML&CSS

<!DOCTYPE html>
<htmllang="en">

<head>
    <metacharset="UTF-8">
    <metaname="viewport"content="width=device-width, initial-scale=1.0">
    <title>3BBS.CN 站长论坛</title>
    <style>
        body {
            margin0;
            padding0;
            background-color#075985;
            display: flex;
            align-items: center;
            justify-content: center;
            height100vh;
        }

        nav {
            --_width: 50px;
            --_padding: 1rem;
            --_speed: 300ms;
            --_f-size: 1.5rem;
            --_clr-borderrgba(255255255, .3);
            --_clr-bg-rgb: 2623;
            --_clr-f: rgb(255255255);

            display: flex;
            border1px solid var(--_clr-border);
            border-radius20px;
            padding-inlinevar(--_padding);
            background-colorrgb(var(--_clr-bg-rgb));
            position: relative;
            isolation: isolate;
        }

        @media (min-width:600px) {
            nav {
                --_width: 100px;
                --_f-size: 2rem;
            }
        }

        button {
            border: none;
            padding: none;
            background: transparent;
            colorvar(--_clr-f);
            cursor: pointer;
            font-sizevar(--_f-size) !important;
            widthvar(--_width);
            aspect-ratio: 3/2;
            opacity0.5;
            transition:
                opacity var(--_speed) ease-in-out,
                width var(--_speed) ease-in-out;
            display: grid;
            place-content: center;
        }

        button>span {
            scale: var(--_speed) ease-in-out;
            font-size14px;
        }

        button.active,
        button:hover {
            opacity1;
        }

        button.active>span,
        button:hover>span {
            scale: 1.15;
            pointer-events: none;
        }

        nav::before,
        nav::after {
            content'';
            position: absolute;
            top: -25%;
            leftvar(--_padding);
            widthvar(--_width);
            aspect-ratio: 1;
            z-index: -1;
            border-top1px solid var(--_clr-border);
            border-bottom1px solid var(--_clr-border);
            border-radius999px;
            transformtranslateX(calc(var(--_x, 2) * 100%));
            transition:
                transform var(--_speed) ease-in-out,
                width var(--_speed) ease-in-out,
                opacity var(--_speed) ease-in-out;
        }

        nav::before {
            --_x: var(--_x-active);
            background-colorrgb(var(--_clr-bg-rgb));
        }

        nav::after {
            --_x: var(--_x-hover);
            background-colorrgba(var(--_clr-bg-rgb), .4);
            opacity0;
        }

        nav:has(button:nth-child(1).active)::before {
            --_x-active: 0;
        }

        nav:has(button:nth-child(2).active)::before {
            --_x-active: 1;
        }

        nav:has(button:nth-child(3).active)::before {
            --_x-active: 2;
        }

        nav:has(button:nth-child(4).active)::before {
            --_x-active: 3;
        }

        nav:has(button:nth-child(5).active)::before {
            --_x-active: 4;
        }

        nav:has(button:nth-child(1):hover)::after {
            --_x-hover: 0;
            opacity1;
        }

        nav:has(button:nth-child(2):hover)::after {
            --_x-hover: 1;
            opacity1;
        }

        nav:has(button:nth-child(3):hover)::after {
            --_x-hover: 2;
            opacity1;
        }

        nav:has(button:nth-child(4):hover)::after {
            --_x-hover: 3;
            opacity1;
        }

        nav:has(button:nth-child(5):hover)::after {
            --_x-hover: 4;
            opacity1;
        }
    </style>
</head>

<body>
    <nav>
        <buttontype="button"><spanclass="">首页</span></button>
        <buttontype="button"><spanclass="">钱包</span></button>
        <buttontype="button"><spanclass=" active">金融</span></button>
        <buttontype="button"><spanclass="">商城</span></button>
        <buttontype="button"><spanclass="">我的</span></button>
    </nav>
    <script>
        document.querySelector("nav").addEventListener("click", (e) => {
            if (e.target.tagName == "BUTTON") {
                document.querySelector("nav .active").classList.remove("active");
                e.target.classList.add("active");
            }
        });
    </script>
</body>

</html>

HTML 结构

  • body: 包含页面的可见内容。
  • nav: 创建一个 nav 元素,用于包含导航栏的各个按钮。
  • 五个 button: 每个 button 代表一个导航按钮,包含一个 span 元素显示按钮文本。
  • span: 显示按钮的文本内容。

CSS 样式

  • body: 设置页面的边距、填充、背景色、显示方式和对齐方式。
  • nav: 设置导航栏的样式,包括宽度、内边距、边框、圆角、背景色和位置。
  • @media (min-width: 600px): 媒体查询,当屏幕宽度大于 600px 时,调整导航栏的宽度和字体大小。
  • button: 设置按钮的样式,包括边框、背景、颜色、光标、字体大小、宽度、比例、透明度和过渡效果。
  • button>span: 设置按钮文本的样式,包括缩放和字体大小。
  • button.active, button:hover: 设置按钮在激活或悬停时的样式,包括透明度。
  • button.active>span, button:hover>span: 设置按钮文本在激活或悬停时的样式,包括缩放。
  • nav::before, nav::after: 设置导航栏的伪元素,用于创建动态背景效果。
  • nav::before: 设置导航栏的背景效果,包括位置、宽度、比例、边框、圆角和背景色。
  • nav::after: 设置导航栏的悬停背景效果,包括位置、宽度、比例、边框、圆角和背景色。
  • nav:has(button:nth-child(n).active)::before: 设置导航栏的背景效果,根据激活的按钮位置调整。
  • nav:has(button:nth-child(n):hover)::after: 设置导航栏的悬停背景效果,根据悬停的按钮位置调整。

JavaScript 部分

  • 添加一个事件监听器,当点击导航栏中的按钮时,移除当前激活按钮的 active 类,并为被点击的按钮添加 active 类。
赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《HTML&CSS :如此丝滑优雅的导航栏》
文章链接:https://www.wzdao.com/1205.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册