Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop touchend firing links unintentionally

I'm using the touchendevent to prevent ios from requiring two touches to fire href links. This works fine however it is firing the links unintentionally when scrolling.

I know that the the solution is to implement the touchstart to see if there is movement, but I'm a jquery novice and I'm not sure how apply this.

Here is the touchendcode

$('a').on('touchend', function(e) {
var el = $(this);
var link = el.attr('href');
window.location = link;
});

Hope someone can help.

Thanks

like image 602
Corey Harrison Avatar asked Jan 01 '26 12:01

Corey Harrison


1 Answers

Ok this is what is working for me to solve this using code from this post

var dragging = false;
$("a").on("touchmove", function(){
  dragging = true;
});

$("a").on("touchend", function(e){
  if (dragging){
e.preventDefault();
}
else {var el = $(this);
var link = el.attr('href');
window.location = link;
}    

});

$("a").on("touchstart", function(){
dragging = false;
});

This works for me.

like image 79
Corey Harrison Avatar answered Jan 03 '26 03:01

Corey Harrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!