I'm trying to replicate as closely as possible the sliding toggles of iOS, using just JS and CSS. I've found the excellent pen by @3rror404 that does exactly that here.
While it works perfectly in iOS Safari , it does react just to clicks and not to drags in Chrome (both Desktop and Android) and I don't understand why. I even added mouseup/mousedown/mousemove events, but still a no go..
for (let i = 0; i < switches.length; i++) {
const switchEl = switches[i];
switchEl.draggable = true;
['dragstart', 'touchstart','mousedown'].forEach(function(e) {
switchEl.addEventListener(e, onDragStart);
});
['dragover', 'touchmove','mousemove'].forEach(function(e) {
switchEl.addEventListener(e, onDragOver);
});
['dragend', 'touchend','mouseup'].forEach(function(e) {
switchEl.addEventListener(e, onDragEnd);
});
}
see my edited pen here: https://codepen.io/azerty1234/pen/BajLqgN
any idea why this happens or possible fixes? Thanks!
I found a bug in the pen.
It adds the same event handler for
dragstartandtouchstartChrome (in mobile mode) fires thetouchstartevent.The
touchstartevent does not have apageXandpageYvariable (line 64 and 65)It does have
evt.touches[0].pageXandevt.touches[0].pageYYou should check if
evt.typeequals 'touchstart' or not:Apply this change in
onDragOver(64,65) andonDragStart(90,91) and it will work in mobile mode