-
Notifications
You must be signed in to change notification settings - Fork 1
Touch Event detection with JavaScript
Mark Howells-Mead edited this page Sep 8, 2020
·
2 revisions
const isTouch = 'ontouchstart' in window || 'onmsgesturechange' in window;
Add the following to the page head
.
<script>!function(a,b){"use strict";b.documentElement.className=b.documentElement.className.replace("no-js","js"),("ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch)&&(document.documentElement.className=document.documentElement.className.replace("no-touch","touch"))}(window,document);</script>
Then you can react to the class touch
(or no-touch
) on the html
element.
var showOverlay = function(){
// your function to show an overlay
};
var hideOverlay = function(){
// your function to hide an overlay
};
$('html.touch [data-show-overlay]').on('touchstart.showoverlay', function(e){
showOverlay();
});
$('html.no-touch [data-show-overlay]').on('mouseover.showoverlay', function(e){
showOverlay();
});
$('html.no-touch [data-show-overlay]').on('mouseout.showoverlay', function(e){
hideOverlay();
});
Mark Howells-Mead | https://permanenttourist.ch and https://sayhello.ch/ | Wiki since 2016
Use this code freely, widely and for free. Provision of this code provides and implies no guarantee.
Please respect the GPL v3 licence, which is available via http://www.gnu.org/licenses/gpl-3.0.html