I need to have a navigation menu that reflects page changes based on clicks and swipes. To perform this I’ve had to trigger a click when I swipe. Problem is, when I do this, I can no longer use:
$.mobile.changePage(prevpage, { transition : 'realslide', reverse : true });
With the property reverse:true being the important part. ‘swipeleft’ can have a regular transition but ‘swiperight’ needs a reverse transition and I can’t do this in the HTML with data-transition="reverse" because it’s dependant on the swipe direction.
Currently this is what my code looks like (provided by user Jasper). I’m not sure where I can tie in the reverse transition in.
$(function(){
var $nav = $('#nav').children().children(),
totNav = $nav.length;
$(document).on('swipeleft', '.ui-page', function() {
var next = ($nav.filter('.active').index() + 1);
if (next === totNav)
return;
$nav.eq(next).children().trigger('click');
}).on('swiperight', '.ui-page', function() {
var prev = ($nav.filter('.active').index() - 1);
if (prev === -1)
return;
$nav.eq(prev).children().trigger('click');
}).on('click', '#nav a', function(e) {
$(this).parent().addClass("active").siblings().removeClass("active");
});
});
This question is an extension of this: Update menu in jQuery Mobile based on swipe event
EDIT:
Thanks everyone for your help! Here is my final solution:
<script>
$(function(){
var $nav = $('#nav').children().children(),
totNav = $nav.length;
$(document).on('swipeleft', '.ui-page', function() {
var next = ($nav.filter('.active').index() + 1);
var $elem = $nav.eq(next).children();
if (next === totNav)
return;
$.mobile.changePage.defaults.reverse = false;
$elem.trigger('click');
}).on('swiperight', '.ui-page', function() {
var prev = ($nav.filter('.active').index() - 1);
var $elem = $nav.eq(prev).children();
if (prev === -1)
return;
$.mobile.changePage.defaults.reverse = true;
$elem.trigger('click');
}).on('click', '#nav a', function(e) {
var prev = $nav.filter('.active').index();
$(this).parent().addClass("active").siblings().removeClass("active");
var now = $nav.filter('.active').index();
if (prev > now)
$.mobile.changePage.defaults.reverse = true;
else
$.mobile.changePage.defaults.reverse = false;
});
});
</script>
Okey, this solution might be a bit hackish, but it works.
Before you trigger the event you can set the default page transition to be reversed, then after the trigger set it to the default value. I have not tried it, but I think it might be achieved like this: