简体   繁体   English

Jquery mouseenter 和 mouseleave 在 Next.js 中被弃用,Tailwind

[英]Jquery mouseenter and mouseleave is deprecated in Next.js, Tailwind

enter image description here在此处输入图像描述

I wanted to create useEffect variables and put it to true/false when user hovers on parent div .我想创建useEffect变量并在用户将鼠标悬停在父div上时将其设置为 true/false。 I want to use that hover variable in a child div with image and resize image when user hovers on parent div with id="infoCard" .我想在带有图像的子div中使用 hover 变量,并在用户将鼠标悬停在带有id="infoCard"的父div上时调整图像大小。

Code-代码-

const [hover, setHover] = useState(false);

<div
  className="flex flex-col md:flex-row font-inter py-7 px-2 border-b rounded-xl cursor-pointer hover:shadow-lg pr-6 transition duration-200 ease-out first:border-t hover:bg-red-100 mb-2"
  id="infoCard"
>
  <div className="relative h-40 w-64 md:h-52 md:w-80 flex-shrink-0 ml-6">
    <Image
      src={img}
      layout="fill"
      objectFit="cover"
      className={`rounded-2xl scale-95 ${hover ? "scale-100" : ""
        } transform transition duration-200 ease-out`}
    />
  </div>

  <div className="flex flex-col flex-grow pl-5 ml-2 mt-2 md:mt-0">
    <div className="flex justify-between">
      <p>
        {location} {city}
      </p>
      <HeartIcon className="h-7 cursor-pointer" />
    </div>

    <h4 className="text-xl">{title}</h4>

    <div className="border-b w-10 pt-2" />

    <p className="pt-2 text-sm text-gray-500 flex-grow">
      {numberOfGuest}
      {description}
    </p>

    <div className="flex justify-between items-end">
      <p className="flex items-center">
        <StarIcon className="h-5 text-red-400" />
        {star}
      </p>

      <div>
        <p className="text-lg pb-2 font-semibold lg:text-2xl">{price}</p>
        <p className="text-right font-light">{total}</p>
      </div>
    </div>
  </div>
</div>

You could do this without the hover state with just CSS. Take a look at the documentation on group-hover classes here .您可以在没有 hover state 的情况下仅使用 CSS 来执行此操作。请在此处查看有关组悬停类的文档。 They work like this:他们是这样工作的:

<div class="group">
   <img class="transform scale-95 group-hover:scale-100" />
</div>

Here's a minimal example of how you could do this.这是您如何执行此操作的最小示例。 Note that you also need to extend the variants of the scale classes in the tailwind.config.js file as it's not included by default.请注意,您还需要在tailwind.config.js文件中扩展比例类的变体,因为默认情况下不包含它。

As a side note: You don't typically use jQuery in a React-based projects.作为旁注:您通常不会在基于 React 的项目中使用 jQuery。 If you need to detect hover, React has built-in mouse events.如果需要检测hover,React内置了鼠标事件。 Read more here .在这里阅读更多。

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

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