限制 JavaScript 中的功能范围(滑块用例)?

Restrict the scope of functions in JavaScript ( Slider Use Case )?

在下面的脚本中,当有一个滑块时代码有效,但是当页面上的滑块数量超过一个时,下一个滑块不起作用。

当我们在页面上有多个滑块时,如何更改此脚本以使每个滑块“可拖动功能”单独工作且不重叠?

需要进行哪些更改才能使此代码 “可拖动功能” 对每个滑块单独工作?

如何使用“querySelectorAll”和“forEach”来处理这个问题?

const slider = document.querySelector('.items');
let isDown = false;
let startX;
let scrollLeft;

slider.addEventListener('mousedown', (e) => {
  isDown = true;
  slider.classList.add('active');
  startX = e.pageX - slider.offsetLeft;
  scrollLeft = slider.scrollLeft;
});
slider.addEventListener('mouseleave', () => {
  isDown = false;
  slider.classList.remove('active');
});
slider.addEventListener('mouseup', () => {
  isDown = false;
  slider.classList.remove('active');
});
slider.addEventListener('mousemove', (e) => {
  if (!isDown) return;
  e.preventDefault();
  const x = e.pageX - slider.offsetLeft;
  const walk = (x - startX) * 3; //scroll-fast
  slider.scrollLeft = scrollLeft - walk;
});
.items,
.items_1 {
  position: relative;
  width: 90%;
  background: green;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  transition: all 0.2s;
  transform: scale(0.98);
  will-change: transform;
  user-select: none;
  cursor: pointer;

  margin: 2rem auto;
  border: 3px solid violet;

}

.item {
  display: inline-block;
  background: red;
  min-height: 50px;
  min-width: 150px;
  margin: .5rem;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>slider</title>
  <link rel="stylesheet" href="./style.css">
</head>


<body>

  <div class="items">

    <div class="item"></div>
    <div class="item"></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>

  </div>


  <div class="items">

    <div class="item"></div>
    <div class="item"></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>

  </div>

  <div class="items">

    <div class="item"></div>
    <div class="item"></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>

  </div>


  <script src="./script.js"></script>

</body>

</html>

How can I handle this with " querySelectorAll " and " forEach " ?

只需将声明状态变量并将事件处理程序附加到 一个 滑块的代码放入这样的循环中:

const sliders = document.querySelectorAll('.items');
for (const slider of sliders) {
  let isDown = false;
  let startX;
  let scrollLeft;

  slider.addEventListener('mousedown', (e) => {
    isDown = true;
    slider.classList.add('active');
    startX = e.pageX - slider.offsetLeft;
    scrollLeft = slider.scrollLeft;
  });
  slider.addEventListener('mouseleave', () => {
    isDown = false;
    slider.classList.remove('active');
  });
  slider.addEventListener('mouseup', () => {
    isDown = false;
    slider.classList.remove('active');
  });
  slider.addEventListener('mousemove', (e) => {
    if (!isDown) return;
    e.preventDefault();
    const x = e.pageX - slider.offsetLeft;
    const walk = (x - startX) * 3; //scroll-fast
    slider.scrollLeft = scrollLeft - walk;
  });
}
.items {
  position: relative;
  width: 90%;
  background: green;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  transition: all 0.2s;
  transform: scale(0.98);
  will-change: transform;
  user-select: none;
  cursor: pointer;

  margin: 2rem auto;
  border: 3px solid violet;

}
.items.active {
  border-color: blue;
}

.item {
  display: inline-block;
  background: red;
  min-height: 50px;
  min-width: 150px;
  margin: .5rem;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>slider</title>
  <link rel="stylesheet" href="./style.css">
</head>


<body>

  <div class="items">

    <div class="item"></div>
    <div class="item"></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>

  </div>


  <div class="items">

    <div class="item"></div>
    <div class="item"></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>

  </div>

  <div class="items">

    <div class="item"></div>
    <div class="item"></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>
    <div class="item "></div>

  </div>


  <script src="./script.js"></script>

</body>

</html>