简体   繁体   English

<React/tailwind>具有 z-index 递减/渲染顺序的地图对象

[英]<React/tailwind> map object with z-index decrement / order of rendering

My Navigation bar code我的导航条码

import React, { useLayoutEffect, useState } from "react";
import { useQuery,useMutation } from "../../../convex/_generated";
import { Link } from "react-router-dom";
import _function from "../../function";
import { Menu } from "../../model";
import ham from "../../assets/ham.svg";
import close from "../../assets/close.svg";
import "./nav.css";

const Nav = () => {

  let {windowSize} = _function();

  const [showSidebar, setShowSidebar] = useState(false);

  const list : {id:number, display: string, url: string, picurl: string }[]= [
    Menu(1,"Home","/",ham),
    Menu(2,"About","/about",ham),
    Menu(3,"Skill","/skills",ham),
    Menu(4,"Work","/works",ham),
    Menu(5,"Contact","/contact",ham)
];

  function navContentSwitch(text:string,url:string) { 
    return windowSize() >= 640 ? text : (<img
    width="20px"
    alt="smallicon"
    src={url}
  />) ;
  };

  return <nav>
  {showSidebar ? (
    <img
      className="flex z-30 items-center fixed left-5 top-5"
      onClick={() => setShowSidebar(!showSidebar)}
      src={close}
      width="50px"
      alt=""
    />
  ) : (
    <img
      onClick={() => setShowSidebar(!showSidebar)}
      className="fixed z-30 flex items-center cursor-pointer left-5 top-5"
      src={ham}
      width="40px"
      alt=""
    />
  )}
<div className="top-0 left-0 text-white fixed h-full z-40">
  <nav
    className={`z-50 fixed top-2/4 -translate-y-[calc(50%+3vh)] ${
      showSidebar ? "block" : "hidden"
    }`}
  >
    <ul className=" ">
      {list.map(item => {
        return <Link key={item.id} className="btn3d-a glow" to={item.url}>
        <li id="listmenu" className="btn3d-li z-10">{navContentSwitch(item.display,item.picurl)}</li>
      </Link>})}
    </ul>
  </nav>
</div>
</nav>
};

export default Nav;

Problem is z-index must be decrement from z-50 to z-10.问题是 z-index 必须从 z-50 减少到 z-10。 My code before map我在地图之前的代码

<ul className=" ">
  <Link key={list[0].id} className="btn3d-a glow" to={list[0].url}>
    <li id="listmenu" className="btn3d-li z-50">
      {navContentSwitch(list[0].display, list[0].picurl)}
    </li>
  </Link>
  <Link key={list[1].id} className="btn3d-a glow" to={list[1].url}>
    <li id="listmenu" className="btn3d-li z-40">
      {navContentSwitch(list[1].display, list[1].picurl)}
    </li>
  </Link>
  <Link key={list[2].id} className="btn3d-a glow" to={list[2].url}>
    <li id="listmenu" className="btn3d-li z-30">
      {navContentSwitch(list[2].display, list[2].picurl)}
    </li>
  </Link>
  <Link key={list[3].id} className="btn3d-a glow" to={list[3].url}>
    <li id="listmenu" className="btn3d-li z-20">
      {navContentSwitch(list[3].display, list[3].picurl)}
    </li>
  </Link>
  <Link key={list[4].id} className="btn3d-a glow" to={list[4].url}>
    <li id="listmenu" className="btn3d-li z-10">
      {navContentSwitch(list[4].display, list[4].picurl)}
    </li>
  </Link>
</ul>;

I tried to write class name this way我试着用这种方式写类名

    <ul className=" ">
      {list.map(item => {
        return <Link key={item.id} className="btn3d-a glow" to={item.url}>
        <li id="listmenu" className={`btn3d-li ${<--do something-->}`}>{navContentSwitch(item.display,item.picurl)}</li>
      </Link>})}
    </ul>

In a "do something" part.在“做某事”部分。 I declare ZLevel state with additional function to return string (z-50 ...) and write inside it like我用附加函数声明 ZLevel 状态以返回字符串 (z-50 ...) 并在其中写入

  • ZLevel && ZChange() ZLevel && ZChange()
  • ZChange()变化()

and I get this error ("Too many re-renders. React limits the number of renders to prevent an infinite loop").我收到此错误(“重新渲染太多。React 限制渲染数量以防止无限循环”)。 Is there a way to do z-index increment or What is the correct way to do so.有没有办法做 z-index 增量或这样做的正确方法是什么。

I am react beginner so any advice will be appreciated.我是反应初学者,所以任何建议将不胜感激。

Correct Code From Zachiah Advice Zachiah Advice 中的正确代码

    <ul className=" ">
      {list.map(item => {
        return <Link key={item.id} className="btn3d-a glow" to={item.url}>
        <li className={`btn3d-li z-${(list.length - item.id)*10}`}>{navContentSwitch(item.display,item.picurl)}</li>
      </Link>})}
    </ul>

Additional Information after Zachiah advice i found that when upload to host z-index is still not work properly even it work fine before deploy.And i find out about this by : Is it possible to change the order of list items using CSS3? Zachiah建议后的附加信息我发现,当上传到主机 z-index 时即使它在部署之前工作正常,它仍然无法正常工作。我通过以下方式了解这一点: 是否可以使用 CSS3 更改列表项的顺序? : https://coder-coder.com/z-index-isnt-working/ It's mean that what render first stack in a bottom . https ://coder-coder.com/z-index-isnt-working/ 这意味着在底部渲染的第一个堆栈

So in order to make first menu get render last just flip a list position因此,为了使第一个菜单最后渲染,只需翻转一个列表位置

  //by order of apperance affect z-index ***firstmenu = last in list***
  const list: {display: string; url: string; picurl: string }[] = [
    Menu("Contact", "/contact", "fa-solid fa-address-book"),
    Menu("Work", "/works", "fa-solid fa-laptop-file"),
    Menu("Skill", "/skills", "fa-solid fa-user-gear"),
    Menu("About", "/about", "fa-solid fa-address-card"),
    Menu("Home", "/", "fa-solid fa-house-chimney")
  ];

and rewrite ul list map like this.并像这样重写 ul 列表映射。

<ul className="flex flex-col-reverse">
  {list.map((item, index) => {
    return (
      <Link key={index} className="btn3d-a glow" to={item.url}>
        <li className="btn3d-li">
          {navContentSwitch(item.display, item.picurl)}
        </li>
      </Link>
    );
  })}
</ul>; 

It a lot cleaner like this and no need of z-index at all.像这样更干净,根本不需要 z-index。

If I'm not mistaken all you want to do is:如果我没记错的话,你想做的就是:

<ul className=" ">
  {list.map((item, index) => (
      <Link key={item.id} className="btn3d-a glow" to={item.url}>
          <li id="listmenu" className={`btn3d-li z-${(list.length - index) * 10}`}>{navContentSwitch(item.display,item.picurl)}</li>
      </Link>
  )}
</ul>

Notice the second argument to .map on an array is the index.请注意,数组上.map的第二个参数是索引。 If this isn't what you're looking for, please let me know.如果这不是你要找的,请告诉我。

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

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