JSFiddle - React, Tailwind, and code Playground

by gilbertomg

HTML

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <div class="container mt-5">
    <div class="row g-1">
      <div class="col-1 ">
        <div id="prevBtn" class="button-container h-100 rounded bg-warning" onclick="prevStep()"><i class="bi bi-arrow-left"></i></div>
      </div>
      <div class="col-10">
        <div class=" text-center text-dark border rounded shadow">
          <div id="step-container" class="card-body m-1 rounded" style="background-color:var(--bs-success-bg-subtle)">
            <div id="step-1" class="step-content active">
              <p>Frase 1: Este é o primeiro passo.</p>
            </div>
            <div id="step-2" class="step-content">
              <div>
                Selecione um dos botões
                <div>
                  <button type="button" name="button"> 1 </button>
                  <button type="button" name="button"> 2 </button>
                  <button type="button" name="button"> 3 </button>
                </div>
              </div>
            </div>
            <div id="step-3" class="step-content">
              <p>Frase 3: Este é o terceiro passo.</p>
            </div>
            <div id="step-4" class="step-content">
              <p>Frase 4: Este é o quarto passo.</p>
            </div>
            <div id="step-5" class="step-content">
              <p>Frase 5: Este é o quinto passo.</p>
            </div>
            <div id="step-6" class="step-content">
              <p>
                ver since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
                It was popularised in the 1960s with the release of Letraset sheets
              </p>
           ...

CSS

.step-content {
      display: none;
    }

    .step-content.active {
      display: inline-block;
    }

    #step-container {
      height: 100px;
      overflow-y: hidden;
    }

    .button-container {
      display: flex;
      justify-content: center;
      align-items: center;
    }

JavaScript

let currentStep = 1;
    const totalSteps = 6;

    function showStep(step) {
      for (let i = 1; i <= totalSteps; i++) {
        document.getElementById(`step-${i}`).classList.remove('active');
      }
      document.getElementById(`step-${step}`).classList.add('active');
      updateButtons();
    }

    function nextStep() {
      if (currentStep < totalSteps) {
        currentStep++;
        showStep(currentStep);
      }
    }

    function prevStep() {
      if (currentStep > 1) {
        currentStep--;
        showStep(currentStep);
      }
    }

    function updateButtons() {
      document.getElementById('prevBtn').disabled = currentStep === 1;
      document.getElementById('nextBtn').disabled = currentStep === totalSteps;
    }

    // Initial button state update
    updateButtons();

    // Touch event handling
    let touchstartX = 0;
    let touchendX = 0;

    function handleGesture() {
      if (touchendX < touchstartX - 50) {
        nextStep();
      }
      if (touchendX > touchstartX + 50) {
        prevStep();
      }
    }

    document.addEventListener('touchstart', function(event) {
      touchstartX = event.changedTouches[0].screenX;
    }, false);

    document.addEventListener('touchend', function(event) {
      touchendX = event.changedTouches[0].screenX;
      handleGesture();
    }, false);