CSS动画(动态导航栏)

简介: CSS动画(动态导航栏)

1.项目简介

一个具有创意的导航菜单不仅能为你的大作业增色,还能展示你的技术实力。本文将分享一系列常用于期末大作业的CSS动画导航效果,这些效果不仅外观酷炫,而且易于实现。我们提供了一键复制的代码,让你能够快速集成到自己的项目中,节省时间,提高效率,让你的期末大作业脱颖而出。

2.完整代码

HTML

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Title</title>  
    <link rel="stylesheet" type="text/css" href="6_21.css">  
</head>  
<body>  
<nav>  
    <!-- 导航栏按钮,用于主要页面导航 -->  
    <nav>  
        <!-- 主页按钮,包含文本和图标表示 -->  
        <button type="button" title="Home">  
            <span>Home</span>  
            <!-- 使用Material Symbols Outline图标的HTML代码,用于增强视觉效果 -->  
            <span class="material-symbols-outlined" aria-hidden="true">首页</span>  
            <!-- SVG图形用于呈现"Home"文本的视觉效果,aria-hidden属性表示此元素不用于屏幕阅读器 -->  
            <svg viewBox="0 0 300 300" aria-hidden="true">  
                <g>  
                    <!-- 使用textPath元素将文本沿着路径绘制 -->  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath">Home</textPath>  
                    </text>  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath" startOffset="50%">Home</textPath>  
                    </text>  
                </g>  
            </svg>  
        </button>  
        <!-- 关于按钮,包含文本和图标表示 -->  
        <button type="button">  
            <span>About</span>  
            <span class="material-symbols-outlined" aria-hidden="true">信息</span>  
            <svg viewBox="0 0 300 300" aria-hidden="true">  
                <g>  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath">About</textPath>  
                    </text>  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath" startOffset="50%">About</textPath>  
                    </text>  
                </g>  
            </svg>  
        </button>  
        <!-- 服务按钮,包含文本和图标表示 -->  
        <button type="button">  
            <span>Services</span>  
            <span class="material-symbols-outlined" aria-hidden="true">服务</span>  
            <svg viewBox="0 0 300 300" aria-hidden="true">  
                <g>  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath">Services</textPath>  
                    </text>  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath" startOffset="50%">Services</textPath>  
                    </text>  
                </g>  
            </svg>  
        </button>  
        <!-- 联系按钮,包含文本和图标表示 -->  
        <button type="button">  
            <span>concat</span>  
            <span class="material-symbols-outlined" aria-hidden="true">联系</span>  
            <svg viewBox="0 0 300 300">  
                <g>  
                    <text fill="currentColor" aria-hidden="true">  
                        <textPath xlink:href="#circlePath">Contact</textPath>  
                    </text>  
                    <text fill="currentColor">  
                        <textPath xlink:href="#circlePath" startOffset="50%">Contact</textPath>  
                    </text>  
                </g>  
            </svg>  
        </button>  
    </nav>  
  
    <!-- SVG 模板与动态文本 -->  
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  
         viewBox="0 0 300 300" width="0" height="0">  
        <defs>  
            <path id="circlePath" d="M 150, 150 m -50, 0 a 50,50 0 0,1 100,0 a 50,50 0 0,1 -100,0" />  
        </defs>  
    </svg>  
</body>  
</html>

CSS

* {  
    margin: 0;  
    padding: 0;  
    box-sizing: border-box;  
}  
/* 重置按钮样式 */button {  
    appearance: none;  
    background-color: transparent;  
    border: none;  
    cursor: pointer;  
    outline: none;  
    padding: 0;  
    margin: 0;  
    font-family: inherit;  
    font-size: inherit;  
    color: inherit;  
    text-decoration: none;  
    text-transform: none;  
    line-height: normal;  
    overflow: visible;  
}  
  
body {  
    min-height: 100svh;  
    background-color: rgb(15, 23, 42);  
    color: white;  
    display: grid;  
    place-content: center;  
    font-size: 1rem;  
    font-family: system-ui;  
}  
  
nav {  
    --_clr-txt: rgb(255, 255, 255);  
    --_clr-txt-svg: rgb(147, 158, 184);  
    --_ani-speed: 6s;  
    /* 旋转文本的速度 */  
    display: flex;  
    /*flex-wrap: wrap;*/  
    gap: 1rem;  
    font-size: 1.4rem;  
}  
  
nav>button {  
    position: relative;  
    display: grid;  
    place-content: center;  
    grid-template-areas: 'stack';  
    padding: 0 1.5rem;  
    text-transform: uppercase;  
    font-weight: 300;  
}  
  
/* 将按钮元素堆叠在一起 */nav>button>span {  
    transition: all 300ms ease-in-out;  
    grid-area: stack;  
}  
  
/* 导航图标 */nav>button>span:last-of-type {  
    margin-top: 0.25rem;  
    transform: scale(0);  
    transition-delay: 0ms;  
    border-radius: 50%;  
}  
  
/* 悬停 - 隐藏文本 */nav>button:focus-visible>span:first-of-type,  
nav>button:hover>span:first-of-type {  
    transform: scale(0);  
}  
  
/* 悬停 - 显示图标 */nav>button:focus-visible>span:last-of-type,  
nav>button:hover>span:last-of-type {  
    transform: scale(1);  
}  
  
/* 导航 SVG 圆形文本 */nav>button>svg {  
    position: absolute;  
    width: 200px;  
    height: 200px;  
    top: 50%;  
    left: 50%;  
    transform: translate(-50%, -50%);  
    transform-origin: center;  
    opacity: 0;  
    text-transform: uppercase;  
    transition: all 300ms ease-in-out;  
    color: var(--_clr-txt-svg);  
}  
  
/* 悬停 - 显示旋转的 SVG */nav>button:focus-visible>svg,  
nav>button:hover>svg {  
    transform: translate(-50%, -50%) scale(1);  
    opacity: 1;  
    transition-delay: 150ms;  
    transition: all 300ms ease-in-out;  
}  
  
/*  
@supports (-webkit-touch-callout: none) {  
/* 特定于 iOS 设备 * /button svg {  
/* 调整 iOS 设备的位置 * /translate: -50% -50%;  
animation: rotate var(--_ani-speed) linear infinite;  
}  
}  
@supports not (-webkit-touch-callout: none) {  
*/  
  
  
button svg g {  
    transform-origin: center;  
    animation: rotate var(--_ani-speed) linear infinite;  
}  
  
  
@keyframes rotate {  
    0% {  
        transform: rotate(0deg);  
    }  
  
    100% {  
        transform: rotate(360deg);  
    }  
}
相关文章
|
前端开发 JavaScript
CSS 过渡和动画
CSS过渡和动画是用于为网页元素添加动态效果的两种重要技术
604 143
|
7月前
|
自然语言处理 前端开发 JavaScript
用 通义灵码 一键生成“水波纹按钮”,连 CSS 动画都不用自己写了!
通义灵码是一款智能编程辅助工具,它可以根据自然语言指令自动生成高质量的代码。例如,只需输入“生成一个按钮,点击时带水波纹动画,模拟 Material Ripple 效果”,它就能生成具备完整交互逻辑、CSS 动画和良好性能的按钮组件。不仅如此,它还支持拓展功能,如长按触发提示、添加图标等,并能自动优化样式适配不同场景。通过通义灵码,开发者可以大幅提升效率,专注于创意实现,而不必纠结于繁琐的代码细节。
|
4月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(九):强势分析Animation动画各类参数;从播放时间、播放方式、播放次数、播放方向、播放状态等多个方面,完全了解CSS3 Animation
Animation属性 css3为Animation动画提供的几个属性如下: 属性名 属性值 animation-name 指定动画名称,该属性指定一个已有的关键帧定义。 animation-duration 指定动画持续时间。 animation-timing-funtion 指定动画变化速度。 animation-delay 指定动画延迟多长时间才开始执行。 animation-iteration-count 指定动画的循环执行次数。 animation:这是一个复合属性。
358 2
css3 svg制作404页面动画效果HTML源码
css3 svg制作404页面动画效果HTML源码
267 34
jQuery+CSS3实现404背景游戏动画源码
jQuery+CSS3实现404背景游戏动画源码
239 22
纯css3实现的百分比渐变进度条加载动画源码
纯css3实现的百分比渐变进度条加载动画特效源码
254 31
|
Web App开发 移动开发 JavaScript
纯CSS3+SVG实现的节日庆祝五彩纸屑动画效果源码
这是一款基于纯CSS3+SVG实现的节日庆祝五彩纸屑动画效果源码。画面中左下角是一个圆锥形礼炮卡通效果,呈现出节日庆祝时礼花爆破、五彩纸屑纷飞的动画特效。整体动画效果采用纯css3+svg实现,没有引入任何外部图形或js脚本元素。建议使用支持HTML5与css3效果较好的火狐(Firefox)或谷歌(Chrome)等浏览器预览本源码。
297 6
|
前端开发 JavaScript UED
CSS滚动效果和视差滚动的原理、应用及其对用户体验的影响。从平滑滚动到元素跟随,再到滚动触发动画
本文探讨了CSS滚动效果和视差滚动的原理、应用及其对用户体验的影响。从平滑滚动到元素跟随,再到滚动触发动画,这些效果增强了页面的吸引力和互动性。视差滚动通过不同层次元素的差异化移动,增加了页面的深度感和沉浸感。文章还讨论了实现方法、性能优化及案例分析,旨在为设计师和开发者提供实用指导。
377 7
CSS3制作的聚光灯下倒影文字选装动画特效源码
CSS3聚光灯下倒影文字特效是一段基于CSS3实现的聚光灯下带倒影的文字旋转动画效果代码,具有真实的视觉感,同时文字还会在旋转过程中显示出灯光的反射效果,很有意思,欢迎对此段代码感兴趣的朋友前来下载使用。
163 6
纯css3加载loading发光变色动画代码
纯css3加载loading发光变色动画特效代码是一款基于css3 keyframes属性实现的发光变色圆点串联旋转loading加载动画
226 2

热门文章

最新文章

  • 1
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(九):强势分析Animation动画各类参数;从播放时间、播放方式、播放次数、播放方向、播放状态等多个方面,完全了解CSS3 Animation
    358
  • 2
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(八):学习transition过渡属性;本文学习property模拟、duration过渡时间指定、delay时间延迟 等多个参数
    279
  • 3
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(七):学习ransform属性;本文学习 rotate旋转、scale缩放、skew扭曲、tanslate移动、matrix矩阵 多个参数
    245
  • 4
    (CSS)使用Flex布局,帮助你快速了解各种基本的Flex布局属性以及帮你让元素快速达到布局中的指定位置!
    169
  • 5
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(六):全方面分析css的Flex布局,从纵、横两个坐标开始进行居中、两端等元素分布模式;刨析元素间隔、排序模式等
    368
  • 6
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(五):背景属性;float浮动和position定位;详细分析相对、绝对、固定三种定位方式;使用浮动并清除浮动副作用
    514
  • 7
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(四):元素盒子模型;详细分析边框属性、盒子外边距
    370
  • 8
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(三):元素继承关系、层叠样式规则、字体属性、文本属性;针对字体和文本作样式修改
    168
  • 9
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(二):CSS伪类:UI伪类、结构化伪类;通过伪类获得子元素的第n个元素;创建一个伪元素展示在页面中;获得最后一个元素;处理聚焦元素的样式
    354
  • 10
    【CSS】前端三大件之一,如何学好?从基本用法开始吧!(一):CSS发展史;CSS样式表的引入;CSS选择器使用,附带案例介绍
    319