Changing jQuery Mobile transition when using trigger ('click')
I need to have a navigation menu that reflects page changes based on clicks and validations. To accomplish this, I had to trigger a click when I sit down. The problem is that when I do this, I can no longer use:
$.mobile.changePage(prevpage, { transition : 'realslide', reverse : true });
Property belongs as an important component reverse:true
. "swipeleft" can have a regular transition, but "swiperight" needs a reverse transition, and I cannot do it in HTML with data-transition="reverse"
because it depends on the direction of travel.
It currently looks like my code (provided by Jasper user ). I'm not sure where I can bind to the reverse jump.
$(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>
source to share
Okay, this solution might be a little hacky, but it works.
Before triggering the event, you can set the default toggle to the page, after which the trigger will set the default. I haven't tried it, but I think it can be achieved like this:
$(function(){
var $nav = $('#nav').children().children(),
totNav = $nav.length;
$(document).on('swipeleft', '.ui-page', function() {
var next = ($nav.filter('.active').index() + 1);
$.mobile.changePage.defaults.reverse = true; // Reverse transition
$nav.eq(next).children().trigger('click');
$.mobile.changePage.defaults.reverse = false; // Dont reverse
}).on('swiperight', '.ui-page', function() {
var prev = ($nav.filter('.active').index() - 1);
$nav.eq(prev).children().trigger('click');
}).on('click', '#nav a', function(e) {
$(this).parent().addClass("active").siblings().removeClass("active");
});
});
source to share