Javascript: 水平自动滚动
Javascript: horizontal autoscroll
如果用户将容器中的元素拖动到水平边缘附近,我需要将容器自动滚动到相应方向的末尾。
如何改进下面的代码?
const scrollSpeed = 10;
const scrollSensitivity = 10;
let scrollInterval = null;
const scrollContainer = document.getElementById("scrollContainer");
scrollContainer.addEventListener("touchmove", autoScroll);
function disableAutoScrolling() {
clearInterval(scrollInterval);
scrollInterval = null;
}
function autoScroll(event) {
// pointer offset
const offsetX = event.targetTouches[0].pageX;
const offsetY = event.targetTouches[0].pageY;
// container offset relative to viewport
const rect = scrollContainer.getBoundingClientRect();
// pointer is over right / left edge of a scrollContainer
const SCROLL_RIGHT = ( rect.width + rect.left - offsetX <= scrollSensitivity ) && ( offsetY > rect.top );
const SCROLL_LEFT = ( offsetX - rect.left <= scrollSensitivity ) && ( offsetY > rect.top );
disableAutoScrolling();
// the container should scroll
if (SCROLL_RIGHT) {
scrollInterval = setInterval(() => {
scrollContainer.scrollLeft += scrollSpeed;
}, 10);
}
if (SCROLL_LEFT) {
scrollInterval = setInterval(() => {
scrollContainer.scrollLeft -= scrollSpeed;
}, 10);
}
}
感谢您的帮助。
我不知道当前代码是否有效,但这里有一个更有条理的版本。
const scroller={
speed: 10,
sensitivity: 10,
container: document.getElementById("scrollContainer"),
rect: scroller.container.getBoundingClientRect(),
interval: null,
offsetX: 0,
offsetY: 0
};
scroller.container.addEventListener("touchmove",function(e){
clearInterval(scroller.interval);
scroller.interval = null; // May not be needed
// pointer offset
scroller.offsetX = e.targetTouches[0].pageX;
scroller.offsetY = e.targetTouches[0].pageY;
// the container should scroll
// pointer is over right / left edge of a scroller.container
if((scroller.rect.width+scroller.rect.left-scroller.offsetX<=scroller.sensitivity)&&(scroller.offsetY>scroller.rect.top))scroller.interval=setInterval(()=>{scroller.container.scrollLeft+=scroller.speed;},10);
if((scroller.offsetX-scroller.rect.left<=scroller.sensitivity)&&(scroller.offsetY>scroller.rect.top))scroller.interval=setInterval(()=>{scroller.container.scrollLeft-=scroller.speed;},10);
});
如果用户将容器中的元素拖动到水平边缘附近,我需要将容器自动滚动到相应方向的末尾。
如何改进下面的代码?
const scrollSpeed = 10;
const scrollSensitivity = 10;
let scrollInterval = null;
const scrollContainer = document.getElementById("scrollContainer");
scrollContainer.addEventListener("touchmove", autoScroll);
function disableAutoScrolling() {
clearInterval(scrollInterval);
scrollInterval = null;
}
function autoScroll(event) {
// pointer offset
const offsetX = event.targetTouches[0].pageX;
const offsetY = event.targetTouches[0].pageY;
// container offset relative to viewport
const rect = scrollContainer.getBoundingClientRect();
// pointer is over right / left edge of a scrollContainer
const SCROLL_RIGHT = ( rect.width + rect.left - offsetX <= scrollSensitivity ) && ( offsetY > rect.top );
const SCROLL_LEFT = ( offsetX - rect.left <= scrollSensitivity ) && ( offsetY > rect.top );
disableAutoScrolling();
// the container should scroll
if (SCROLL_RIGHT) {
scrollInterval = setInterval(() => {
scrollContainer.scrollLeft += scrollSpeed;
}, 10);
}
if (SCROLL_LEFT) {
scrollInterval = setInterval(() => {
scrollContainer.scrollLeft -= scrollSpeed;
}, 10);
}
}
感谢您的帮助。
我不知道当前代码是否有效,但这里有一个更有条理的版本。
const scroller={
speed: 10,
sensitivity: 10,
container: document.getElementById("scrollContainer"),
rect: scroller.container.getBoundingClientRect(),
interval: null,
offsetX: 0,
offsetY: 0
};
scroller.container.addEventListener("touchmove",function(e){
clearInterval(scroller.interval);
scroller.interval = null; // May not be needed
// pointer offset
scroller.offsetX = e.targetTouches[0].pageX;
scroller.offsetY = e.targetTouches[0].pageY;
// the container should scroll
// pointer is over right / left edge of a scroller.container
if((scroller.rect.width+scroller.rect.left-scroller.offsetX<=scroller.sensitivity)&&(scroller.offsetY>scroller.rect.top))scroller.interval=setInterval(()=>{scroller.container.scrollLeft+=scroller.speed;},10);
if((scroller.offsetX-scroller.rect.left<=scroller.sensitivity)&&(scroller.offsetY>scroller.rect.top))scroller.interval=setInterval(()=>{scroller.container.scrollLeft-=scroller.speed;},10);
});