0

Simple touch event is not working on iPhone or on iPad, But in DevTools (device mode with touch) all is okey!

My example is about slider for calculator.

element.addEventListener('touchstart', function(e) {
    var startPoint = window.getComputedStyle(element).getPropertyValue('left'),
        shiftX = e.touches[0].pageX - Number.parseInt(startPoint),
        moveAt = function (e) {
            e.preventDefault();
            if (e.touches[0].pageX < 50) {
                element.style.left = '50' + 'px';
            } else if (e.touches[0].pageX > 330) {
                element.style.left = '330' + 'px';
            } else {
                let accessubleDifX = e.touches[0].pageX - shiftX;

                if (accessubleDifX > 50 || accessubleDifX < 330) {
                    element.style.left = `${accessubleDifX}px`;
                    console.log("imhere");
                }
            }
        };
    moveAt(e);

    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
        moveAt(e);
        counting();
    });

    document.addEventListener('touchend', function () {
        document.removeEventListener('touchmove', null);
        element.removeEventListener('touchstart', null);
    });
})
  • What is the difference between this question and [this deleted question](https://stackoverflow.com/questions/51456393/why-touchstart-doesn-t-work-on-phones)? – Teemu Jul 22 '18 at 10:29
  • 1
    `document.removeEventListener('touchmove', null);` is a no-op (you need to pass the exact function that has been previously added for it to have an effect). And your code being poorly formatted I struggle to read it correctly, but if you are really adding a new listener on the document at every element's touchstart, then your code will fire many times with many different `e`, in a few touches. – Kaiido Jul 22 '18 at 10:32
  • @Teemu is there not difference. Thank you for you closing, man! – Andrey Alekseev Jul 22 '18 at 11:06
  • @Kaiido sorry for bad formatting. Apparently, my question is closed because it is duplicate. And thank you for your comment! – Andrey Alekseev Jul 22 '18 at 11:10

0 Answers0