以下是一个完整的解决方案,包括 switch-card
勾和一个示例 Vue 项目:
switch-card 勾代码
将这个文件保存为 src/hooks/switch-card.js
:
<<<<<<< SEARCH
=======
const SwitchCard = () => {
const switchElements = new Map();
function init() {
// 遍历所有 DOM 元素,查找符合条件的元素并添加到 map
document.querySelectorAll('.switch-element').forEach(element => {
switchElements.set(element.getAttribute('id'), element);
});
// 创建切换动画
const card = switchElements.get(document.querySelector('.currentCardId!-visible'));
if (card) {
createSwitchAnimation(card);
}
}
function createSwitchAnimation(element) {
// 添加中间线
const middleLine = document.createElement('div');
middleLine.className = 'switch-line';
const path = document.createElement('div');
path.style.opacity = '0.5';
path.innerHTML = `
<div class="switch-line"></div>
`;
element.appendChild(middleLine);
element.appendChild(path);
// 定义切换逻辑
function handleSwitch(id) {
switchElements.delete(id);
const target = switchElements.get(document.querySelector(`[id="${id}"]!-visible`));
if (target) {
show背面(target);
show正面(element);
}
}
function show背面(element) {
element.classList.add('visible');
element.classList.remove('hidden');
// Animate flipping
animateFlipping(element, '背面', ['背面']);
}
function show正面(element) {
element.classList.add('hidden');
element.classList.remove('visible');
// Animate flipping
animateFlipping(element, '正面', ['正面']);
}
function animateFlipping(element, side, classNames) {
return new Promise((resolve) => {
const flipElement = switchElements.get(document.querySelector(`[id="${side}"]!-visible`));
if (flipElement) {
element.classList.remove(classNames);
flipElement.classList.add(classNames);
resolve();
}
});
}
// 添加点击事件捕获
element.addEventListener('click', () => {
const id = document.querySelector('.currentCardId!-visible').getAttribute('id');
handleSwitch(id);
});
return () => {
middleLine.remove();
path.remove();
switchElements.clear();
};
}
init();
};
// 示例使用:
const switchElementId = 'currentCardId';
const switchCard = SwitchCard();
function show背面(element) {
element.classList.add('visible');
element.classList.remove('hidden');
// 添加翻转动画
if (typeof CSS animations === 'undefined') {
const style = document.createElement('style');
style.textContent = `
.switch-element {
opacity: 0 !important;
transform: translateY(20px) !important;
}
.背面 {
transform: translateX(-100%) !important;
opacity: 1 !important;
transform: translateY(-20px);
}
`;
document.head.appendChild(style);
}
}
function show正面(element) {
element.classList.add('hidden');
element.classList.remove('visible');
// 移除翻转动画
if (typeof CSS animations === 'undefined') {
const style = document.createElement('style');
style.textContent = `
.switch-element {
opacity: 0 !important;
transform: translateY(20px) !important;
}
.背面 {
transform: translateX(-100%) !important;
opacity: 1 !important;
transform: translateY(-20px);
}
`;
document.head.removeChild(style);
}
}
>>>>>>> REPLACE
Vue 项目示例
将以下代码保存为 src/index.js
:
<<<<<<< SEARCH
=======
import SwitchCard from './hooks/switch-card';
const App = () => {
const switchCard = SwitchCard();
return (
<div className="container">
<h1>Switch Card</h1>
<!-- 主卡片 -->
<div
ref="currentCardId"
className="switch-element"
id="main-card"
style=
>
<p>正面内容</p>
</div>
<!-- 其他卡片 -->
<div
ref="cardId1"
className="switch-element"
id="card-1"
style=
>
<p>背面内容 1</p>
</div>
<div
ref="cardId2"
className="switch-element"
id="card-2"
style=
>
<p>背面内容 2</p>
</div>
</div>
);
};
export default App;
>>>>>>> REPLACE
使用说明
- 将
SwitchCard.js
文件保存到项目根目录下。 - 在你的 Vue 项目中导入并使用 SwitchCard 勾:
import SwitchCard from './hooks/switch-card';
const App = () => {
const switchCard = SwitchCard();
// ... 其他代码 ...
};
功能说明
- 每个卡片都有一个
id
,用于唯一标识。 - 点击卡片时会触发翻转动画:
- 显示背面时,
visible
类别被添加 - 隐藏卡片时,
hidden
类别被移除 - 动画效果通过 CSS 实现
- 显示背面时,
- 可以通过重复点击切换多个方向(背面 → 正面 → 背面)
示例效果
- 主卡片:
- 第一次点击:正面 → 背面
- 第二次点击:背面 → 正面
- 其他卡片:
- 第一次点击:背面内容 1 → 正面内容 1
- 第二次点击:正面内容 1 → 背面内容 1
注意事项
- 确保项目中使用 modern CSS 动画功能(
-webkit-media-axis
,backface blend mode
等) - 根据需要调整卡片的尺寸和样式
- 确保所有
switch-element
卡片具有相同的宽高比
通过这个完整的解决方案,你可以轻松地在 Vue 项目中实现多方向翻转卡片功能。