Skip to content

Commit ab56f1b

Browse files
committed
SMOOOOOTH draghgable item move
1 parent d38d63f commit ab56f1b

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

popup-page-scripts/popup-page-customButtons.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -272,48 +272,41 @@ function handleDragOver(e) {
272272
});
273273

274274
// --- FLIP: Last, Invert, Play for DRAGGED item ---
275-
if (firstDraggedRect) { // Ensure we recorded the initial position
276-
const lastDraggedRect = draggedItem.getBoundingClientRect(); // Last
275+
if (firstDraggedRect) {
276+
const lastDraggedRect = draggedItem.getBoundingClientRect();
277277

278+
// Calculate delta based on the TOP-LEFT corner
278279
const deltaDraggedX = firstDraggedRect.left - lastDraggedRect.left;
279280
const deltaDraggedY = firstDraggedRect.top - lastDraggedRect.top;
280281

281-
// Check if the element's calculated position actually changed
282+
// Define the transform states explicitly
283+
const invertTransform = `translate(${deltaDraggedX}px, ${deltaDraggedY}px) scale(0.8)`;
284+
const playTransform = 'scale(0.8)'; // Target state (scaled, at natural position)
285+
286+
// Check if the element's calculated position actually needs to change
282287
if (deltaDraggedX !== 0 || deltaDraggedY !== 0) {
283-
// Apply inverse transform immediately WITHOUT transition
284-
draggedItem.style.transition = 'transform 0s';
285-
draggedItem.style.transform = `translate(${deltaDraggedX}px, ${deltaDraggedY}px)`;
288+
// Invert: Apply transform immediately, ensuring NO transition happens
289+
draggedItem.style.transition = 'none'; // Explicitly disable transitions
290+
draggedItem.style.transform = invertTransform;
286291

287292
// Force reflow is crucial here
288293
draggedItem.offsetWidth;
289294

290-
// Play: Apply transition and animate back to natural position (transform: '')
291-
// Use a slightly shorter duration as it's actively moving
295+
// Play: Enable transition ONLY for the transform property,
296+
// and set the target transform state.
292297
draggedItem.style.transition = 'transform 150ms ease-out';
293-
draggedItem.style.transform = '';
294-
295-
// Cleanup: Remove transition after animation.
296-
// NOTE: This might be problematic due to rapid 'dragover' events.
297-
// A flag or debouncing might be needed for robust cleanup.
298-
// For simplicity here, we'll use 'once', but be aware it might
299-
// get interrupted by the next dragover event.
300-
draggedItem.addEventListener('transitionend', () => {
301-
// Only remove transition if it hasn't been reset by another dragover
302-
if (draggedItem.style.transition.includes('150ms')) {
303-
draggedItem.style.transition = '';
304-
}
305-
}, { once: true });
298+
draggedItem.style.transform = playTransform;
299+
300+
// Cleanup will implicitly happen on the next dragover or dragend.
306301

307302
} else {
308-
// If it didn't move position-wise, ensure no leftover styles
309-
// Check if a transition is currently active from a previous move
310-
// If not, clear styles. If yes, let it finish.
311-
if (!draggedItem.style.transition.includes('150ms')) {
312-
draggedItem.style.transition = '';
313-
draggedItem.style.transform = '';
314-
}
303+
// If DOM position didn't change, ensure it still has the correct base dragging transform
304+
// and importantly, ensure no transition is active from a previous interrupted move.
305+
draggedItem.style.transition = 'none'; // Remove any potentially active transition
306+
draggedItem.style.transform = playTransform; // Ensure the scale(0.8) is applied
315307
}
316308
}
309+
317310
} // end if(domChanged)
318311
}
319312

0 commit comments

Comments
 (0)