Skip to content

Commit 792f603

Browse files
committed
优化翻转卡片功能,简化事件处理逻辑,提升用户体验
1 parent 17e8553 commit 792f603

File tree

2 files changed

+34
-115
lines changed

2 files changed

+34
-115
lines changed

client/css/body.css

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ body {
99
perspective: 1000px;
1010
max-width: 400px;
1111
width: 100%;
12-
height: 500px; /* 固定高度,避免翻转时高度变化 */
12+
height: 500px;
13+
position: relative;
1314
}
1415

1516
.flip-card-inner {
1617
position: relative;
1718
width: 100%;
1819
height: 100%;
1920
text-align: center;
20-
transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
21+
transition: transform 0.3s ease;
2122
transform-style: preserve-3d;
22-
transform-origin: center center; /* 设置旋转中心为卡片中心 */
23-
transform: rotateX(0deg); /* 初始状态 */
23+
}
24+
25+
.flip-card-inner.flipped {
26+
transform: rotateX(180deg);
2427
}
2528

2629
.flip-card-front,
@@ -30,14 +33,14 @@ body {
3033
left: 0;
3134
width: 100%;
3235
height: 100%;
33-
backface-visibility: hidden;
3436
border-radius: 14px;
3537
padding: 39px 15px 20px 15px;
3638
box-shadow: 0 2px 16px rgba(60, 80, 100, 0.09);
3739
background: #fff;
3840
box-sizing: border-box;
3941
display: flex;
4042
flex-direction: column;
43+
backface-visibility: hidden;
4144
}
4245

4346
.flip-card-back {
@@ -63,7 +66,7 @@ body {
6366
z-index: 10;
6467
}
6568

66-
/* Firefox兼容性修复:控制翻转时按钮的可见性 */
69+
/* 控制翻转时按钮的可见性 */
6770
.flip-card-inner.flipped .flip-card-front .help-btn {
6871
visibility: hidden;
6972
opacity: 0;

client/js/ui.js

Lines changed: 25 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -617,117 +617,33 @@ export function initFlipCard() {
617617

618618
if (!flipCard || !helpBtn || !backBtn) return;
619619

620-
// 检测是否为触摸设备 / Detect touch device
621-
// 更准确的触摸设备检测方法
622-
const isTouchDevice = window.matchMedia('(hover: none) and (pointer: coarse)').matches;
620+
const flipCardInner = flipCard.querySelector('.flip-card-inner');
621+
if (!flipCardInner) return;
623622

624-
// 当前旋转角度 / Current rotation angle
625-
let currentRotation = 0;
626-
// 执行翻转操作 / Execute flip operation
627-
function performFlip() {
628-
currentRotation += 180;
629-
const flipCardInner = flipCard.querySelector('.flip-card-inner');
630-
if (flipCardInner) {
631-
flipCardInner.style.transform = `rotateX(${currentRotation}deg)`;
632-
633-
// 添加或移除flipped类来控制按钮可见性(Firefox兼容性修复)
634-
if (currentRotation % 360 === 180 || currentRotation % 360 === -180) {
635-
flipCardInner.classList.add('flipped');
636-
} else {
637-
flipCardInner.classList.remove('flipped');
638-
}
639-
}
640-
}
623+
// 翻转状态
624+
let isFlipped = false;
641625

642-
if (isTouchDevice) {
643-
// 移动端:点击切换 / Mobile: click to toggle
644-
helpBtn.addEventListener('click', (e) => {
645-
e.preventDefault();
646-
e.stopPropagation();
647-
performFlip();
648-
});
649-
650-
backBtn.addEventListener('click', (e) => {
651-
e.preventDefault();
652-
e.stopPropagation();
653-
performFlip();
654-
}); } else {
655-
// 桌面端:鼠标悬停持续旋转 / Desktop: hover to continuously rotate
656-
let rotationInterval;
657-
let isHovering = false;
658-
659-
// 开始持续旋转
660-
function startContinuousRotation() {
661-
if (isHovering) return; // 已经在旋转中
662-
isHovering = true;
663-
664-
// 立即执行一次旋转
665-
performFlip();
666-
667-
// 然后每800ms旋转一次(与CSS动画时长一致)
668-
rotationInterval = setInterval(() => {
669-
if (isHovering) {
670-
performFlip();
671-
}
672-
}, 800);
673-
}
674-
675-
// 停止持续旋转
676-
function stopContinuousRotation() {
677-
isHovering = false;
678-
if (rotationInterval) {
679-
clearInterval(rotationInterval);
680-
rotationInterval = null;
681-
}
682-
}
683-
684-
// 检查鼠标是否在任一按钮上
685-
function isMouseOverButton(e) {
686-
const helpBtnRect = helpBtn.getBoundingClientRect();
687-
const backBtnRect = backBtn.getBoundingClientRect();
688-
const x = e.clientX;
689-
const y = e.clientY;
690-
691-
// 检查是否在帮助按钮上
692-
const onHelpBtn = x >= helpBtnRect.left && x <= helpBtnRect.right &&
693-
y >= helpBtnRect.top && y <= helpBtnRect.bottom;
694-
695-
// 检查是否在返回按钮上
696-
const onBackBtn = x >= backBtnRect.left && x <= backBtnRect.right &&
697-
y >= backBtnRect.top && y <= backBtnRect.bottom;
698-
699-
return onHelpBtn || onBackBtn;
626+
// 简单的翻转函数
627+
function toggleFlip() {
628+
isFlipped = !isFlipped;
629+
if (isFlipped) {
630+
flipCardInner.classList.add('flipped');
631+
} else {
632+
flipCardInner.classList.remove('flipped');
700633
}
701-
702-
// 监听整个卡片的鼠标移动
703-
flipCard.addEventListener('mousemove', (e) => {
704-
if (isMouseOverButton(e)) {
705-
if (!isHovering) {
706-
startContinuousRotation();
707-
}
708-
} else {
709-
if (isHovering) {
710-
stopContinuousRotation();
711-
}
712-
}
713-
});
714-
715-
// 监听鼠标离开卡片
716-
flipCard.addEventListener('mouseleave', () => {
717-
stopContinuousRotation();
718-
});
719-
720-
// 按钮点击事件(保持原有功能)
721-
helpBtn.addEventListener('click', (e) => {
722-
e.preventDefault();
723-
e.stopPropagation();
724-
performFlip();
725-
});
726-
727-
backBtn.addEventListener('click', (e) => {
728-
e.preventDefault();
729-
e.stopPropagation();
730-
performFlip();
731-
});
732634
}
635+
636+
// 帮助按钮点击事件
637+
helpBtn.addEventListener('click', (e) => {
638+
e.preventDefault();
639+
e.stopPropagation();
640+
toggleFlip();
641+
});
642+
643+
// 返回按钮点击事件
644+
backBtn.addEventListener('click', (e) => {
645+
e.preventDefault();
646+
e.stopPropagation();
647+
toggleFlip();
648+
});
733649
}

0 commit comments

Comments
 (0)