gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:30:52 27 4
gpt4 key购买 nike

我有一个水平滚动的 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>

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

最佳答案

我可以推荐一种方法。

定义一个函数 preventClick

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

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

let isDragged = false;

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

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

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

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

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://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>

关于javascript - 在js中拖动滚动时防止点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58353280/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com