萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

UI 交互模式切換特效:編輯模式的滑入式工具列設計與實作

模式切換的設計意圖

當使用者進入編輯模式 (如列表項可刪除、排序、編輯),需要額外控制項。模式切換特效透過「滑動位移 + 工具列滑入」雙重動畫,強調新控制項的重要性,並保持空間連貫性。

交互流程

  1. 常態:列表項顯示內容,右側顯示「編輯」按鈕或長按觸發。
  2. 進入編輯:
    • 列表項內容向左滑動 (位移 ≈ 工具列寬度)。
    • 編輯工具列 (刪除、編輯、拖曳把手) 從右側滑入。
    • 標題/標籤同步位移,強化「模式改變」認知。
  3. 編輯操作:使用者點擊工具列按鈕執行動作。
  4. 退出編輯:
    • 點擊完成、空白處、Esc 鍵、或執行破壞性動作 (刪除) 後。
    • 工具列滑出螢幕,內容滑回原位。

技術實作 (Vue 3 Composition API 範例)

<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
  items: Array,
  editable: { type: Boolean, default: true }
});

const editingId = ref(null);
const itemWidth = ref(0); // 動態計算工具列寬度

const itemStyle = (id) => {
  if (editingId.value !== id) return {};
  const offset = `-${itemWidth.value}px`;
  return { transform: `translateX(${offset})` };
};

const actionsStyle = (id) => {
  if (editingId.value !== id) return { transform: 'translateX(100%)' };
  return { transform: 'translateX(0)' };
};

function enterEdit(id, el) {
  if (!props.editable) return;
  itemWidth.value = el.querySelector('.item-actions').offsetWidth;
  editingId.value = id;
}

function leaveEdit() {
  editingId.value = null;
}

function handleAction(action, item) {
  // 觸發父組件事件或直接處理
  $emit(action, item);
  if (action === 'delete') leaveEdit(); // 刪除後自動關閉
}
</script>

<template>
  <ul class="editable-list">
    <li v-for="item in items" :key="item.id" class="list-item">
      <div class="item-content" :style="itemStyle(item.id)" @click="enterEdit(item.id, $el)">
        <slot :item="item">{{ item.label }}</slot>
      </div>
      <div class="item-actions" :style="actionsStyle(item.id)">
        <button @click.stop="handleAction('edit', item)" aria-label="編輯">✎</button>
        <button @click.stop="handleAction('delete', item)" aria-label="刪除" class="destructive">🗑</button>
        <button @click.stop="leaveEdit" aria-label="完成">✓</button>
      </div>
    </li>
  </ul>
</template>

<style scoped>
.list-item {
  position: relative;
  overflow: hidden;
  background: white;
  border-bottom: 1px solid #eee;
}
.item-content {
  display: flex; align-items: center; padding: 1rem;
  transition: transform 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  cursor: pointer;
}
.item-actions {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0 1rem;
  background: #fff5f5;
  border-left: 1px solid #fed7d7;
  transition: transform 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  will-change: transform;
}
.item-actions button {
  background: none; border: none; font-size: 1.2rem; cursor: pointer;
  padding: 0.5rem; border-radius: 4px;
  transition: background 0.15s;
}
.item-actions button:hover { background: #fee; }
.item-actions button.destructive { color: #e53e3e; }
</style>

關鍵設計決策

決策點選擇理由
動畫驅動CSS Transform + TransitionGPU 加速、不觸發佈局、可被瀏覽器最佳化
緩動函數Material Design Standard Easing cubic-bezier(0.25, 0.46, 0.45, 0.94)自然、有機感、進場快離場慢
工具列寬度動態測量 offsetWidth適應內容變化 (國際化、響應式)、避免硬編碼
單項編輯同一時間僅一項進入編輯模式避免認知負荷過高、簡化狀態管理
焦點管理進入編輯聚焦首按鈕、退出回歸觸發元素鍵盤/螢幕閱讀器可用

變體與延伸

  • 批次編輯模式:頂部工具列滑下,每項顯示核取方塊,底部浮動批次操作列。
  • 拖曳排序:編輯模式顯示把手 ☰,配合 @dnd-kit/core 或 HTML5 Drag API。
  • 右滑單一動作:右滑超過閾值直接觸發主要動作 (如「刪除」或「歸檔」),無需進入完整編輯模式 (Gmail/Outlook 風格)。

模式切換特效的核心在於「空間位移傳達狀態變化」。透過內容讓位、工具滑入的雙重動畫,使用者能瞬間理解:當前進入特定操作模式,完成後將回歸常態。這種空間敘事比單純顯示/隱藏更符合人類認知直覺。

copyright © 萬盛學電腦網 all rights reserved