我如何仅使用NextJS激活样式属性以悬停组件?

发布于 2025-02-07 02:00:08 字数 2261 浏览 0 评论 0 原文

我希望能够悬停一些组件并具有特殊的CSS特征。因此,例如,如果我徘徊在项目A上,我只希望将项目A与其他项目区分开。

现在,我有这种方法更改元素背景图像,这是我的代码:


const StatsIndicadores = () => {

    const somearray = ['10%', '50%', '60%', '60%']

    const [isHovering, setIsHovering] = useState(false)
    const [hoverStyle, setHoverStyle] = useState({})
    const [index, setIndex] = useState(0)

    const mouseEntered = (index) => {
        setIsHovering(true);
        setIndex(index + 1);
    }
    const mouseLeft = () => setIsHovering(false)


    useEffect(() => {
        if (isHovering) {
            setHoverStyle({
                background: `center url("/images/stats/rounded-images/${index}.png") no-repeat`,
                border: '1px solid #f5f5f5',
                boxShadow: '0px 0px 10px #f5f5f5',
                transition: 'all 0.3s ease-in-out'
            });
        } else {
            setHoverStyle({});
        }
    }, [isHovering]);

    return (
        <>

            {<Grid container className={style.test}>
                {
                    somearray.map((item, index) => (
                        <Grid item xs={12} md={3} key={index}>
                            <div className={style.statsContainer}>
                                <div className={style.circle} style={hoverStyle} onMouseEnter={(e) => { mouseEntered(index) }} onMouseLeave={mouseLeft}>
                                    <p className={style.circleStats}>Some value</p>
                                    <p className={`${style.circleStats} ${style.circleStatsDescription}`}>Ver más</p>
                                </div>
                                <p className={style.indicador}>Some name</p>
                            </div>
                        </Grid>
                    ))
                }
            </Grid>}
        </>
    )
}

当我徘徊在地图中的某些项目上时,这就是我得到的:

我不想在其他圆圈上显示背景图像,但我不知道如何仅在悬停的一个圆圈上显示它。 我有想法比较盘旋项目的索引,如果该项目活跃,请显示背景...但是我不知道这样做是最好的。

I want to be able to hover some component and display with special CSS characteristics. So, for example, if I hover on item A, i only want item A to be distinguished from the other items.

Right now I have this approach changing the element background image, this is my code:


const StatsIndicadores = () => {

    const somearray = ['10%', '50%', '60%', '60%']

    const [isHovering, setIsHovering] = useState(false)
    const [hoverStyle, setHoverStyle] = useState({})
    const [index, setIndex] = useState(0)

    const mouseEntered = (index) => {
        setIsHovering(true);
        setIndex(index + 1);
    }
    const mouseLeft = () => setIsHovering(false)


    useEffect(() => {
        if (isHovering) {
            setHoverStyle({
                background: `center url("/images/stats/rounded-images/${index}.png") no-repeat`,
                border: '1px solid #f5f5f5',
                boxShadow: '0px 0px 10px #f5f5f5',
                transition: 'all 0.3s ease-in-out'
            });
        } else {
            setHoverStyle({});
        }
    }, [isHovering]);

    return (
        <>

            {<Grid container className={style.test}>
                {
                    somearray.map((item, index) => (
                        <Grid item xs={12} md={3} key={index}>
                            <div className={style.statsContainer}>
                                <div className={style.circle} style={hoverStyle} onMouseEnter={(e) => { mouseEntered(index) }} onMouseLeave={mouseLeft}>
                                    <p className={style.circleStats}>Some value</p>
                                    <p className={`${style.circleStats} ${style.circleStatsDescription}`}>Ver más</p>
                                </div>
                                <p className={style.indicador}>Some name</p>
                            </div>
                        </Grid>
                    ))
                }
            </Grid>}
        </>
    )
}

When I hover over some item in the map, this is what I get:
Hover on image isnt working as expected

I don't want to display the background image on the other circles, but I don't know how to display it only on the hovered one.
I have the idea to compare the index of the hovered item and if that one is active, display the background... but I can't figure which way of doing this would be the best one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2025-02-14 02:00:08

截至目前,当其中一个盘旋时,您将这些样式应用于所有圆圈。您需要根据活动盘旋圆圈有条件地应用样式(您可以使用任何独特的属性 - 在下面的示例中,我使用了ID)。

演示代码

import { useState } from "react";
import "./styles.css";

const data = [
  {
    id: 1,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_3305.jpg"
  },
  {
    id: 2,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_10245.jpg"
  },
  {
    id: 3,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_1345.jpg"
  },
  {
    id: 4,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_10947.jpg"
  },
  {
    id: 5,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_5331.jpg"
  }
];

export default function App() {
  const [activeId, setActiveId] = useState(0);

  const setActiveElementOnHover = (id) => {
    setActiveId(id);
  };

  const resetActiveElementOnLeave = () => {
    setActiveId(0);
  };

  return (
    <div className="app">
      <h1 className="headline">Dog Gallery</h1>
      <div className="gallery-container">
        {data.map(({ id, url }) => (
          <div
            key={id}
            role="presentation"
            className="circle"
            style={{
              background:
                activeId === id ? `center / cover no-repeat url(${url})` : ""
            }}
            onMouseEnter={() => setActiveElementOnHover(id)}
            onMouseLeave={resetActiveElementOnLeave}
          >
            <p>Dog #{id}</p>
          </div>
        ))}
      </div>
    </div>
  );
}

.app {
  font-family: sans-serif;
  text-align: center;
}

.gallery-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 800px;
  margin: 0 auto;
}

.circle {
  padding: 75px;
  border-radius: 50%;
  background: navy;
  color: white;
  margin: 0 10px;
}

.headline {
  font-size: 10rem;
  color: navy;
}

p {
  padding: 0;
  margin: 0;
}

As of now, you're applying the styles to all circles when one of them is hovered. You need to conditionally apply the styles based upon the active hovered circle (you can use any property that's unique -- in the example below, I used an id).

Demo Code:

Edit fervent-wildflower-tc1xiy

Working Demo

Code:

import { useState } from "react";
import "./styles.css";

const data = [
  {
    id: 1,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_3305.jpg"
  },
  {
    id: 2,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_10245.jpg"
  },
  {
    id: 3,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_1345.jpg"
  },
  {
    id: 4,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_10947.jpg"
  },
  {
    id: 5,
    url: "https://images.dog.ceo/breeds/terrier-american/n02093428_5331.jpg"
  }
];

export default function App() {
  const [activeId, setActiveId] = useState(0);

  const setActiveElementOnHover = (id) => {
    setActiveId(id);
  };

  const resetActiveElementOnLeave = () => {
    setActiveId(0);
  };

  return (
    <div className="app">
      <h1 className="headline">Dog Gallery</h1>
      <div className="gallery-container">
        {data.map(({ id, url }) => (
          <div
            key={id}
            role="presentation"
            className="circle"
            style={{
              background:
                activeId === id ? `center / cover no-repeat url(${url})` : ""
            }}
            onMouseEnter={() => setActiveElementOnHover(id)}
            onMouseLeave={resetActiveElementOnLeave}
          >
            <p>Dog #{id}</p>
          </div>
        ))}
      </div>
    </div>
  );
}

styles:

.app {
  font-family: sans-serif;
  text-align: center;
}

.gallery-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 800px;
  margin: 0 auto;
}

.circle {
  padding: 75px;
  border-radius: 50%;
  background: navy;
  color: white;
  margin: 0 10px;
}

.headline {
  font-size: 10rem;
  color: navy;
}

p {
  padding: 0;
  margin: 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文