當我想模仿某種類型的事件我只是用.trigger()
觸發正確的元素,而不是對事件重新寫我的代碼在兩種不同情況下運行事件處理程序。由於您的代碼,是用click
事件,可以觸發基於當前active
列表項正確的鏈路上的click
事件:
$(function(){
//cache all of the list-items in the navigation div
var $nav = $('#nav').children().children(),
//also cache the number of list-items found
totNav = $nav.length;
//notice the use of `.on()` rather than `.live()` since the latter is depreciated
$(document).on('swipeleft', '.ui-page', function() {
//get the next index based on the current list-item with the `active` class
var next = ($nav.filter('.active').index() + 1);
//if you're already on the last page then stop the function and do nothing
if (next === totNav) {
return;
//you could use this next line to wrap to the beginning rather than not doing anything
//next = 0;
}
//trigger a `click` event on the link within the list-item at the next index
$nav.eq(next).children().trigger('click');
//notice I'm chaining the `.on()` function calls on the `$(document)` selection
}).on('swiperight', '.ui-page', function() {
//get the previous index
var prev = ($nav.filter('.active').index() - 1);
if (prev === -1) {
return;
//you could use this next line to wrap to the beginning rather than not doing anything
//prev = (totNav - 1);
}
$nav.eq(prev).children().trigger('click');
}).on('click', '#nav a', function(e) {
//more chaining
$(this).parent().addClass("active").siblings().removeClass("active");
});
});
這裏是一個演示:http://jsfiddle.net/QFX5x/4/
什麼令人難以置信的幫助,文檔和說明。謝謝! – bafromca 2012-03-13 20:48:07
我確實有一個問題,如果你使用trigger()來模擬'click',我如何獲得動畫方向,如果我不使用'$ .mobile.changePage(prevpage,{transition: 'realslide',reverse:true});'?? – bafromca 2012-03-14 13:28:11