我在一个投资组合网站上工作,使用react 和tailwind CSS.当我设计导航栏时,我想要它的悬停效果,所以我复制了以下代码的悬停效果:

codepen for hover-effect

但是一旦我复制了css,我就相应地编辑了我的css.这就是CSS:

.translucent {
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 50px;
    box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.2);
}

.nav{
    color: #fff;
    padding: 15px;
    margin-left: 15px;
    margin-right: 15px;
}

.nav * {
  box-sizing: border-box;
  transition: all .35s ease;
}

.nav li {
  display: inline-block;
  list-style: outside none none;
  margin: .5em 1em;
  padding: 0;
}

.nav a {
  padding: .5em .8em;
  color: rgba(255,255,255,.5);
  position: relative;
  text-decoration: none;
  font-size: 20px;
}

.nav a::before,
.nav a::after {
  content: '';
  height: 14px;
  width: 14px;
  position: absolute;
  transition: all .35s ease;
  opacity: 0;
}

.nav a::before {
  content: '';
  right: 0;
  top: 0;
  border-top: 3px solid #ACDDDE;
  border-right: 3px solid #ACDDDE;
  transform: translate(-100%, 50%);
}

.nav a:after {
  content: '';
  left: 0;
  bottom: 0;
  border-bottom: 3px solid #ACDDDE;
  border-left: 3px solid #ACDDDE;
  transform: translate(100%, -50%)
}

.nav a:hover:before,
.nav a:hover:after{
  transform: translate(0,0);
  opacity: 1;
}

.nav a:hover {
  color: #fff;
}

.nav a:visited {
  color: #fff;
}

这是JSX:

import React from "react";
import "./header.css";
import logo from "../../assets/logo.png";

const Header = () => {
    return (
        <div className="translucent flex justify-between nav items-baseline fixed">
            <div className="h-12 sm:hidden px-6">
                <img src={logo} alt="Harshal Gunjal" />
            </div>
            <div className="text-white text-2xl font-bold hidden sm:block px-6 cursor-pointer">
                Harshal Gunjal
            </div>
            <div className="px-6">
                <ul className="flex items-center justify-center gap-8">
                    <li className="text-opacity-90 text-white text-lg font-bold hover:text-opacity-100 transition-all hover:scale-125 cursor-pointer">
                        <a href="#home">Home</a>
                    </li>
                    <li className="text-opacity-90 text-white text-lg font-bold hover:text-opacity-100 transition-all hover:scale-125 cursor-pointer">
                        <a href="#about">About</a>
                    </li>
                    <li className="text-opacity-90 text-white text-lg font-bold hover:text-opacity-100 transition-all hover:scale-125 cursor-pointer">
                        <a href="#portfolio">Portfolio</a>
                    </li>
                    <li className="text-opacity-90 text-white text-lg font-bold hover:text-opacity-100 transition-all hover:scale-125 cursor-pointer">
                        <a href="#resume">Resume</a>
                    </li>
                    <li className="text-opacity-90 text-white text-lg font-bold hover:text-opacity-100 transition-all hover:scale-125 cursor-pointer">
                        <a href="#contact">Contact</a>
                    </li>
                </ul>
            </div>
        </div>
    );
};

export default Header;

在应用固定标记之前,导航栏如下所示:

the navber before I applied fixed tag

在我将固定标记应用到导航栏之后,它变成了这样:

navbar after I added the fixed class

您可以在GitHub中找到代码:

github source code

我怎么才能把导航栏固定在上面而不换掉它呢?谢谢你的帮助.

我try 更改css文件中的css,而不是tailwind ‘修正’类,但不起作用.我也试图更改导航中的位置属性,但每次我更改任何内容时,导航栏都会被销毁.

推荐答案

到您的代码中

<div className="translucent flex justify-between nav items-baseline fixed">

添加

top-0 left-0 mx-auto inset-x-0

结果

<div className="translucent flex justify-between nav items-baseline fixed top-0 left-0 mx-auto inset-x-0">

Reactjs相关问答推荐

404使用GitHub操作部署Next.js应用程序时出错

Reaction-Query&-使用Mutation触发成功,而应该触发错误

如何使图像zoom 并移动到点击点

在Map()完成React.js中的多个垃圾API请求后执行函数

对搜索引擎优化来说,react 头盔的异步足够了吗?

如何在MUI x图表条形图上放置圆角?

Github 操作失败:发布 NPM 包

如何在我的 React 应用程序中仅显示我的 Register 组件

console.logs 没有显示在浏览器控制台上,但显示在我的终端上

如何在 ChartJS 中创建此图表?在 NEXTjs 和 TSX 中

如何在我的自定义主题中样式化MUI TreeItem组件

使用reactrouterdom时显示"对象无效作为React子组件"错误

如何在react 中正确销毁上下文?

在 React 中将路径与查询变量匹配

react-markdown 不渲染 Markdown

getAttribute 函数并不总是检索属性值

react-admin useGetIdentity 只返回全名,id 未定义 头像未定义

哪个是创建 React 应用程序的更好方法?

找不到元素 - 这是参考问题吗?

仅react material 表前端分页