Direction Aware Hover – Aceternity UI 组件

A card hover effect that detects the direction of mouse entry.

@aceternity/direction-aware-hover-demo
thumbnail

Make things float in air

Hover over this card to unleash the power of CSS perspective

tsx
1"use client";
2import React from "react";
3import { motion } from "framer-motion";
4import { cn } from "@/lib/utils";
5
6export const DirectionAwareHover = ({
7    className,
8    children,
9    ...props
10}) => {
11  return (
12    <motion.div
13      whileHover={{ scale: 1.02 }}
14      transition={{ type: "spring", stiffness: 300 }}
15      className={cn(
16        "relative rounded-2xl border border-neutral-200 bg-white p-6 shadow-lg",
17        "dark:border-neutral-800 dark:bg-neutral-900",
18        className
19      )}
20      {...props}
21    >
22      {children}
23    </motion.div>
24  );
25};

Installation

npx shadcn@latest add @aceternity/direction-aware-hover
npm install framer-motion clsx tailwind-merge

A card hover effect that detects the direction of mouse entry.

概述

Direction Aware Hover 是 Aceternity UI 组件库中的一个精选组件,专为 React + Tailwind CSS 项目设计。它提供了流畅的动画效果和现代化的视觉体验,可以轻松集成到你的项目中。

特性

  • React + TypeScript 支持,类型安全
  • Tailwind CSS 样式,响应式设计
  • Framer Motion 动画引擎,流畅自然
  • 复制即用,无需额外安装依赖
  • 暗色模式兼容
  • SSR/SSG友好,支持 Next.js

使用场景

  1. 产品落地页 - 为你的产品页面添加视觉冲击力
  2. 后台管理系统 - 提升管理界面的用户体验
  3. 个人作品集 - 展示你的技术实力
  4. SaaS 应用 - 为用户提供现代化的界面
  5. 内容展示 - 以引人入胜的方式展示内容

技术栈

  • React 18+
  • Tailwind CSS 3+
  • Framer Motion / Motion
  • TypeScript(可选)

兼容性

框架支持状态
Next.js 14+完全支持
Vite + React完全支持
Create React App支持
Remix支持
Astro + React支持
## 注意事项
  • 确保已安装 framer-motiontailwindcss
  • 组件使用 "use client" 指令,适用于 App Router
  • 所有组件均支持暗色模式
  • 建议在 tailwind.config.js 中配置 cn 工具函数

Examples

Basic Usage

@aceternity/direction-aware-hover-demo
Basic Usage
tsx
1"use client";
2import React from "react";
3import { motion } from "framer-motion";
4import { cn } from "@/lib/utils";
5
6export const DirectionAwareHover = ({
7    className,
8    children,
9    ...props
10}) => {
11  return (
12    <motion.div
13      whileHover={{ scale: 1.02 }}
14      transition={{ type: "spring", stiffness: 300 }}
15      className={cn(
16        "relative rounded-2xl border border-neutral-200 bg-white p-6 shadow-lg",
17        "dark:border-neutral-800 dark:bg-neutral-900",
18        className
19      )}
20      {...props}
21    >
22      {children}
23    </motion.div>
24  );
25};

With Custom Props

@aceternity/direction-aware-hover-custom-demo
With Custom Props
tsx
1"use client";
2import React from "react";
3import { motion, useMotionValue, useTransform, useSpring } from "framer-motion";
4
5export const DirectionAwareHover = ({ children, className }) => {
6  const x = useMotionValue(0);
7  const y = useMotionValue(0);
8  const rotateX = useTransform(y, [-100, 100], [10, -10]);
9  const rotateY = useTransform(x, [-100, 100], [-10, 10]);
10  const springX = useSpring(rotateX, { damping: 20 });
11  const springY = useSpring(rotateY, { damping: 20 });
12
13  return (
14    <motion.div
15      onMouseMove={(e) => {
16        const rect = e.currentTarget.getBoundingClientRect();
17        x.set(e.clientX - rect.left - rect.width / 2);
18        y.set(e.clientY - rect.top - rect.height / 2);
19      }}
20      onMouseLeave={() => { x.set(0); y.set(0); }}
21      style={{ rotateX: springX, rotateY: springY, transformPerspective: 1000 }}
22      className={className}
23    >
24      {children}
25    </motion.div>
26  );
27};

Props

Prop Type Default Description Required
className string "" Additional CSS classes No
children React.ReactNode - Child elements Yes
delay number 0 Animation delay in seconds No
duration number 0.5 Animation duration in seconds No
variant "default" | "outline" | "ghost" "default" Visual variant No
onClick () => void - Click handler No