Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Stop CSS transition (in progress) and start from beginning

If I click keyboard arrow keys (next/previous), before previously transition is finished I want to start from the beginning.

That should happen both in handleNext & handlePrevious

Now it's not returning to base position (if I navigate before transition finished), even though I tried to force it with:

card.style.transition = "";
card.style.transform = "";

and than call handleMoving() to start moving again.

I also tried with removeEventListener, but something strange is happening.

Maybe somehow to remove transitionend and set it again?

WHOLE CODE:

document.body.onkeydown = handleKeyDown;

const card = document.getElementById('card');
let direction;


function handleKeyDown(e) {
    if(e.key === "ArrowRight") handleNext();
    if(e.key === "ArrowLeft") handlePrevious();
}

function handleNext() {
    direction = "next"
    
    // If I click arrow next/previous before previous transition is finished I want to start from the beginning
    
    /* NOT WORKING */
    
    disableTransition(); // Here I want to disable previously transition that is in proccess (probably)
    removeTransform(); // Remove translateX -2rem or 2rem, be ready from 0px everytime
    
    /* END NOT WORKING */
    
    handleMoving(); // Prepared from start, handle moving again
}

function handlePrevious() {
    direction = "previous"
    
    disableTransition();
    removeTransform();
    
    handleMoving();
}

function handleMoving() {
    enableTransition();
    addTransform();
}

function enableTransition() {
    card.style.transition = "transform .5s ease-in-out";
}

function disableTransition() {
    card.style.transition = "";
}

function addTransform() {
    card.style.transform = (direction === 'previous') ? "translate3d(-2rem, 0, 0)" : "translate3d(2rem, 0, 0)";
    card.addEventListener('transitionend', removeTransform, {once: true});
}

function removeTransform() {
    card.style.transform = "";
}
.card {
  position: absolute;
  inset: 0;
  border: 2px solid black;
}

.card-wrapper {
  position: relative;
  width: 300px;
  height: 150px;
  background: lightgray;
  margin: auto;
}
<div class="card-wrapper">
  <div id="card" class="card"></div>
</div>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!