How to select all elements in Bootstrap pagination code using JavaScript?
P粉165522886
P粉165522886 2024-02-25 23:29:10
0
1
333

I'm using Bootstrap for a website I'm preparing. Since the overflow scrollbar looked bad, I hid it with CSS and tried scrolling the pagination with the mouse wheel via JavaScript.

When I use the code given below, the top code works but the bottom code does not. When I give [0,1] as index value in JavaScript code, this time the bottom index value works but the top index value doesn't work. How can I make it work on two paginations on the same page? From what I understand from research, I may need to use let or var variables instead of const.

const container = document.querySelectorAll(".table-responsive")[0];
container.addEventListener("wheel", function (e) {
  if (e.deltaY > 0) {
    container.scrollLeft += 100;
    e.preventDefault();
  } else {
    container.scrollLeft -= 100;
    e.preventDefault();
  }
});
.table-responsive::-webkit-scrollbar {
  width: 0 !important;
}

.content {
  width: 100%;
  height: 250px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 3em;
}
<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css">
  <title>Hello, world!</title>
</head>
<br>
<br>

<body>
  <div class="container">
    <nav aria-label="Page navigation example" class="table-responsive">
      <ul class="pagination justify-content-between pagination-md">
        <li class="page-item m-1 active"><a class="page-link" href="#">1</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">2</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">3</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">4</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">5</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">6</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">7</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">8</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">9</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">10</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">11</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">12</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">13</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">14</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">15</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">16</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">17</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">18</a>
        </li>

      </ul>
    </nav>
    <div class="content">
      Some Page Content
    </div>

    <nav aria-label="Page navigation example" class="table-responsive">
      <ul class="pagination justify-content-between pagination-md">
        <li class="page-item m-1 active"><a class="page-link" href="#">1</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">2</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">3</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">4</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">5</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">6</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">7</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">8</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">9</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">10</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">11</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">12</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">13</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">14</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">15</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">16</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">17</a>
        </li>
        <li class="page-item m-1"><a class="page-link" href="#">18</a>
        </li>

      </ul>
    </nav>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
    crossorigin="anonymous"></script>

</body>

</html>

P粉165522886
P粉165522886

reply all(1)
P粉676821490

You can use querySelectorAll to get all elements of that class and then loop through them to add an event listener to each element.

const containers = document.querySelectorAll(".table-responsive");
containers.forEach(container => {
  container.addEventListener("wheel", function (e) {
    if (e.deltaY > 0) {
      container.scrollLeft = 100;
      e.preventDefault();
    } else {
      container.scrollLeft -= 100;
      e.preventDefault();
    }
  });
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template