Переписал стандартный скроллинг и для мыши и для тачскрина. Осталось переписать масштабирование на тачскрине. Есть ли фундаментальные замечания к коду? А то боюсь я уже завёл себя в густое болото....
var MouseScrollTop = 0;
var MouseScrollSpeed = 0;
var MouseScrollDelta;
var LastMouseScrollTime = 0;
var LastMouseScrollAnimationStepTime;
var MouseScrollAnimationStepFlag = false;
$(window).on("wheel mousewheel", function (event) {
if (event.originalEvent.deltaY > 0) {
MouseScrollDelta = 100;
} else {
MouseScrollDelta = -100;
}
if (Math.abs(MouseScrollSpeed + MouseScrollDelta) < Math.abs(MouseScrollSpeed)) {
MouseScrollSpeed = 0;
}
if (LastMouseScrollTime != 0) {
MouseScrollSpeed = MouseScrollSpeed * Math.pow(0.995, new Date() - LastMouseScrollTime);
}
LastMouseScrollTime = new Date();
MouseScrollSpeed = MouseScrollSpeed + MouseScrollDelta;
if (MouseScrollAnimationStepFlag == false) {
MouseScrollAnimationStepFlag = true;
MouseScrollTop = $(this).scrollTop();
LastMouseScrollAnimationStepTime = new Date();
function MouseScrollAnimationStep() {
MouseScrollSpeed = MouseScrollSpeed * Math.pow(0.995, new Date() - LastMouseScrollTime);
LastMouseScrollTime = new Date();
if (Math.abs(MouseScrollSpeed) < 5) {
MouseScrollSpeed = 0;
MouseScrollAnimationStepFlag = false;
}
MouseScrollTop = MouseScrollTop + MouseScrollSpeed * (new Date() - LastMouseScrollAnimationStepTime) / 150;
LastMouseScrollAnimationStepTime = new Date();
$(this).scrollTop(Math.round(MouseScrollTop));
if ($(this).scrollTop() != Math.round(MouseScrollTop)) {
MouseScrollSpeed = 0;
MouseScrollAnimationStepFlag = false;
}
if (MouseScrollAnimationStepFlag == true) {
requestAnimationFrame(MouseScrollAnimationStep);
}
}
MouseScrollAnimationStep();
}
});
var TouchScrollStartDelta;
var TouchScrollTop;
var LastTouchScrollTop;
var TouchScrollSpeed = 0;
var TouchScrollAnimationStepFlag = false;
var TouchScrollInertiaAnimationStepFlag = false;
var LastTouchScrollAnimationStepTime = 0;
var LastTouchScrollInertiaAnimationStepTime;
var TouchScrollSpeedInitializationRate = 0;
$(window).on("touchstart", function (event) {
TouchScrollInertiaAnimationStepFlag = false;
TouchScrollTop = $(this).scrollTop();
TouchScrollStartDelta = event.originalEvent.touches[0].screenY + $(this).scrollTop();
TouchScrollAnimationStepFlag = true;
TouchScrollSpeedInitializationRate = 0;
LastTouchScrollTop = TouchScrollTop;
LastTouchScrollAnimationStepTime = new Date();
function TouchScrollAnimationStep() {
$(this).scrollTop(Math.round(TouchScrollTop));
if (TouchScrollAnimationStepFlag == true) {
requestAnimationFrame(TouchScrollAnimationStep);
}
}
TouchScrollAnimationStep();
});
$(window).on("touchmove", function (event) {
event.preventDefault();
TouchScrollTop = TouchScrollStartDelta - event.originalEvent.touches[0].screenY;
if (LastTouchScrollAnimationStepTime != 0) {
if (TouchScrollSpeedInitializationRate == 1) {
TouchScrollSpeed = (TouchScrollTop - LastTouchScrollTop) / (new Date() - LastTouchScrollAnimationStepTime) * 150;
LastTouchScrollTop = TouchScrollTop;
LastTouchScrollAnimationStepTime = new Date();
TouchScrollSpeedInitializationRate = 0;
} else {
TouchScrollSpeedInitializationRate = TouchScrollSpeedInitializationRate + 1;
}
} else {
LastTouchScrollTop = TouchScrollTop;
LastTouchScrollAnimationStepTime = new Date();
}
});
$(window).on("touchend", function () {
TouchScrollAnimationStepFlag = false;
TouchScrollInertiaAnimationStepFlag = true;
LastTouchScrollInertiaAnimationStepTime = new Date();
function TouchScrollInertiaAnimationStep() {
TouchScrollSpeed = TouchScrollSpeed * Math.pow(0.998, new Date() - LastTouchScrollInertiaAnimationStepTime);
if (Math.abs(TouchScrollSpeed) < 5) {
TouchScrollSpeed = 0;
TouchScrollInertiaAnimationStepFlag = false;
}
TouchScrollTop = TouchScrollTop + TouchScrollSpeed * (new Date() - LastTouchScrollInertiaAnimationStepTime) / 150;
LastTouchScrollInertiaAnimationStepTime = new Date();
$(this).scrollTop(Math.round(TouchScrollTop));
if ($(this).scrollTop() != Math.round(TouchScrollTop)) {
TouchScrollSpeed = 0;
TouchScrollInertiaAnimationStepFlag = false;
}
if (TouchScrollInertiaAnimationStepFlag == true) {
requestAnimationFrame(TouchScrollInertiaAnimationStep);
}
}
TouchScrollInertiaAnimationStep();
});