You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

响应式带动画导航栏无法正常显示问题求助

解决响应式动画导航栏不生效的问题

嘿,我来帮你搞定这个导航栏的问题!你遇到的核心问题是代码用了SCSS(Sass)预处理器语法,但浏览器无法直接识别,所以样式和动画都没生效,只显示了原始的复选框和列表元素。

问题原因

你写的CSS代码里用了SCSS的嵌套特性(比如.navigation &__checkbox这种写法),这是预处理器的专属语法,浏览器只能识别标准的CSS语法,所以这些样式根本没被解析。另外,你用了rem单位,如果没设置根元素字体大小,也可能导致元素尺寸显示异常。

解决方案

不需要额外的CDN,只需要把SCSS转换成标准CSS,或者确保你的代码被编译成CSS后再引入。下面是两种可行的办法:

办法1:直接使用转换后的标准CSS

我已经把你的SCSS代码转换成了浏览器能识别的普通CSS,替换掉你原来的CSS代码即可:

/* 先设置根字体大小,让rem单位更易计算 */
html {
  font-size: 62.5%; /* 1rem = 10px */
}

.navigation__checkbox {
  display: none;
}

.navigation__button {
  background-color: #fff;
  height: 7rem;
  width: 7rem;
  border-radius: 50%;
  position: fixed;
  top: 4rem;
  right: 6rem;
  z-index: 2000;
  box-shadow: 0 1rem 3rem rgba(0, 0, 0, .2);
  text-align: center;
  cursor: pointer;
}

.navigation__background {
  height: 6rem;
  width: 6rem;
  border-radius: 50%;
  position: fixed;
  top: 4.5rem;
  right: 6.5rem;
  background-image: radial-gradient(#2998ff, #5643fa);
  z-index: 1000;
  transition: transform .8s cubic-bezier(0.86, 0, 0.07, 1);
}

.navigation__nav {
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1500;
  opacity: 0;
  width: 0;
  transition: all .8s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.navigation__list {
  list-style: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  width: 100%;
}

.navigation__link:link,
.navigation__link:visited {
  display: inline-block;
  font-size: 3rem;
  font-weight: 300;
  padding: 1rem 2rem;
  color: #fff;
  text-decoration: none;
  text-transform: uppercase;
  background-image: linear-gradient(120deg, transparent 0%, transparent 50%, #fff 50%);
  background-size: 230%;
  transition: all .4s;
}

.navigation__link:hover,
.navigation__link:active {
  background-position: 100%;
  color: #2998ff;
  transform: translateX(1rem);
}

.navigation__checkbox:checked ~ .navigation__background {
  transform: scale(80);
}

.navigation__checkbox:checked ~ .navigation__nav {
  opacity: 1;
  width: 100%;
}

.navigation__icon,
.navigation__icon::before,
.navigation__icon::after {
  width: 3rem;
  height: 2px;
  background-color: #333;
  display: inline-block;
}

.navigation__icon {
  position: relative;
  margin-top: 3.5rem;
}

.navigation__icon::before,
.navigation__icon::after {
  content: "";
  position: absolute;
  left: 0;
  transition: all .2s;
}

.navigation__icon::before {
  top: -.8rem;
}

.navigation__icon::after {
  bottom: -.8rem;
}

.navigation__button:hover .navigation__icon::before {
  top: -1rem;
}

.navigation__button:hover .navigation__icon::after {
  bottom: -1rem;
}

.navigation__checkbox:checked + .navigation__button .navigation__icon {
  background-color: transparent;
}

.navigation__checkbox:checked + .navigation__button .navigation__icon::before {
  transform: rotate(135deg);
  top: 0;
}

.navigation__checkbox:checked + .navigation__button .navigation__icon::after {
  transform: rotate(-135deg);
  bottom: 0;
}

办法2:编译SCSS为CSS

如果你想继续使用SCSS语法,需要把代码编译成CSS:

  • 可以用在线编译器把你的SCSS代码转换成CSS;
  • 本地开发的话,可以安装Node.js的sass包,通过命令行编译;
  • 如果是在CodePen里使用,记得在CSS设置里把预处理器改成SCSS。

验证效果

替换代码后,你应该能看到:

  • 右上角的白色圆形汉堡按钮;
  • 点击按钮后,背景会展开成渐变的全屏遮罩;
  • 导航列表会从透明变为可见,链接有hover动画。

内容的提问来源于stack exchange,提问作者rk_1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 08:57:30