防止在离开拖动时单击以在 js 中滚动

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


这是一个片段:


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://stackoverflow.com/" class="book one"></a>

    <a href="https://stackoverflow.com/" class="book two"></a>

    <a href="https://stackoverflow.com/" class="book three"></a>

    <a href="https://stackoverflow.com/" class="book four"></a>

    <a href="https://stackoverflow.com/" class="book five"></a>

    <a href="https://stackoverflow.com/" class="book six"></a>

</div>

</body>


慕运维8079593
浏览 133回答 1
1回答

吃鸡游戏

我可以建议的一种方法。定义一个函数 preventClickconst preventClick = (e) => {&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; e.stopImmediatePropagation();&nbsp; &nbsp; &nbsp; }定义一个变量isDragged以有条件地添加和删除事件侦听器。let isDragged = false;在mouseup事件中,您输入 2 种情况,isDragged = false(单击时)或isDragged = true(拖动时)第一种情况: 移除阻止点击传播的事件监听器第二种情况: 添加防止点击传播的事件监听器。试试下面的代码,让我知道这是否有帮助。请注意,这不是优化代码。我正在为所有锚标记添加和删除事件处理程序,只是为了演示您可以遵循的方法。const slider = document.querySelector(".wrapper");&nbsp; &nbsp; &nbsp; const preventClick = (e) => {&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; e.stopImmediatePropagation();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; let isDown = false;&nbsp; &nbsp; &nbsp; var isDragged = false;&nbsp; &nbsp; &nbsp; let startX;&nbsp; &nbsp; &nbsp; let scrollLeft;&nbsp; &nbsp; &nbsp; slider.addEventListener("mousedown", e => {&nbsp; &nbsp; &nbsp; &nbsp; isDown = true;&nbsp; &nbsp; &nbsp; &nbsp; slider.classList.add("active");&nbsp; &nbsp; &nbsp; &nbsp; startX = e.pageX - slider.offsetLeft;&nbsp; &nbsp; &nbsp; &nbsp; scrollLeft = slider.scrollLeft;&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; slider.addEventListener("mouseleave", () => {&nbsp; &nbsp; &nbsp; &nbsp; isDown = false;&nbsp; &nbsp; &nbsp; &nbsp; slider.classList.remove("active");&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; slider.addEventListener("mouseup", e => {&nbsp; &nbsp; &nbsp; &nbsp; isDown = false;&nbsp; &nbsp; &nbsp; &nbsp; const elements = document.getElementsByClassName("book");&nbsp; &nbsp; &nbsp; &nbsp; if(isDragged){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let i = 0; i<elements.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements[i].addEventListener("click", preventClick);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let i = 0; i<elements.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements[i].removeEventListener("click", preventClick);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; slider.classList.remove("active");&nbsp; &nbsp; &nbsp; &nbsp; isDragged = false;&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; slider.addEventListener("mousemove", e => {&nbsp; &nbsp; &nbsp; &nbsp; if (!isDown) return;&nbsp; &nbsp; &nbsp; &nbsp; isDragged =&nbsp; true;&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; const x = e.pageX - slider.offsetLeft;&nbsp; &nbsp; &nbsp; &nbsp; const walk = (x - startX) * 2;&nbsp; &nbsp; &nbsp; &nbsp; slider.scrollLeft = scrollLeft - walk;&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; document.getElementsByClassName("book").ondragstart = function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log("Drag start");&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; .wrapper {&nbsp; &nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; &nbsp; display: -webkit-box;&nbsp; &nbsp; &nbsp; &nbsp; display: -webkit-flex;&nbsp; &nbsp; &nbsp; &nbsp; display: -ms-flexbox;&nbsp; &nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; &nbsp; overflow: auto;&nbsp; &nbsp; &nbsp; &nbsp; min-width: 100%;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .book {&nbsp; &nbsp; &nbsp; &nbsp; width: auto;&nbsp; &nbsp; &nbsp; &nbsp; height: 100vh;&nbsp; &nbsp; &nbsp; &nbsp; min-width: 50vw;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .one {&nbsp; &nbsp; &nbsp; &nbsp; background-color: #d07fe0;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .two {&nbsp; &nbsp; &nbsp; &nbsp; background-color: #808888;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .three {&nbsp; &nbsp; &nbsp; &nbsp; background-color: #83e7dc;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .four {&nbsp; &nbsp; &nbsp; &nbsp; background-color: #edf7a8;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .five {&nbsp; &nbsp; &nbsp; &nbsp; background-color: #e9d98f;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; .six {&nbsp; &nbsp; &nbsp; &nbsp; background-color: #fdd;&nbsp; &nbsp; &nbsp; }<body>&nbsp; &nbsp; &nbsp; <div class="wrapper">&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://stackoverflow.com/" class="book one"></a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://stackoverflow.com/" class="book two"></a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://stackoverflow.com/" class="book three"></a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://stackoverflow.com/" class="book four"></a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://stackoverflow.com/" class="book five"></a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="https://stackoverflow.com/" class="book six"></a>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript