Capture “tap” event with pure JavaScript

后端 未结 4 402
囚心锁ツ
囚心锁ツ 2020-12-23 16:20

How can I capture a user\'s \"tap\" event with pure JS? I cannot use any libraries, unfortunately.

4条回答
  •  有刺的猬
    2020-12-23 17:20

    I wrote a little script myself. It's not in pure-JS, but works fine for me. It prevents executing the script on scrolling, meaning the script only fires on a 'tap'-event.

    $(element)
            .on('touchstart', function () {
                $(this).data('moved', '0');
            })
            .on('touchmove', function () {
                $(this).data('moved', '1');
            })
            .on('touchend', function () {
                if($(this).data('moved') == 0){
                    // HERE YOUR CODE TO EXECUTE ON TAP-EVENT
                }
            });
    

提交回复
热议问题