Using iScroll with JQM and dynamic content

我们两清 提交于 2019-12-01 12:16:37

问题


I am trying to implement iScroll into my HMTL5 game using jQuery. I can't seem to get it to work? I have followed the guide here: http://www.gajotres.net/using-iscroll-with-jquery-mobile/

Here is what I am trying to do:

$(document).on('pagebeforeshow', '#index', function(){ 
    $(".example-wrapper").html("");

    var html = '<ul data-role="listview">';
    for(i = 0; i < 30; i++) {
        html += '<li><a href="#">link '+i+'</a></li>';
    }
    html += '</ul>';
    $(".example-wrapper").append(html);
    $(".example-wrapper").iscrollview("refresh");
});

The difference in the project is that it uses trigger and I am using append... I can't seem t get the connection?

My example is on jsfiddle here: http://jsfiddle.net/jmansa/952NJ/23/


回答1:


iScroll dynamically generates a div which contains scrollable elements, that div has class iscroll-content.

<div class="example-wrapper" data-iscroll></div>

Becomes

<div class="example-wrapper" data-iscroll>
  <div class="iscroll-content"></div>
</div>

So when you use $(".example-wrapper").html("") you remove all contents of the div, instead, you should use $(".example-wrapper .iscroll-content").html("") to clear previous contents/elements.

Also, you need to append new elements to iscroll-content, and then refresh both, listview() and .iscrollview().

$(document).on('pagebeforeshow', '#index', function () {
    $(".example-wrapper .iscroll-content").html("");

    var html = '<ul data-role="listview">';
    for (i = 0; i < 30; i++) {
        html += '<li><a href="#">link ' + i + '</a></li>';
    }
    html += '</ul>';

    $(".example-wrapper .iscroll-content").append(html);
    $("[data-role=listview]").listview();
    $(".example-wrapper").iscrollview("refresh");
});

Demo



来源:https://stackoverflow.com/questions/20616078/using-iscroll-with-jqm-and-dynamic-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!