DEV Community

Vijay Kumar
Vijay Kumar

Posted on

1

How to make slide elements using CSS and Javascript.

HTML

 <section class="container">
      <div class="box">GH</div>
      <div class="box">IJ</div>
      <div class="box">KL</div>
      <div class="box">MN</div>
      <div class="box">OP</div>
      <div class="box">QR</div>
    </section>
    <button class="prev">Prev</button>
    <button class="next">Next</button>
Enter fullscreen mode Exit fullscreen mode

CSS

.container {
  display: flex;  
  width: 100%; 
  overflow: hidden;
  border: 1px solid black;
}
.box {
  min-width: 100%; 
  text-align: center;
  background-color: brown;
  transition: transform 0.5s ease;/
}
Enter fullscreen mode Exit fullscreen mode

JAVASCRIPT

const boxes = document.querySelectorAll(".box");
const prevBtn = document.querySelector(".prev");
const nextBtn = document.querySelector(".next");
const arrLength = boxes.length - 1;

let c = 0;

prevBtn.addEventListener("click", () => updatefnPrev());
nextBtn.addEventListener("click", () => updatefnNext());
function updatefnNext() {
  if (c > arrLength - 1) return;

  c = c++;

  boxes.forEach((box) => {
    box.style.transform = `translateX(-${c * box.offsetWidth}px)`;
  });
}
function updatefnPrev() {
  if (c == 0) return;
  c--;

  boxes.forEach((box) => {
    box.style.transform = `translateX(-${c * box.offsetWidth}px)`;
  });
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series