简体   繁体   English

把styles改成hover一个合适的项目

[英]Change styles on hover of an appropriate item

How can I Change styles on hover of an appropriate item react?我怎样才能改变 styles 上 hover 的适当项目反应? Now the styles are changed of all of the items at a time.现在 styles 一次更改所有项目。 Hovering on add to cart button I need to change only the chosen div styles. https://codesandbox.io/s/trusting-moon-djocul?file=/src/components/Category.js **将鼠标悬停在添加到购物车按钮上,我只需要更改所选的 div styles。https://codesandbox.io/s/trusting-moon-djocul?file=/src/components/Category.js **

 import React, { useState } from "react"; import styles from "./category.module.css"; import Categories from "./Categories"; const Category = () => { const [hovered, setHovered] = useState(false); const [data, setData] = useState(Categories); const style = hovered? { backgroundColor: "#ffcbcb", color: "#fff", transition: "all 0.5s ease" }: {}; const filterResult = (catItem) => { const result = Categories.filter((curData) => curData.category === catItem); setData(result); }; return ( <> <div className={styles.items}> {data.map((value) => { const { id, title, price, description } = value; return ( <> <div className={styles.item} key={id} style={style}> <h1>{title}</h1> <p> {price} <span>$</span> </p> <p>{description}</p> <button onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)} className={styles.btn} > Add to Cart </button> </div> </> ); }; export default Category;

That's because you have a single "hovered" state shared between all your cards, you should create a "Card" component and have that component have its own hovered state那是因为您在所有卡片之间共享一个“悬停” state,您应该创建一个“卡片”组件并让该组件有自己的悬停 state

  return (
    <>
        <div className={styles.items}>
          {data.map((value) => {
            return (
              <>
               <Card {...props} />
              </>
          
  );

Problem问题

This issue is occurring cause you are applying CSS to all the cards.出现此问题是因为您将 CSS 应用于所有卡片。 The only small thing you are missing in your logic is adding CSS to the only card whose button was being hovered.你的逻辑中唯一缺少的小东西是将 CSS 添加到唯一一个按钮被悬停的卡片。

Solution解决方案

That can be achieved by using event objects on mouse enter and mouse leave events.这可以通过在鼠标进入和鼠标离开事件上使用事件对象来实现。

<div className={styles.item} key={id} style={style}>
  <h1>{title}</h1>
  <p>
    {price} <span>$</span>
  </p>
  <p>{description}</p>
  <button
    onMouseEnter={(e) =>
      e.currentTarget.parentElement.classList.add("active_card")
    }
    onMouseLeave={(e) =>
      e.currentTarget.parentElement.classList.remove("active_card")
    }
    className={styles.btn}
    >
     Add to Cart
 </button>
</div>

This will add a class of "active_card" on the card whose Add To Card button is being hovered, then you can apply your desired styling to that class.这将在“添加到卡片”按钮悬停的卡片上添加“active_card”的 class,然后您可以将所需的样式应用到该 class。

.active_card {
  background-color: #ffcbcb !important;
}

Example例子

Working Code Sandbox Example工作代码沙箱示例

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

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