Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Differentiate between touch start and click

188 views
Skip to first unread message

Andrew Poulos

unread,
Sep 16, 2016, 4:26:25 AM9/16/16
to
I have a touchstart event and a click event both triggering this function

function checkTrigger(e){
if (e.type != "click") {
e = e.touches[0];
}

var elem = document.elementFromPoint(e.pageX - window.pageXOffset,
e.pageY - window.pageYOffset);
...
}

To tell whether it was a touchstart or a click I'm using
(e.type != "click")
is that the right way to differentiate between the two or should I have
two smaller functions (one for each event) which then pass on the
relevant values to checkTrigger?

Andrew Poulos

Ben Bacarisse

unread,
Sep 16, 2016, 6:25:20 AM9/16/16
to
I don't think it matters much but I would, in general, make the test
positive (logically you are doing something when it's a touch event, not
when it isn't a click event). Indeed, you might even want to test for
the touches property itself.

I'd be more concerned about the pageX/pageY stuff which is not
standardised (though I may be behind the times on that). You usually
get the target element using "e.target" and "this" will usually refer to
the element to which the listener was added.

--
Ben.

Thomas 'PointedEars' Lahn

unread,
Sep 16, 2016, 6:06:59 PM9/16/16
to
Andrew Poulos wrote:

> I have a touchstart event and a click event both triggering this function

You should not. AISB, “touchstart” is equivalent to “mousedown” and
“pointerdown”; ”click” is equivalent to “touchstart” followed by
“touchend”, or “pointerdown” followed by “pointerup”, instead.

> To tell whether it was a touchstart or a click I'm using
> (e.type != "click")
> is that the right way to differentiate between the two or should I have
> two smaller functions (one for each event) which then pass on the
> relevant values to checkTrigger?

No.

--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Andrew Poulos

unread,
Sep 18, 2016, 5:25:21 AM9/18/16
to
On 17/09/2016 8:06 AM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> I have a touchstart event and a click event both triggering this function
>
> You should not. AISB, “touchstart” is equivalent to “mousedown” and
> “pointerdown”; ”click” is equivalent to “touchstart” followed by
> “touchend”, or “pointerdown” followed by “pointerup”, instead.

While they have been times I have pressed a button on my mouse only to
change my mind before I release the button I have never touched a
touch-enabled device and have changed my mind before I lifted my finger.
So for me "touchstart" seems appropriate.

>> To tell whether it was a touchstart or a click I'm using
>> (e.type != "click")
>> is that the right way to differentiate between the two or should I have
>> two smaller functions (one for each event) which then pass on the
>> relevant values to checkTrigger?
>
> No.

No to which one: the former, the latter, or both?

Andrew Poulos

0 new messages