Navbar组件的条件位置属性在Tailwind中无法正常工作。

huangapple go评论85阅读模式
英文:

Conditional Position Property of Navbar Component not Working in Tailwind

问题

我正在尝试根据变量的值是true还是false来显示和隐藏导航栏。我能够实现导航栏的显示/隐藏,但当我尝试将导航栏设置为在大屏幕断点下始终显示时,出现问题。

换句话说,我希望导航栏在大屏幕断点及以上始终显示,即使控制导航栏的变量设置为false也是如此。然而,这并没有发生。我无法弄清楚我的代码有什么问题。有人可以帮帮我吗?

我的代码如下:

<div className={`lg:!relative z-50 ${showNavBar ? "absolute" : "hidden"} `}>
      <Navbar showNavBar={showNavBar} handleNavBar={handleNavBar} />
</div>

请注意,我保留了您的代码中的代码部分,只翻译了注释和问题描述。

英文:

I am trying to show and hide a navbar based on whether a variable is set to true or false. I am able to get the Navbar to show/hide however, my problem happens when I try to set the Navbar to always show at the large breakpoint.

In other words, I want the navbar to be constantly showing at the large breakpoint and above even if the variable controlling the navbar is set to false. However, this does not happen. I can't figure out what is wrong with my code. Can someone please help me?

My code is here:

 &lt;div className={`lg:!relative z-50 ${showNavBar ? &quot;absolute&quot; : &quot;hidden&quot;} `}&gt;
      &lt;Navbar showNavBar={showNavBar} handleNavBar={handleNavBar} /&gt;
&lt;/div&gt;

答案1

得分: 0

你需要使用display工具来覆盖hiddenshowNavBar为假时应用的display: none

function Navbar() {
  return (
    <React.Fragment>
      Foo
    </React.Fragment>
  );
}

const showNavBar = false;
const handleNavBar = null;

ReactDOM.createRoot(document.getElementById('app')).render(
  <div className={`lg:!relative lg:block z-50 ${showNavBar ? "absolute" : "hidden"} `}>
    <Navbar showNavBar={showNavBar} handleNavBar={handleNavBar} />
  </div>
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js" integrity="sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js" integrity="sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.tailwindcss.com/3.3.2"></script>

<div id="app"></div>
英文:

You need to override the display: none applied by hidden when showNavBar is falsy by using a display utility, like block for display: block:

<!-- begin snippet: js hide: false console: false babel: true -->

<!-- language: lang-js -->

function Navbar() {
  return (
    &lt;React.Fragment&gt;
      Foo
    &lt;/React.Fragment&gt;
  );
}

const showNavBar = false;
const handleNavBar = null;

ReactDOM.createRoot(document.getElementById(&#39;app&#39;)).render(
  &lt;div className={`lg:!relative lg:block z-50 ${showNavBar ? &quot;absolute&quot; : &quot;hidden&quot;} `}&gt;
    &lt;Navbar showNavBar={showNavBar} handleNavBar={handleNavBar} /&gt;
  &lt;/div&gt;
);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js&quot; integrity=&quot;sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js&quot; integrity=&quot;sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.tailwindcss.com/3.3.2&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

你可以在方括号内的变量前加上 "max-lg",这样它们只会在大屏幕断点以下生效:

<div class={`lg:absolute ${showNavBar ? "max-lg:hidden" : "max-lg:absolute"} `}>
      <Navbar showNavBar={showNavBar} handleNavBar={handleNavBar} />
</div>
英文:

You can prefix the variables inside the brackets with "max-lg" so they only apply below the large breakpoint:

&lt;div class={`lg:absolute ${showNavBar ? &quot;max-lg:hidden&quot; : &quot;max-lg:absolute&quot;} `}&gt;
      &lt;Navbar showNavBar={showNavBar} handleNavBar={handleNavBar} /&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年7月11日 01:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定