I created a jquery function to be able to scroll a list of items back and forth, and it works perfectly with the mouse wheel.
Now I plan to use hammer.js to apply the same logic with touch, but I am failing miserably.
My function:
$.fn.scrollToNearest = function (delta) {
var direction = delta > 0 ? "prev" : "next"; // Get the direction of the scroll.
var elementToScroll = $(this)[direction](); // Get the element that should be scrolled into view.
// Scroll the element into view.
if (elementToScroll.length) {
var id = elementToScroll.attr("id");
document.querySelector("#" + id).scrollIntoView({
behavior: "smooth",
block: "end",
});
}
return this; // Needed for other methods to be able to chain off of scrollToPreviousOrNext.
};
In an attempt to use hammerjs I have tried swipe and pan events without success, how can I create a method that has the same logic but touch?