简体   繁体   English

使用iscroll和jquery移动绑定问题

[英]using iscroll with jquery mobile binding issue

Im currently pulling my hair out trying to get iscroll 4 working with jQuery Mobile. 我正在努力让我的头发试图让iscroll 4与jQuery Mobile一起工作。

It all works fine if i disable JQM ajax default navigation but I would like to retain this. 如果我禁用JQM ajax默认导航这一切都工作正常,但我想保留这个。

My issue is I cant work out how to successfully call/bind iscroll so it works with the pages that need them. 我的问题是我无法解决如何成功调用/绑定iscroll所以它适用于需要它们的页面。 I have tried pageinit() and pagecreate() to no avail. 我试过pageinit()和pagecreate()无济于事。

A basic example of this can be found here: http://bit.ly/ngXkNR 可以在这里找到一个基本的例子: http//bit.ly/ngXkNR

Any pointers much appreciated. 任何指针都非常赞赏。

A. 一种。

Thanks Jasper, I changed your method a bit, so that you can call iScroll on any wrapper identified with a class. 谢谢Jasper,我稍微改变了你的方法,这样你就可以在任何用类标识的包装上调用iScroll。 Also I unload and destroy all iScroll instances on the pagehide event - i dont need the refresh method for my needs: 另外,我在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);
    }

});

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. 按照我四天前为您创建的示例( 使用iscroll with jquery mobile )...绑定到仅在初始页面加载时触发的事件,并且您希望绑定到每当新页面触发的jQuery Mobile事件被添加到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. 这将需要将每页上的wrapper div重命名为_wrapper。 Which is necessary anyway because each element with an ID needs a unique ID in the DOM. 无论如何,这是必要的,因为具有ID的每个元素在DOM中都需要唯一的ID。

Link: using iscroll with jquery mobile 链接: 使用iscroll和jquery mobile

--UPDATE-- --UPDATE--

I have created an example of using iScroll carousels on multiple pages. 我创建了一个在多个页面上使用iScroll轮播的示例。 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. 请注意我如何在每个页面上包含自定义JavaScript和CSS,因此如果用户刷新页面(在任何页面上),由于缺少代码,它们不会收到任何错误。

Here is the link to the working example: http://apexeleven.com/stackoverflow/iScroll/default.html 以下是工作示例的链接: http//apexeleven.com/stackoverflow/iScroll/default.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM