diff --git a/README.md b/README.md index 502f71b..548fd04 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,10 @@ This action expects that a container has a fixed height and a direct child that This is so simple that it doesn't need a NPM package. Just copy the file ``/src/kineticscroll.js`` to your project's appropriate folder and import it where needed. All other files are just there for the sake of the example. +## Notes + +To avoid problems with underlying click events, this class temporarily suspends pointer events on the container element's children. This is done via the pointer-events CSS selector. You may experience some problems should you be manipulating this selector on your container's children and using this class at the same time. The class will do a pointer-events: 'none' when a drag has moved and a pointer-events: 'auto' at the mouseup event. + ## Example Download this code, extract it and run: diff --git a/public/countries.js b/public/countries.js index 8691b80..a299bec 100644 --- a/public/countries.js +++ b/public/countries.js @@ -183,7 +183,7 @@ const countries = [ {name: 'Reunion', code: 'RE'}, {name: 'Romania', code: 'RO'}, {name: 'Russian Federation', code: 'RU'}, - {name: 'RWANDA', code: 'RW'}, + {name: 'Rwanda', code: 'RW'}, {name: 'Saint Helena', code: 'SH'}, {name: 'Saint Kitts and Nevis', code: 'KN'}, {name: 'Saint Lucia', code: 'LC'}, diff --git a/public/kineticscroll.js b/public/kineticscroll.js index 9f89ed3..e069ef4 100644 --- a/public/kineticscroll.js +++ b/public/kineticscroll.js @@ -1,5 +1,5 @@ -// Svelte action to transform a container into a momentum scroller +// Vanilla JS module and Svelte action to transform a container into a momentum scroller // By Walter Staeblein - 2022 export function kineticscroll(node, cfgs) { 'use strict'; @@ -122,6 +122,7 @@ export function kineticscroll(node, cfgs) { if (isWheel) { wheeling = true; } else { pressed = true; } reference = ypos(e); + node.style.cursor = 'ns-resize'; velocity = amplitude = 0; frame = offset; timestamp = Date.now(); @@ -139,9 +140,11 @@ export function kineticscroll(node, cfgs) { if (pressed) { y = ypos(e); delta = reference - y; - if (delta > 2 || delta < -2) { + if (Math.abs(delta) > 5) { reference = y; scroll(offset + delta); + + Array.from(node.children).forEach((ele) => { ele.style.pointerEvents = 'none' }); } } e.preventDefault(); @@ -150,10 +153,15 @@ export function kineticscroll(node, cfgs) { } function preRelease(e) { + e.stopPropagation(); + e.preventDefault(); if (e.buttons == 1) { release(e); } } function release(e) { + e.stopPropagation(); + e.preventDefault(); + pressed = false; wheeling = false; clearInterval(ticker); @@ -166,7 +174,10 @@ export function kineticscroll(node, cfgs) { requestAnimationFrame(autoScroll); } e.preventDefault(); - e.stopPropagation(); + e.stopPropagation(); + + Array.from(node.children).forEach((ele) => { ele.style.pointerEvents = 'auto' }); + node.style.cursor = 'auto'; return false; } diff --git a/public/vanilla.html b/public/vanilla.html index 26764a3..5a4b367 100644 --- a/public/vanilla.html +++ b/public/vanilla.html @@ -3,6 +3,7 @@