且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用iScroll与JQM和动态内容

更新时间:2023-09-29 11:08:22

iScroll动态生成包含滚动元素的div,这div有类 iscroll含量

 < D​​IV CLASS =例如,包装数据iscroll>< / DIV>

变为

 < D​​IV CLASS =例如,包装数据iscroll>
  < D​​IV CLASS =iscroll内容>< / DIV>
< / DIV>

所以,当你使用 $(。例如,包装)。HTML()您删除分区中的所有内容,相反,你应该使用 $(。例如,包装.iscroll内容)。HTML()来清除previous内容/元素。

此外,您还需要新元素追加到 iscroll含量,然后的刷新的两种,列表视图() .iscrollview()

 的$(document)。在('pagebeforeshow','#INDEX',函数(){
    $(例如,包装.iscroll的内容。)HTML()。    VAR HTML ='< UL数据角色=列表视图>';
    对于(I = 0; I&小于30;我++){
        HTML + ='<立GT;< A HREF =#>链接'+ I +'< / A>< /李>';
    }
    HTML + ='< / UL>';    $(。例如,包装.iscroll内容)追加(HTML);
    $([数据角色=列表视图])列表视图()。
    $(例如-包装)iscrollview(刷新)。
});


  

演示


块引用>

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/

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