简体   繁体   English

在移动设备上禁用 hover 的需要

[英]Disable the need to hover on mobile devices

I've built a project that is displaying images upon render.我已经建立了一个在渲染时显示图像的项目。 When you hover over an image a heart icon and shopping cart icon pop up and has their own functionality.当您 hover 在图像上时,会弹出一个心形图标和购物车图标并具有自己的功能。 Everything works fine but I need the heart icon and shopping cart icon to show up on mobile devices without the need to hover over them.一切正常,但我需要心形图标和购物车图标显示在移动设备上,而不需要 hover 覆盖它们。

I'm using the custom hook below to manage the functionality of hover state.我正在使用下面的自定义挂钩来管理 hover state 的功能。

  import  { useState, useEffect, useRef }from "react"
    
    const useHover = () =>{
    const[hovered, setHovered] = useState(false)
    const ref = useRef(null)
    
    useEffect(() => {
        const cleanUp = ref.current
    
        ref.current.addEventListener("mouseenter", enter)
        ref.current.addEventListener("mouseleave", leave)
        
        return () => {    
            cleanUp.removeEventListener("mouseenter", enter)
            cleanUp.removeEventListener("mouseleave", leave)
        }
    }, [])
    
        const enter = () =>{
            setHovered(true)
        }
    
        const leave = () =>{
            setHovered(false)
        }
    
    return [hovered, ref]
    
    }
    
    export default useHover

In my image component I'm using my ref在我的图像组件中,我正在使用我的 ref

import React, { useContext } from "react";
import useHover from "../hooks/useHover"; 
import { Context } from "../../Context";

import "../Image/Image.css";

const Image = ({ className, photo }) => {
  const { 
  toggleFavorite, 
  addToCart, 
  cartItems, 
  removeFromCart } = useContext(Context);

  const [hovered, ref] = useHover();


  //displaying filled heart icon when clicked, or outline heart when hovered
  function heartIcon() {
    if (photo.liked_by_user) {
      return<i onClick={() => toggleFavorite(photo.id)} className="ri-heart-fill favorite"></i>
    } else if (hovered) {
      return <i onClick={() => toggleFavorite(photo.id)} className="ri-heart-line favorite"></i>
    }
  }




   //displaying shopping cart icon when clicked, or plus icon when hovered
  function cartIcon() {
    const alreadyInCart = cartItems.find((item) => item.id === photo.id);
    if (alreadyInCart) {
      return <i onClick={() => removeFromCart(photo)} className="ri-shopping-cart-fill cart shopping-cart-icon"></i>
    } else if (hovered) {
      return <i onClick={() => addToCart(photo)} className="ri-add-circle-line plus-icon"></i>
    }
  }




  return (
    <div ref={ref} className={`${className} image-container`}>
      <img
        className="image-grid"
        src={photo.urls.thumb}
        alt={photo.alt_description}
      />
      {heartIcon()}
      {cartIcon()}
    </div>
  );
};

export default Image;

Can anyone point me in the right direction on this?谁能指出我正确的方向? When on mobile devices I basically need to set my hovered state to stay true and have my icons to always show.在移动设备上时,我基本上需要将悬停的 state 设置为保持真实并始终显示我的图标。 Any help would be appreciated.任何帮助,将不胜感激。

You can try an npm package like, react-device-detect , to detect mobile devices.您可以尝试使用类似react-device-detect的 npm package 来检测移动设备。 In case it's a mobile you can return hovered as true always I guess.如果它是移动设备,我猜你总是可以返回悬停。

CSS media query, @media (hover: hover) , seems like a better option here. CSS 媒体查询@media (hover: hover)在这里似乎是一个更好的选择。 You should check it out too.你也应该检查一下。

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

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