Communitygithub.com

互動設計

設計並實現微互動、動態設計、轉場效果以及使用者回饋模式。適用於為 UI 互動增添精緻感、實作載入狀態,或打造令人愉悅的使用者體驗。

互動設計 是什麼?

此技能專注於打造引人入勝且直覺化的互動體驗,透過流暢的動態效果、即時且饒富意義的回饋、以及深思熟慮的狀態轉場,來提升可用性並為使用者帶來愉悅感。它涵蓋了從按鈕微互動、頁面轉場,到載入狀態骨架螢幕、手勢操作、拖放介面乃至於通知系統等一系列常見的前端互動模式。每個模式都提供了可直接使用的 React(搭配 Framer Motion)與 CSS 範例程式碼,並強調了效能最佳化與無障礙設計的重要性,例如使用 `transform` 和 `opacity` 屬性以達到 60fps 的流暢度,以及實作 `prefers-reduced-motion` 媒體查詢來尊重使用者的動態偏好。此技能特別適合在 React、Next.js、Vue 或任何支援 CSS 動畫的 Web 專案中,為應用程式注入生命與個性。

相容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/itsmeyaw/skillset-website/tree/HEAD/.agents/skills/interaction-design

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

互動設計 是做什麼的?

透過動態效果、回饋機制與精巧的狀態轉場,打造引人入勝且直覺的互動體驗,提升可用性並為使用者帶來愉悅感。

使用時機

  • 為使用者回饋增添微互動
  • 實作流暢的頁面與元件轉場
  • 設計載入狀態與骨架螢幕
  • 建立基於手勢的互動
  • 建構通知與提示系統
  • 實作拖放介面
  • 新增觸發於捲動的動畫
  • 設計滑鼠懸停與焦點狀態

核心原則

1. 目的性動態

動態應該用來傳達資訊,而非裝飾:

  • 回饋:確認使用者的操作已發生
  • 方向:顯示元素從何而來、往何處去
  • 焦點:將注意力引導至重要的變化
  • 連續性:在轉場過程中維持情境脈絡

2. 時間指南

持續時間使用情境
100-150ms微回饋(滑鼠懸停、點擊)
200-300ms小型轉場(開關、下拉選單)
300-500ms中型轉場(模態視窗、頁面切換)
500ms+複雜的編排動畫

3. 緩動函數

/* 常用緩動函數 */
--ease-out: cubic-bezier(0.16, 1, 0.3, 1); /* 減速 - 進入 */
--ease-in: cubic-bezier(0.55, 0, 1, 0.45); /* 加速 - 離開 */
--ease-in-out: cubic-bezier(0.65, 0, 0.35, 1); /* 加減速 - 兩者之間 */
--spring: cubic-bezier(0.34, 1.56, 0.64, 1); /* 彈性過衝 - 活潑感 */

快速入門:按鈕微互動

import { motion } from "framer-motion";

export function InteractiveButton({ children, onClick }) {
  return (
    <motion.button
      onClick={onClick}
      whileHover={{ scale: 1.02 }}
      whileTap={{ scale: 0.98 }}
      transition={{ type: "spring", stiffness: 400, damping: 17 }}
      className="px-4 py-2 bg-blue-600 text-white rounded-lg"
    >
      {children}
    </motion.button>
  );
}

互動模式

1. 載入狀態

骨架螢幕:載入時保留頁面佈局

function CardSkeleton() {
  return (
    <div className="animate-pulse">
      <div className="h-48 bg-gray-200 rounded-lg" />
      <div className="mt-4 h-4 bg-gray-200 rounded w-3/4" />
      <div className="mt-2 h-4 bg-gray-200 rounded w-1/2" />
    </div>
  );
}

進度指示器:顯示明確的進度

function ProgressBar({ progress }: { progress: number }) {
  return (
    <div className="h-2 bg-gray-200 rounded-full overflow-hidden">
      <motion.div
        className="h-full bg-blue-600"
        initial={{ width: 0 }}
        animate={{ width: `${progress}%` }}
        transition={{ ease: "easeOut" }}
      />
    </div>
  );
}

2. 狀態轉場

帶有平滑轉場的開關:

function Toggle({ checked, onChange }) {
  return (
    <button
      role="switch"
      aria-checked={checked}
      onClick={() => onChange(!checked)}
      className={`
        relative w-12 h-6 rounded-full transition-colors duration-200
        ${checked ? "bg-blue-600" : "bg-gray-300"}
      `}
    >
      <motion.span
        className="absolute top-1 left-1 w-4 h-4 bg-white rounded-full shadow"
        animate={{ x: checked ? 24 : 0 }}
        transition={{ type: "spring", stiffness: 500, damping: 30 }}
      />
    </button>
  );
}

3. 頁面轉場

Framer Motion 佈局動畫:

import { AnimatePresence, motion } from "framer-motion";

function PageTransition({ children, key }) {
  return (
    <AnimatePresence mode="wait">
      <motion.div
        key={key}
        initial={{ opacity: 0, y: 20 }}
        animate={{ opacity: 1, y: 0 }}
        exit={{ opacity: 0, y: -20 }}
        transition={{ duration: 0.3 }}
      >
        {children}
      </motion.div>
    </AnimatePresence>
  );
}

4. 回饋模式

點擊漣漪效果:

function RippleButton({ children, onClick }) {
  const [ripples, setRipples] = useState([]);

  const handleClick = (e) => {
    const rect = e.currentTarget.getBoundingClientRect();
    const ripple = {
      x: e.clientX - rect.left,
      y: e.clientY - rect.top,
      id: Date.now(),
    };
    setRipples((prev) => [...prev, ripple]);
    setTimeout(() => {
      setRipples((prev) => prev.filter((r) => r.id !== ripple.id));
    }, 600);
    onClick?.(e);
  };

  return (
    <button onClick={handleClick} className="relative overflow-hidden">
      {children}
      {ripples.map((ripple) => (
        <span
          key={ripple.id}
          className="absolute bg-white/30 rounded-full animate-ripple"
          style={{ left: ripple.x, top: ripple.y }}
        />
      ))}
    </button>
  );
}

5. 手勢互動

滑動關閉:

function SwipeCard({ children, onDismiss }) {
  return (
    <motion.div
      drag="x"
      dragConstraints={{ left: 0, right: 0 }}
      onDragEnd={(_, info) => {
        if (Math.abs(info.offset.x) > 100) {
          onDismiss();
        }
      }}
      className="cursor-grab active:cursor-grabbing"
    >
      {children}
    </motion.div>
  );
}

CSS 動畫模式

關鍵幀動畫

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.animate-fadeIn {
  animation: fadeIn 0.3s ease-out;
}
.animate-pulse {
  animation: pulse 2s ease-in-out infinite;
}
.animate-spin {
  animation: spin 1s linear infinite;
}

CSS 轉場

.card {
  transition:
    transform 0.2s ease-out,
    box-shadow 0.2s ease-out;
}

.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
}

無障礙設計考量

/* 尊重使用者的動態偏好 */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
function AnimatedComponent() {
  const prefersReducedMotion = window.matchMedia(
    "(prefers-reduced-motion: reduce)",
  ).matches;

  return (
    <motion.div
      animate={{ opacity: 1 }}
      transition={{ duration: prefersReducedMotion ? 0 : 0.3 }}
    />
  );
}

最佳實踐

  1. 效能優先:使用 transform 和 opacity 以達到流暢的 60fps
  2. 減少動態支援:務必尊重 prefers-reduced-motion
  3. 一致的時間控制:在整個應用程式中使用統一的時間比例尺
  4. 自然的物理特性:優先使用彈性動畫而非線性動畫
  5. 可中斷性:允許使用者取消長時間的動畫
  6. 漸進增強:在無 JavaScript 動畫的情況下仍可運作
  7. 在實際裝置上測試:效能差異可能很大

常見問題

  • 卡頓動畫:避免對 width、height、top、left 進行動畫
  • 過度動畫:過多的動態效果會造成視覺疲勞
  • 阻斷互動:動畫進行中絕不應阻止使用者輸入
  • 記憶體洩漏:元件卸載時應清除動畫監聽器
  • 內容閃爍:謹慎使用 will-change 進行最佳化

Individual skills in this repo

This repo contains 1 individual skill — each has its own dedicated page.

相關技能