使用 iscroll 与 jquery 移动绑定问题

发布于 2024-12-09 18:26:06 字数 311 浏览 5 评论 0原文

我目前正在绞尽脑汁地尝试让 iscroll 4 与 jQuery Mobile 一起使用。

如果我禁用 JQM ajax 默认导航,一切都会正常工作,但我想保留它。

我的问题是我无法弄清楚如何成功调用/绑定 iscroll,以便它适用于需要它们的页面。我尝试过 pageinit() 和 pagecreate() 无济于事。

可以在这里找到这方面的基本示例: http://bit.ly/ngXkNR

非常感谢任何指点。

一个。

Im currently pulling my hair out trying to get iscroll 4 working with jQuery Mobile.

It all works fine if i disable JQM ajax default navigation but I would like to retain this.

My issue is I cant work out how to successfully call/bind iscroll so it works with the pages that need them. I have tried pageinit() and pagecreate() to no avail.

A basic example of this can be found here:
http://bit.ly/ngXkNR

Any pointers much appreciated.

A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-12-16 18:26:06

谢谢贾斯珀,
我稍微改变了你的方法,这样你就可以在用类标识的任何包装器上调用 iScroll 。
另外,我卸载并销毁 pagehide 事件上的所有 iScroll 实例 - 我不需要刷新方法来满足我的需要:

// iScroll variable
var myScroll = [];

$(document).delegate('[data-role="page"]', 'pageshow', function () {

    var $page = $(this);

    // setup iScroll
    $($page.find('.iscroll_wrapper')).each(function(index) {

        var scroller_id = $(this).get(0);

        myScroll.push(
            new iScroll(scroller_id, {
                snap       : true,
                momentum   : false,
                hScrollbar : false
            }));
    });

});

$(document).delegate('[data-role="page"]', 'pagehide', function () {

    // unset and delete iScroll
    for (x in myScroll)
    {
        myScroll[x].destroy();
        myScroll[x] = null;
        myScroll.splice(x, 1);
    }

});

Thanks Jasper,
I changed your method a bit, so that you can call iScroll on any wrapper identified with a class.
Also I unload and destroy all iScroll instances on the pagehide event - i dont need the refresh method for my needs:

// iScroll variable
var myScroll = [];

$(document).delegate('[data-role="page"]', 'pageshow', function () {

    var $page = $(this);

    // setup iScroll
    $($page.find('.iscroll_wrapper')).each(function(index) {

        var scroller_id = $(this).get(0);

        myScroll.push(
            new iScroll(scroller_id, {
                snap       : true,
                momentum   : false,
                hScrollbar : false
            }));
    });

});

$(document).delegate('[data-role="page"]', 'pagehide', function () {

    // unset and delete iScroll
    for (x in myScroll)
    {
        myScroll[x].destroy();
        myScroll[x] = null;
        myScroll.splice(x, 1);
    }

});
溺深海 2024-12-16 18:26:06

按照我四天前为您创建的示例(将 iscroll 与 jquery mobile 结合使用 )... 您要绑定到仅在初始页面加载时触发的事件,并且希望绑定到每当新页面添加到 DOM 时触发的 jQuery Mobile 事件。

更改:

var myScroll;
document.addEventListener('DOMContentLoaded', loaded, false);

至:

var myScroll = [];
$(document).delegate('[data-role="page"]', 'pagecreate', function () {
        myScroll[this.id] = new iScroll(this.id + '_wrapper', {
        snap: true,
        momentum: false,
        hScrollbar: false
     });
});

这将需要将每个页面上的 wrapper div 重命名为 _wrapper。无论如何,这是必要的,因为每个具有 ID 的元素在 DOM 中都需要一个唯一的 ID。

链接:将 iscroll 与 jquery mobile 结合使用

--UPDATE- -

我创建了一个在多个页面上使用 iScroll 轮播的示例。请注意我如何在每个页面上包含自定义 JavaScript 和 CSS,这样如果用户刷新页面(在任何页面上),他们就不会因为缺少代码而收到任何错误。

以下是工作示例的链接: http://apexeleven.com/stackoverflow/iScroll/default .html

Follow the example I created for you four days ago ( using iscroll with jquery mobile )... You are binding to an event that only fires on the initial page load and you want to bind to a jQuery Mobile event that fires whenever a new page is added to the DOM.

Change:

var myScroll;
document.addEventListener('DOMContentLoaded', loaded, false);

To:

var myScroll = [];
$(document).delegate('[data-role="page"]', 'pagecreate', function () {
        myScroll[this.id] = new iScroll(this.id + '_wrapper', {
        snap: true,
        momentum: false,
        hScrollbar: false
     });
});

Which will require renaming the wrapper div on each page to _wrapper. Which is necessary anyway because each element with an ID needs a unique ID in the DOM.

Link: using iscroll with jquery mobile

--UPDATE--

I have created an example of using iScroll carousels on multiple pages. Notice how I include the custom JavaScript and CSS on each page so if the user refreshes the page (on any page) they will not receive any errors because of missing code.

Here is the link to the working example: http://apexeleven.com/stackoverflow/iScroll/default.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文