Forum Moderators: open

Message Too Old, No Replies

jQuery swipe event firing twice

         

csdude55

5:45 am on Mar 19, 2018 (gmt 0)

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



This is driving me crazy, guys, so hopefully you have some advise.

I have what should be a simple function:
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="//code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>

<script>
$(function() {
$('#my_div').on('swipeleft', function() {
alert('left');
});
});
</script>


But when I swipe left, I'm getting two alerts, when only one is expected.

I've tried everything I can think of, including these two that came directly from jquerymobile.com:
$(function() {
$('#my_div').off('swipeleft').on('swipeleft', function(event) {
event.stopImmediatePropagation();
alert('left');
});
});

$(function() {
$('#my_div').on("swipeleft", swipeleftHandler);

function swipeleftHandler(event) {
alert('left');
}
});


[api.jquerymobile.com...]

But I'm still getting the same double-fire on all of them! And console.log just said "(2) swipeleft", so that was no help, either.

I've googled and found a lot of references from several years ago, but none of them have worked out. Any other suggestions?

NickMNS

12:26 pm on Mar 19, 2018 (gmt 0)

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



Is possible that your div is configured in such a way that it could be triggering the event twice on one swipe.

from the docs:
Triggered when a horizontal drag of 30px or more (and less than 30px vertically) occurs within 1 second duration in the left direction. See the swipe event entry for more detailed information on the swipe event.


Link in above quote:
[api.jquerymobile.com...]

csdude55

4:07 pm on Mar 19, 2018 (gmt 0)

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



Anything's possible! lol But I can't imagine how it could be.

Here is the entire code that I'm working with; the only thing changed for the post is the class names obviously aren't "blah", and the link to the image... I DO use the ?h=$this_date on the images to keep them from being cached:

<script>
$(function() {
$('#carousel').on('swipeleft', function(event) {
console.log(event.type);
alert('left');
});

$('#carousel').on('swiperight', function(event) {
event.stopPropagation();
alert('right');
});
});
</script>

<div id="carousel">
<div class="blah blah">
<div class="blah blah blah">
<ul>
<li onMouseOver="this.style.cursor= 'pointer';" onClick="#">
<img src="https://www.example.com/photos/sample_0.jpg?h=$this_date" style="width: 294px; height: auto">
</li>

<li onMouseOver="this.style.cursor= 'pointer';" onClick="#">
<img src="https://www.example.com/photos/sample_1.jpg?h=$this_date" style="width: 294px; height: auto">
</li>
</ul>
</div>
</div>
</div>


My only thought was maybe a glitch in the version of jQuery that I'm using? But I had other glitches when I used the newest versions, and it took me forever to find this pairing that didn't seem to have any glitches!

csdude55

6:39 pm on Mar 19, 2018 (gmt 0)

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



Well, I've isolated the problem, but I'm still not sure how to fix it.

Earlier in the script, I have another swipeleft function for mobile navigation. When I remove this for testing, then the above function works just fine. But I need both, so... ?

Here's how that part works:

// in the header
$('#body_con').on('swipeleft', function() {
$(this).css('left', '0');
$('#mobile_nav').hide();
});

// this comes right after <body>, so "body_con" encloses the entire page
<div id="mobile_nav"></div>
<div id="body_con">

// later in the page
<script>
$(function() {
$('#carousel').on('swipeleft', function(event) {
alert('left');
});

$('#carousel').on('swiperight', function(event) {
event.stopPropagation();
alert('right');
});
});
</script>

<div id="carousel">
<div class="blah blah">
<div class="blah blah blah">
<ul>
<li onMouseOver="this.style.cursor= 'pointer';" onClick="#">
<img src="https://www.example.com/photos/sample_0.jpg?h=$this_date" style="width: 294px; height: auto">
</li>

<li onMouseOver="this.style.cursor= 'pointer';" onClick="#">
<img src="https://www.example.com/photos/sample_1.jpg?h=$this_date" style="width: 294px; height: auto">
</li>
</ul>
</div>
</div>
</div>

// closing the "body_con" div
</div>


I CAN "fix" it by changing the "body_con" listener to "mobile_nav", but I don't love it because that makes it a tad harder for the user to swipe the navigation away on every page. So if anybody can suggest a way to make these work together (eg, the child operating separate from the parent), it would still be helpful :-)