JSFiddle - React, Tailwind, and code Playground
by mehulkar
HTML
<div class="wrapper">
<div class="item"></div>
</div>
<button onclick="window.moveLeft()">Left</button>
<button onclick="window.moveRight()">Right</button>
CSS
.wrapper {
width: 400px;
height: 100px;
background: bisque;
border-radius: 12px;
position: relative;
overflow-x: hidden;
}
.item {
height: 100px;
width: 100px;
position: absolute;
background: red;
right: 0;
}
JavaScript
let current = 0;
const STEP = 10;
window.moveLeft = function() {
move(-1);
}
window.moveRight = function() {
move();
}
function move(direction = 1) {
const item = document.querySelector('.item');
const nextPosition = current + (STEP * direction);
current = nextPosition;
item.style.transform = `translateX(${nextPosition}px)`;
}