简体   繁体   English

根据 Gatsby 中的 url 更改字体颜色

[英]Changing font colour depending on url in Gatsby

I have a simple footer component using tailwindcss我有一个使用 tailwindcss 的简单页脚组件

import React from "react"
import { FaMixcloud, FaFacebookSquare, FaTwitter } from "react-icons/fa"

const Footer = () => {
  return (
    <footer className="w-full fixed bottom-0 px-4 sm:px-10 py-6 flex justify-between items-center">
      <div className="text-white text-xs font-mono uppercase">
        &copy; {new Date().getFullYear()} adam lawther
      </div>
      <nav className="flex items-center">
        <FaMixcloud className="h-6 w-6 mr-4 text-white" />
        <FaFacebookSquare className="h-4 w-4 mr-4 text-white" />
        <FaTwitter className="h-4 w-4 text-white" />
      </nav>
    </footer>
  )
}

export default Footer

On the homepage the text is white, which is what I am after...but on other pages, which dont have a full size image.在主页上,文本是白色的,这就是我所追求的……但在其他页面上,没有完整尺寸的图像。 The font is white, how can i change the css class id the url is not / (index.js)字体是白色的,我该如何更改 css class id url 不是 / (index.js)

Thanks谢谢

I would suggest using @reach/router (is what Gatsby's <Link> component uses and how React builds and keeps the logic between links and pages).我建议使用@reach/router (这是 Gatsby 的<Link>组件使用的,以及 React 如何构建和保持链接和页面之间的逻辑)。 Once you've installed and imported the package, you can use a location consumer to get what you need safely inside it, because it will expose a location object with all the information you need.安装并导入 package 后,您可以使用location使用者在其中安全地获取您需要的内容,因为它会公开location object 以及您需要的所有信息。 Just like:就像:

return <Location>
  {({ location })=> {
     let textColor=location.pathname === '/' ? 'text-white' : 'text-gray-500'

        <footer className="w-full fixed bottom-0 px-4 sm:px-10 py-6 flex justify-between items-center">
      <div className=`${textColor} text-xs font-mono uppercase`>
        &copy; {new Date().getFullYear()} adam lawther
      </div>
      <nav className="flex items-center">
        <FaMixcloud className=`h-6 w-6 mr-4 ${textColor}` />
        <FaFacebookSquare className=`h-6 w-6 mr-4 ${textColor}` />
        <FaTwitter className=`h-6 w-6 mr-4 ${textColor}` />
      </nav>
    </footer>
  }}
</Location>

You can check for other options in https://css-tricks.com/how-to-the-get-current-page-url-in-gatsby/您可以在https://css-tricks.com/how-to-the-get-current-page-url-in-gatsby/中查看其他选项

Keep in mind that accessing directly to the window object may cause a break of your site since it's not defined at the runtime (check it in a build mode) so, you will need to check first if it's defined to render and execute the rest of your code logic, dirtying the code and making less clear and scalable.请记住,直接访问window object 可能会导致您的站点中断,因为它没有在运行时定义(在build模式中检查它)因此,您需要首先检查它是否定义为呈现和执行 Z65E8800B5C68A28AAD89 的 Z65E8800B5C68A26AAD89你的代码逻辑,弄脏了代码,变得不那么清晰和可扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM