gpt4 book ai didi

javascript - 如何通过滚动捕捉使拖动滚动工作顺利进行

转载 作者:行者123 更新时间:2023-12-04 12:55:42 27 4
gpt4 key购买 nike

滚动捕捉适用于鼠标滚动,但不适用于拖动滚动。
我希望能够真正滚动,然后让它像正常滚动一样卡在适当的位置。
所以我希望它在我拖动滚动(不捕捉)时正常移动,一旦我离开它,我希望它可以移动和捕捉。
只是在拖动滚动时不想要幻灯片。
任何替代解决方案?

const childern = document.querySelectorAll(".childern");
const parent = document.querySelector(".container");

let startX;
let scrollTop;
let isDown;

parent.addEventListener("mousedown", (e) => mouseIsDown(e));
parent.addEventListener("mouseup", (e) => mouseUp(e));
parent.addEventListener("mouseleave", (e) => mouseLeave(e));
parent.addEventListener("mousemove", (e) => mouseMove(e));

function mouseIsDown(e) {
isDown = true;
startY = e.pageY - parent.offsetTop;
scrollTop = parent.scrollTop;
}
function mouseUp(e) {
isDown = false;
}
function mouseLeave(e) {
isDown = false;
}
function mouseMove(e) {
if (isDown) {
e.preventDefault();
//Move vertcally
const y = e.pageY - parent.offsetTop;
const walkY = (y - startY) * 5;
parent.scrollTop = scrollTop - walkY;
}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100vh;
overflow: hidden;
}

.container {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
.childern {
height: 100vh;
scroll-snap-align: start;
}
.one {
background-color: black;
}
.two {
background-color: rgb(36, 36, 36);
}
.three {
background-color: rgb(71, 71, 71);
}
<html>
<head>
<title>Document</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="childern one"></div>
<div class="childern two"></div>
<div class="childern three"></div>
</div>
</body>
<script src="app.js"></script>
</html>

最佳答案

我实际上找到了解决方案。
所以在javascript中,当鼠标被按住时(function mouseIsDOwn),设置scroll-snap-type = "none"当鼠标被释放时( function mouseUp )设置 scroll-snap-type = "y mandatory"如果其他人正在寻找这个,我希望它会有所帮助。

关于javascript - 如何通过滚动捕捉使拖动滚动工作顺利进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67962715/

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