在 jQuery Mobile 中滑动返回?
我正在尝试 jQuery Mobile,因为我无法通过滑动返回页面以在 jQTouch 中正常工作。但作为 jQuery Mobile 的新手,我不知道如何实现滑动,以及如何向右滑动导致返回到上一页。我一直在谷歌搜索并搜索文档,但找不到它,所以我非常感谢一些帮助。
编辑:
我在谷歌搜索更多时发现了这个解决方案:
$('body').live('pagecreate', function (event) {
$('div.ui-page').live("swipeleft", function () {
var nextpage = $(this).next('div[data-role="page"]');
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, 'slide');
}
});
$('div.ui-page').live("swiperight", function () {
var prevpage = $(this).prev('div[data-role="page"]');
// swipe using id of previous page if exists
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, 'slide', true);
}
// history.back();
// return false;
});
});
这确实有效,但看起来不太稳定。当您滑动时,它会来回跳跃一点。我还尝试了最后注释掉的代码 - History.back(),这是另一个网站上建议的。但这似乎更加不稳定,导致各种奇怪的跳跃。
I'm trying out jQuery Mobile, because I wasn't able to get swipe to go back a page to work well in jQTouch. But being new to jQuery Mobile, I have no idea how to go about implementing swipe, and how to make a swipe right cause a return to the previous page. I've been Googling and searching the docs, but can't find it, so I would really appreciate some help.
EDIT:
I found this solution when Googling a bit more:
$('body').live('pagecreate', function (event) {
$('div.ui-page').live("swipeleft", function () {
var nextpage = $(this).next('div[data-role="page"]');
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, 'slide');
}
});
$('div.ui-page').live("swiperight", function () {
var prevpage = $(this).prev('div[data-role="page"]');
// swipe using id of previous page if exists
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, 'slide', true);
}
// history.back();
// return false;
});
});
This does work, but doesn't seem very stable. It jumps a bit back and forth when you swipe. I also tried the commented out code at the end - history.back(), which was suggested on another site. But that seemed even more unstable, causing all kinds of weird jumps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用 jQuery .live“向左滑动”和“向右滑动”事件。
示例:
此外,此 YouTube 视频也可能对您有帮助。
http://www.youtube.com/watch?v=Ij1RYI1p7rM
You can use the jQuery .live "swipe left" and "swipe right" events.
Example:
Also, this YouTube video might help you as well.
http://www.youtube.com/watch?v=Ij1RYI1p7rM
蒂莫西的答案看起来不错,但有人可能更喜欢这个类似且现成的解决方案:
http://filamentgroup.com/lab /jquery_mobile_pagination_plugin/
由 jQuery Mobile 框架团队的 2 位核心成员今天发布
这是一个让用户轻松滑动的插件页。可能不适合每个用例,但肯定做得很好(如果不是那些人,还有谁会知道如何使用 jqm 做某事?:))
The answer from Timothy looks ok, but someone might prefer this similiar and ready solution:
http://filamentgroup.com/lab/jquery_mobile_pagination_plugin/
Published today by the home of 2 of the core jQuery Mobile Framework Team members
It's a plugin that lets the user easily swipe through pages. Might not fit every use-case, but sure is well-done (who else would know how to do something with jqm if not those guys? :) )
正确的解决方案是使用历史对象并允许 JQM 根据我们向浏览器发送的方向选择正确的(左或右)转换。
因此,将默认转换设置为“滑动”并将所有具有数据转换属性的链接设置为“淡入淡出”,然后将 History.back/forward 附加到滑动事件:
如果您正在创建动态内容(我非常希望您是年轻人),请务必在触发“create”事件后在所有链接上设置 data-transition="fade" 属性。
例如:
Correct solution is to use the history object and allow JQM to select the correct (left or right) transition based on which direction we send the browser.
Hence, set the default transition to "slide" and set all links with data-transition attribute to "fade", then attach history.back/forward to the swipe events:
If you're creating dynamic content (and I damn well hope you are young man), be sure to set the data-transition="fade" attribute on all links after triggering the "create" event.
eg:
在 jquery.mobile-1.0-rc2 中,向后滑动被提及为
$.mobile.changePage('topage', { transition: "slide",
反向:真实,
});
In jquery.mobile-1.0-rc2, swipe back is mentioned as
$.mobile.changePage('topage', { transition: "slide",
reverse: true,
});
只是分享我的代码。我也一直在摆弄这个,最终得到了这个:
请注意,我使用 prevAll 和 nextAll 因为上一页的 div 可能并不总是紧邻当前页面的 div 之前。
Just sharing my code. I have been fiddling with this too, and I ended up having this:
Note that I use prevAll and nextAll because the previous page's div may not always be immediately before the current page's div.
在 jquery.mobile-1.0-rc2 中,滑动到后面即可提及
In jquery.mobile-1.0-rc2, swipe to back to be mentioned as