在js中拖动滚动时防止点击

prevent click when leave drag to scroll in js

我有一个水平滚动条 div 也可以拖动。 div 中的元素是 link。每次我离开拖动时,它都会触发点击并将我发送到 link。 有没有一种方法可以在离开拖动后阻止点击但在不拖动时保持可用?

这是一个片段:

const slider = document.querySelector('.wrapper');
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)*2;
    slider.scrollLeft = scrollLeft - walk;
});

document.getElementsByClassName('.book').ondragstart = function() { return false; };
.wrapper {
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    overflow: auto;
    min-width: 100%;
}

.book {
    width: auto;
    height: 100vh;
    min-width: 50vw;
}

.one {
  background-color: #d07fe0;
  }
  
.two {
  background-color: #808888;
  }
  
.three {
  background-color: #83e7dc;
  }
  
.four {
  background-color: #edf7a8;
  }
  
.five {
  background-color: #e9d98f;
  }
  
.six {
  background-color: #fdd;
  }
  
<body>
<div class="wrapper">
    <a href="https://whosebug.com/" class="book one"></a>
    <a href="https://whosebug.com/" class="book two"></a>
    <a href="https://whosebug.com/" class="book three"></a>
    <a href="https://whosebug.com/" class="book four"></a>
    <a href="https://whosebug.com/" class="book five"></a>
    <a href="https://whosebug.com/" class="book six"></a>
</div>
</body>

如有任何帮助,我将不胜感激。 谢谢! 尼尔

我可以推荐一种方法。

定义一个函数 preventClick

const preventClick = (e) => {
        e.preventDefault();
        e.stopImmediatePropagation();
      }

定义一个变量 isDragged 以有条件地添加和删除事件侦听器。

let isDragged = false;

mouseup 事件中,您输入了 2 种情况,isDragged = false在单击 的情况下)或 isDragged = true在拖动的情况下)

第一种情况: 移除阻止点击传播的 eventListener

第二种情况: 添加阻止点击传播的事件侦听器。

试试下面的代码,如果有帮助请告诉我。请注意,这不是优化代码。我正在为所有锚标记添加和删除事件处理程序只是为了演示您可以遵循的方法。

const slider = document.querySelector(".wrapper");
      const preventClick = (e) => {
        e.preventDefault();
        e.stopImmediatePropagation();
      }
      let isDown = false;
      var isDragged = 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", e => {
        isDown = false;
        const elements = document.getElementsByClassName("book");
        if(isDragged){
            for(let i = 0; i<elements.length; i++){
                  elements[i].addEventListener("click", preventClick);
            }
        }else{
            for(let i = 0; i<elements.length; i++){
                  elements[i].removeEventListener("click", preventClick);
            }
        }
        slider.classList.remove("active");
        isDragged = false;
      });

      slider.addEventListener("mousemove", e => {
        if (!isDown) return;
        isDragged =  true;
        e.preventDefault();
        const x = e.pageX - slider.offsetLeft;
        const walk = (x - startX) * 2;
        slider.scrollLeft = scrollLeft - walk;
      });

      document.getElementsByClassName("book").ondragstart = function() {
        console.log("Drag start");
      };
      .wrapper {
        position: relative;
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        overflow: auto;
        min-width: 100%;
      }

      .book {
        width: auto;
        height: 100vh;
        min-width: 50vw;
      }

      .one {
        background-color: #d07fe0;
      }

      .two {
        background-color: #808888;
      }

      .three {
        background-color: #83e7dc;
      }

      .four {
        background-color: #edf7a8;
      }

      .five {
        background-color: #e9d98f;
      }

      .six {
        background-color: #fdd;
      }
<body>
      <div class="wrapper">
        <a href="https://whosebug.com/" class="book one"></a>
        <a href="https://whosebug.com/" class="book two"></a>
        <a href="https://whosebug.com/" class="book three"></a>
        <a href="https://whosebug.com/" class="book four"></a>
        <a href="https://whosebug.com/" class="book five"></a>
        <a href="https://whosebug.com/" class="book six"></a>
      </div>
    </body>