You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js组件中为.itemCard与按钮设置独立Hover效果方案

实现.itemCard与按钮的独立Hover效果

需要为Next.js组件中的.itemCard添加hover阴影效果,同时让按钮实现hover背景色变化,之前尝试用pointer-events:none的方式未成功,以下是修正后的实现方案。

组件代码(无需修改)

const Img = (props: imgProps) => {
  return (
    <div className={styles.itemCard}>
      <div className={styles.imagecontainer}>
        <div className={styles.imageOuter}>
          <img className={styles.imageItself} src={props.img} alt="" />
        </div>
          <div className={styles.details}>
            <h3 className={styles.titleCardHome}>{props.title}</h3>
            <p className={styles.description}>Lorem ipsum dolor sit amet consectetur.</p>
            <p className={styles.priceHome}>{props.price}</p>
            <button className={styles.btncartAdd}>Add to Cart</button>
          </div>
      </div>
    </div>
  );
};

修改后的CSS代码

.itemCard {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    max-width: 20rem;
    min-width: 15rem;
    background-color: rgba(255, 255, 255, 0);
    border-radius: 5px;
    margin: 10px;
    transition: all 0.3s ease-in-out; /* 合并重复的过渡属性 */
}

/* 为.itemCard添加hover阴影效果 */
.itemCard:hover {
    box-shadow: 0px 10px 17px rgba(0, 0, 0, 0.214);
}

.imagecontainer {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    max-width: 20rem;
    min-width: 15rem;
    background-color: rgb(234, 202, 117);
    border-radius: 5px;
    margin: 20px;
    transition: all 0.3s ease-in-out; /* 合并重复的过渡属性 */
    box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.139);
}

.imagecontainer .imageOuter {
    max-height: 60%;
    min-height: 60%;
    width: 100%;
    background-color: antiquewhite;
}

.imagecontainer .imageOuter img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 5px 5px 0px 0px;
}

.itemCard .details {
    font-family: 'Courier New', Courier, monospace;
    display: flex;
    flex-direction: column;
    margin: -50px 5px -30px 5px;
    background-color: rgb(254, 229, 160);
    box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.067);
    width: 100%;
    height: 8rem;
    border-radius: 5px;
    padding: 10px;
}

.details .btncartAdd {
    background-color: rgb(223, 84, 20);
    border: none;
    width: 100%;
    padding: 5px;
    margin-top: 10px;
    color: white;
    border-radius: 5px;
    transition: background-color 0.3s ease-in-out; /* 为按钮添加背景色过渡 */
}

/* 为按钮添加hover背景色变化 */
.details .btncartAdd:hover {
    background-color: rgb(245, 100, 30); /* 可根据需求调整颜色 */
}

关键修正说明

  • 移除了原代码中不必要的pointer-events:none,以及错误的.itemCard :not(.btncartAdd):hover选择器(该选择器会给卡片内非按钮的子元素添加hover效果,而非卡片本身)。
  • 直接为.itemCard:hover设置阴影,确保鼠标hover到卡片任意区域时触发阴影效果。
  • 单独为.btncartAdd:hover设置背景色变化,同时添加过渡动画让颜色切换更平滑。
  • 合并了原CSS中重复的transition属性,简化代码结构。

内容的提问来源于stack exchange,提问作者Ramesh Madara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 02:23:11