我正在构建一个显示cat 的图像的网页,我想在每个图像上添加一个评级系统,允许用户喜欢cat .当用户将鼠标悬停在图像上时,我可以显示分级系统,但分级系统挡住了图像的一部分,所以当我将鼠标悬停在该部分上时,图像悬停效果不会被记录下来.

我曾try 将评级系统中的指针事件属性设置为无,但之后无法点击评级系统中的心形图标.当我将指针事件设置为All时,阻塞图像问题又回来了.

以下是我为一张cat 图片编写的当前代码:

import React, { useState } from "react";
import "./App.css";

export default function App() {
  const [isHovered, setIsHovered] = useState(false);
  const [isLiked, setIsLiked] = useState(false);
  const catUrl = "https://placekitten.com/300/300";

  return (
    <div className="image-container">
      <img
        src={catUrl}
        alt="img"
        className="img"
        onMouseOver={() => setIsHovered(true)}
        onMouseOut={() => setIsHovered(false)}
      />
      <div className={isHovered ? "rate show" : "rate"}>
        <img
          src={
            !isLiked
              ? "https://i.ibb.co/MMBpKMV/heart-2.png"
              : "https://i.ibb.co/BV2CwZK/heart-1.png"
          }
          alt="heart-2"
          border="0"
          className="heart-icon"
          onClick={() => setIsLiked(true)}
        />
      </div>
    </div>
  );
}
.img {
  width: 300px;
  height: 300px;
  position: relative;
}

.rate {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 20%;
  background-color: rgba(255, 255, 255, 0.9);
  display: none;
  justify-content: center;
  align-items: center;
  pointer-events: none;
}

.show {
  display: flex;
}

.heart-icon {
  width: 30px;
  height: 30px;
  cursor: pointer;
  pointer-events: all;
}

.image-container {
  position: relative;
  width:fit-content
}

如有任何帮助,将不胜感激!

推荐答案

您可以删除所有mouseOver逻辑,只需将:hover规则添加到父元素即可.

import React, { useState } from "react";
import "./App.css";

export default function App() {
  const [isLiked, setIsLiked] = useState(false);
  const catUrl = "https://placekitten.com/300/300";

  return (
    <div className="image-container">
      <img
        src={catUrl}
        alt="img"
        className="img"
      />
      <div className="rate">
        <img
          src={
            !isLiked
              ? "https://i.ibb.co/MMBpKMV/heart-2.png"
              : "https://i.ibb.co/BV2CwZK/heart-1.png"
          }
          alt="heart-2"
          border="0"
          className="heart-icon"
          onClick={() => setIsLiked(true)}
        />
      </div>
    </div>
  );
}
.image-container:hover .rate {
  display: flex;
}

简单的css只是个简单的例子.

document.querySelector('.overlap').addEventListener('click', (e) => {
  e.target.classList.toggle('red');
});
.container {
  position: relative;
  height: 180px;
  width: 180px;
  background: gray;
  border: 1px solid black;
}

.base {
  height: 100%;
  width: 100%;
  background: pink;
}

.overlap {
  display: none;
  position: absolute;
  height: 30px;
  width: 80px;
  bottom: 10px;
  right: 10px;
  background: yellow;
}

.container:hover .overlap {
  display: block;
}

.red {
  background: red;
}
<div class="container">
  <div class="base"></div>
  <div class="overlap"></div>
</div>

Javascript相关问答推荐

promise .all()永不解决

为什么新的Promises会在不需要的情况下被用来等待?

如何在ReactJS中修复这种不需要的按钮行为?

带对角线分隔符的图像滑动器

自定义帖子类型帖子未显示在网站上

如何为GrapesJS模板编辑器创建自定义撤销/重复按钮?

将状态向下传递给映射的子元素

如何在Angular中插入动态组件

Phaser 3 console. log()特定游戏角色的瓷砖属性

google docs boldText直到按行执行应用脚本错误

Mongoose post hook在使用await保存时不返回Postman响应

为什么!逗号和空格不会作为输出返回,如果它在参数上?

如何在Vue 3中创建自定义 Select 组件,并将选项作为HTML而不是props 传递?

如何在JAVASCRIPT中临时删除eventListener?

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

将范围 Select 器添加到HighChart面积图

在Java脚本中录制视频后看不到曲目

JavaScript&;Reaction-如何避免在不使用字典/对象的情况下出现地狱?

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

ReferenceError:无法在初始化之前访问setData