사용자가 화면의 오른쪽을 가리키고 사용자가 화면의 왼쪽을 가리키면 왼쪽으로 스크롤 할 때 오른쪽으로 스크롤하는 간단한 목적으로 다음 스크립트를 작성했습니다 . 마우스를 같은 지점에 너무 오랫동안두면 끝까지 도달하기 전에 스크롤이 멈추는 것을 제외하고는 잘 작동합니다. 이후에 마우스를 움직이면 다시 스크롤을 시작합니다. 코드가 마우스 위치를 확인하고 그에 따라 스크롤하는 무한 타임 루프를 시작하기 때문에 왜 이런 일이 발생하는지 이해할 수 없습니다. 마치 마우스가 너무 오랫동안 비활성 상태 인 경우 마우스 위치가보고되는 것을 중지하는 것과 같습니다. 어떤 아이디어?자동 스크롤이 갑자기 멈춤
var mouseX = 0;
var scrollX = 0;
var timer;
$(document).ready(function() {
// Record the mouse position if the mouse is moved
$(document).mousemove(function(e) {
mouseX = e.pageX;
});
// Record the scroll position if the page is scrolled
$(document).scroll(function() {
scrollX = $(window).scrollLeft();
});
// Initiate the scrolling loop
scroll();
});
function scroll() {
// If the user is hovering over the right side of the window
if ((mouseX - scrollX) > 0.75*$(window).width()) {
scrollX += 1;
$(window).scrollLeft(scrollX);
}
// If the user is hovering over the left side of the window
if ((mouseX - scrollX) < (0.25*$(window).width())) {
scrollX -= 1;
$(window).scrollLeft(scrollX);
}
// Repeat in 5 ms
timer = window.setTimeout('scroll()', 5);
}