简体   繁体   English

onClick 事件可以处理 onMouseEnter 事件吗?

[英]can an onClick event handle an onMouseEnter event?

我的开发者

so I have a bunch of logos displayed on the right side of a div and an information card on the left.所以我在 div 的右侧显示了一堆徽标,在左侧显示了信息卡。

every logo when hovered makes the card display specific info.悬停时的每个徽标都会使卡片显示特定信息。 I'm trying to make a function where when the logo is clicked instead of just hovered, will make the card stick to the info from the logo they clicked on.我正在尝试制作一个 function,当单击徽标而不是仅将鼠标悬停时,将使卡片粘在他们单击的徽标中的信息上。

this issue appears from the fact that some people use the cursor to read.出现这个问题是因为有人使用cursor阅读。 so if the user is on the far right on the screen and is trying to see the info from one of those logos, it will try to go back to the card to read the info and will accidentally change it because of the onMouseEnter event, making it a bad user experience for them.因此,如果用户在屏幕的最右侧并试图从其中一个徽标中查看信息,它会尝试将 go 返回卡以读取信息,并且由于 onMouseEnter 事件会意外更改它,从而这对他们来说是一个糟糕的用户体验。

I have been trying to figure this one out, might not be asking google correctly, hope you guys can understand what I'm asking.我一直在努力解决这个问题,可能没有正确地询问谷歌,希望你们能理解我在问什么。

thank you in advance!先感谢您!

import React, { useState } from "react";
import { imgrep } from "../../Helper/imgrep";
import styled from "styled-components";
import "react-circular-progressbar/dist/styles.css";
import Cards from "./Cards";
import { FaRegEye, VscGraphLine, BsFileEarmarkText, FaGitAlt, IoRocketSharp, SiFastapi } from 'react-icons/all';
import { memerep } from "../../Helper/memes";
import { CircularProgressbar, buildStyles } from 'react-circular-progressbar';
import 'react-circular-progressbar/dist/styles.css';



const ShowAndHide = ( ) => {
  
  const professions = [
    {
      circular: <CircularProgressbar value= "99" text={`99%`} 
      styles={buildStyles({
       // Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
       strokeLinecap: 'butt',
       // Text size
       textSize: '16px',
       // How long animation takes to go from one percentage to another, in seconds
       pathTransitionDuration: 1.5,
       // Colors
       pathColor: `rgba(58, 123, 213, ${99 / 100})`,
       textColor: '#102647',
       trailColor: '#e0e0eb',
       backgroundColor: '#3e98c7',
     })}
     /> ,
      git:<FaGitAlt/>,
      industrycon: <IoRocketSharp alt="Space Rocket"/> ,
      key: "card1",
      meme: <img src={memerep(0)} className="memes"/>,
      project:"PROYECTOlola",
      icon: <FaRegEye alt="Computer-vision-logo" />,
      percentage: 99,
      specialty: "CV",
      industry:"Gases Clinicos",
      summary:"Compliance del 99% de normas sanitarias para operación en pandemia , identifiacando dist. social, aforo y mascarilla.",
      logo: <img className="Mediumsquare" src={imgrep(1)} alt="altofem" />
    },


    { 
      circular: <CircularProgressbar value= "98" text={`98%`} 
      styles={buildStyles({
       // Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
       strokeLinecap: 'butt',
       // Text size
       textSize: '16px',
       // How long animation takes to go from one percentage to another, in seconds
       pathTransitionDuration: 1.5,
       // Colors
       pathColor: `rgba(58, 123, 213, ${98 / 100})`,
       textColor: '#102647',
       trailColor: '#e0e0eb',
       backgroundColor: '#3e98c7',
     })}
     /> ,
      key: "card2",
      industrycon: <IoRocketSharp alt="Space Rocket"/>,
      git:<FaGitAlt/>,
      project:"PROYECTO",
      icon: <VscGraphLine alt="Data-analytics-logo"/>,
      specialty: "NPL",
      industry: "inmobiliaria",
      summary: "Un proyecto Vimac es la garantía de un diseño bien pensado, una arquitectura de tradición basada en principios sólidos, que busca otorgar hogares de calidad que se adaptan a un estilo de vida contemporáneo.",
      logo: <img className="Biground" src={imgrep(2)} alt="vimac" />,
    },


    {
      circular: <CircularProgressbar value= "97" text={`97%`} 
      styles={buildStyles({
       // Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
       strokeLinecap: 'butt',
       // Text size
       textSize: '16px',
       // How long animation takes to go from one nother, in seconds
       pathTransitionDuration: 1.5,
       // Colors
       pathColor: `rgba(58, 123, 213, ${97 / 100})`,
       textColor: '#102647',
       trailColor: '#e0e0eb',
       backgroundColor: '#3e98c7',
     })}
     /> ,
      key: "card3",
      industrycon: <IoRocketSharp alt="Space Rocket"/>,
      git:<FaGitAlt/>,
      project:"PROYECTO",
      icon:<BsFileEarmarkText alt="Natural-language-processing-logo"/> ,
      specialty: "NLP",
      industry: "RRHH",
      summary:"Ahorro de alrededor un 96% de tiempo empleado para diagnóstico de CV's.",
      logo: <img  className="Mediumsquare" src={imgrep(3)} alt="peopleparnerts" />,
    },


    { 
      circular: <CircularProgressbar value= "96" text={`96%`} 
      styles={buildStyles({
       // Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
       strokeLinecap: 'butt',
       // Text size
       textSize: '16px',
       // How long animation takes to go from one nother, in seconds
       pathTransitionDuration: 1.5,
       // Colors
       pathColor: `rgba(58, 123, 213, ${96 / 100})`,
       textColor: '#102647',
       trailColor: '#e0e0eb',
       backgroundColor: '#3e98c7',
     })}
     /> ,
      key: "card4",
      industrycon: <IoRocketSharp alt="Space Rocket"/>,
      git:<FaGitAlt/>,
      project:"PROYECTO",
      icon: <SiFastapi alt="API"/>,
      specialty: "cv",
      industry: "Big Data",
      summary: "Para entregar las soluciones que nuestros clientes necesitan, hemos establecido alianzas con destacadas empresas e instituciones de tecnología o educación superior.",
      logo: <img width="" className="Mediumsquare" src={imgrep(4)} alt="uplanner" />
    }
  ];
  
  
  const [myProfession, setMyProfession] = useState("");
  const[clickStatus, setClickStatus]= useState(false);
 

  if (clickStatus === true) {
    console.log("clicked")
    
  }
  else if(clickStatus === false){
    console.log("not clicked");
  }
   return (

     <>
      {/* INFORMATION CARDS */}
      <Container> 
        <LeftSide>
          <Bottom>
            {professions &&(
          <Cards 
          circular={myProfession.circular}
          project={myProfession.project}
          icon={myProfession.icon}
          percentage={myProfession.percentage} 
          specialty= {myProfession.specialty}
          industry={myProfession.industry}
          summary={myProfession.summary}
          git={myProfession.git}
          industrycon={myProfession.industrycon}
           />       
            )}    
            
            {professions.map((profession) => ( 
              <info
              circular={profession.circular}
              project={profession.project}
              icon={profession.icon}
              git={profession.git}
              industrycon={profession.industrycon}
              percentage={profession.percentage}
              specialty={profession.specialty}
              industry={profession.industry}
              summary={profession.summary}
              onMouseEnter={() => setMyProfession(profession.logo.props.alt)}/>
            ))}
          </Bottom>
        </LeftSide>
        {/* HOVERING LOGOS */}
        <RightSide>
          <h2> - Nuestros Casos de Exito -</h2>
          <br />
          <Buttons>
            {professions.map((profession) => (
              <>
                <img 
                  type="img"
                  key={profession}
                  id={profession.logo.props.id}
                  src={profession.logo.props.src}
                  className={profession.logo.props.className}
                  onMouseEnter={() => setMyProfession(profession)}
                  onClick={()=> setClickStatus(true)}
                  onMouseLeave={()=> setClickStatus(false)}
                ></img>
              </>
            ))}
          </Buttons>
        </RightSide>
      </Container>
    </>
  );
};

export default ShowAndHide;

I hope I understand what you mean.我希望我明白你的意思。

if you want to add an onClick event to right side logo as you did,如果您想像以前一样将 onClick 事件添加到右侧徽标,

import React, { useState } from "react";
import { imgrep } from "../../Helper/imgrep";
import styled from "styled-components";
import "react-circular-progressbar/dist/styles.css";
import Cards from "./Cards";
import { FaRegEye, VscGraphLine, BsFileEarmarkText, FaGitAlt, IoRocketSharp, SiFastapi } from 'react-icons/all';
import { memerep } from "../../Helper/memes";
import { CircularProgressbar, buildStyles } from 'react-circular-progressbar';
import 'react-circular-progressbar/dist/styles.css';

  
  const [myProfession, setMyProfession] = useState("");
  const[Clicked, setClicked]= useState(false);


  const handleSelectLogo = (e) => {
    logoKey = e.currentTarget.dataset.id; // data-key
    const profession = professions.find(prof => prof.key === logoKey);
    setMyProfession(profession);
    setClicked(true);
  }

  const handleLeaveLogo = (e) => {
      if(Clicked){
        return;
      }
      setMyProfession("")
  }

   return (

     <>
      {/* INFORMATION CARDS */}
      <Container> 
        <LeftSide>
          <Bottom>
            {myProfession && (
          <Cards 
          circular={myProfession.circular}
          project={myProfession.project}
          icon={myProfession.icon}
          percentage={myProfession.percentage} 
          specialty= {myProfession.specialty}
          industry={myProfession.industry}
          summary={myProfession.summary}
          git={myProfession.git}
          industrycon={myProfession.industrycon}
           />       
            )}    
            
            {professions.map((profession) => ( 
              <info
              circular={profession.circular}
              project={profession.project}
              icon={profession.icon}
              git={profession.git}
              industrycon={profession.industrycon}
              percentage={profession.percentage}
              specialty={profession.specialty}
              industry={profession.industry}
              summary={profession.summary}
              onMouseEnter={() => setMyProfession(profession.logo.props.alt)}/>
            ))}
          </Bottom>
        </LeftSide>
        {/* HOVERING LOGOS */}
        <RightSide>
          <h2> - Nuestros Casos de Exito -</h2>
          <br />
          <Buttons>
            {professions.map((profession) => (
              <>
                <img 
                  type="img"
                  key={profession}
                  id={profession.logo.props.id}
                  data-key={profession.key}
                  src={profession.logo.props.src}
                  className={profession.logo.props.className}
                  onMouseEnter={() => setMyProfession(profession)}
                  onClick={handleSelectLogo}
                  onMouseLeave={handleLeaveLogo}
                ></img>
              </>
            ))}
          </Buttons>
        </RightSide>
      </Container>
    </>
  );
};

export default ShowAndHide;

you can add a dataset to your dom element like that and you can access that key (or unique value) in your function.您可以像这样将数据集添加到您的 dom 元素中,并且可以在 function 中访问该键(或唯一值)。 I hope I was able to help我希望我能提供帮助

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

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