Forum Moderators: open

Message Too Old, No Replies

jQuery - Simulate Click With Mobile Swipe

         

almo136

5:49 pm on Sep 8, 2023 (gmt 0)

10+ Year Member



I am trying to simulate a click when a user swipes on their mobile. I have different some different options but can't get any of them to work.

At the minute I am trying with tocca.js

The below code allows the issue to be recreated. As you can see left swiping the box should trigger a click on #test-click but it doesn't work.

Any ideas how I can get this to work?

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>

<body>

<div id="test-swipe" style="background-color: red; min-height: 200px;"></div>

<a id="test-click" href="https://google.com">link</a>

<br ><br >

<a id="manual-trigger" href="https://google.com">manually trigger the click</a>

<script>
// On Swipe Left
$("#test-swipe").on('swipeleft',function (e,data){
console.log(data.x);
console.log(data.y);
console.log(data.distance.x);
console.log(data.distance.y);
$("#test-click").click();
});
</script>

<script>
// On Manual Click
$( "#manual-trigger" ).on( "click", function() {
$("#test-click").click();
});
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Tocca.js/2.0.9/Tocca.min.js"></script>

</body>
</html>

csdude55

6:35 pm on Sep 11, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is the one I used:

[github.com...]

Then it would be:

$('#test-swipe').on('swipeleft', (e, data) => {
console.log(data.x);
console.log(data.y);
console.log(data.distance.x);
console.log(data.distance.y);
$("#test-click").click();
});


I haven't used the params in my script, though, so I can't swear to that info coming through. I'm guessing that the data object is where you're falling apart.

What happens if you just use this for a test?

$('#test-swipe').on('swipeleft', () => {
console.log('swiped');
});


Do you get "swiped" in the console?

If so, maybe try this next:

$('#test-swipe').on('swipeleft', (e, data) => {
console.log('swiped');
});


If you still get "swiped", try changing it to this to see what all is in data:

$('#test-swipe').on('swipeleft', (e, data) => {
console.log(JSON.stringify(data);
});


What does that show?