跳跃式位置移动,暂不平滑

This commit is contained in:
WindowBird 2025-11-03 09:18:31 +08:00
parent ea139100e6
commit 9169b7e838

View File

@ -215,6 +215,7 @@ const translateX = ref(-375); // 当前滑动偏移量,初始值设为 -375
const baseTranslateX = ref(-375); // const baseTranslateX = ref(-375); //
const isAnimating = ref(false); // const isAnimating = ref(false); //
const isDragging = ref(false); // const isDragging = ref(false); //
const isInitialized = ref(false); //
// //
const initScreenWidth = () => { const initScreenWidth = () => {
@ -222,10 +223,11 @@ const initScreenWidth = () => {
success: (res) => { success: (res) => {
const width = res.windowWidth || res.screenWidth || 375; const width = res.windowWidth || res.screenWidth || 375;
screenWidth.value = width; screenWidth.value = width;
// //
if (translateX.value === -375) { if (!isInitialized.value) {
translateX.value = -width; translateX.value = -width;
baseTranslateX.value = -width; baseTranslateX.value = -width;
isInitialized.value = true;
} }
} }
}); });
@ -249,10 +251,10 @@ const handleTouchMove = (e) => {
const deltaX = touch.clientX - touchStartX.value; const deltaX = touch.clientX - touchStartX.value;
const deltaY = Math.abs(touch.clientY - touchStartY.value); const deltaY = Math.abs(touch.clientY - touchStartY.value);
// // //
if (Math.abs(deltaX) > deltaY && Math.abs(deltaX) > 10) { // if (Math.abs(deltaX) > deltaY && Math.abs(deltaX) > 10) {
e.preventDefault(); // e.preventDefault();
} // }
// //
translateX.value = baseTranslateX.value + deltaX; translateX.value = baseTranslateX.value + deltaX;
@ -280,10 +282,10 @@ const handleTouchEnd = (e) => {
if (isHorizontalSwipe) { if (isHorizontalSwipe) {
// //
if (deltaX > 0) { if (deltaX > 0) {
// //
// slideToPreviousDay(); slideToPreviousDay();
} else { } else {
// //
slideToNextDay(); slideToNextDay();
} }
} else { } else {
@ -306,12 +308,12 @@ const resetToCenter = () => {
const slideToPreviousDay = () => { const slideToPreviousDay = () => {
isAnimating.value = true; isAnimating.value = true;
// -screenWidth * 2 // -screenWidth * 2
const targetX = -screenWidth.value*2; // const targetX = -screenWidth.value*2;
translateX.value = targetX; // translateX.value = targetX;
setTimeout(() => { setTimeout(() => {
// transition // transition
isAnimating.value = false; // isAnimating.value = false;
// transition // transition
setTimeout(() => { setTimeout(() => {
@ -327,14 +329,15 @@ const slideToPreviousDay = () => {
console.log(`日期切换:上一天,新日期:${selectedDate.value}`); console.log(`日期切换:上一天,新日期:${selectedDate.value}`);
}, 16); // transition }, 16); // transition
}, 1000); }, 1000);
isAnimating.value = false;
}; };
// //
const slideToNextDay = () => { const slideToNextDay = () => {
isAnimating.value = true; isAnimating.value = true;
// 0 // 0
const targetX = 0; // const targetX = 0;
translateX.value = targetX; // translateX.value = targetX;
setTimeout(() => { setTimeout(() => {
// transition // transition
@ -353,7 +356,7 @@ const slideToNextDay = () => {
console.log(`日期切换:下一天,新日期:${selectedDate.value}`); console.log(`日期切换:下一天,新日期:${selectedDate.value}`);
}, 16); // transition }, 16); // transition
}, 1000); }, 300);
}; };
// //