在html5中水平滚动以动态创建<li> </li>

时间:2013-08-07 04:38:23

标签: android jquery-mobile cordova scroll

我正在使用Phonegap + jQueryMobile + Android。当我创建我的<li>静态时,它工作正常,但动态地它不像静态一样工作。请检查我在下面的代码中做了什么错误:

在jquery中: -

    $(document).unbind('pageinit').bind('pageinit', function () {

          callMenuConnection(); 

      });
       function callMenuConnection() {  
        $.support.cors = true;
           $.ajax({
                type: "GET",
                url: "one.html",
                contentType: "text/xml",
                dataType: "xml",
                data: "",
                cache:false,
                processData:false,
                crossDomain:true,
                success: processSuccess,
                error: processError
            }); 
      }

      function processSuccess(data) {
             $(data).find("category").each(function () {     
             var id = $(this).find('id').text();
             var title = $(this).find('title').text();
            $('#cat_list').append('<li><span><a href="#" data-role="button" data-inline="true">'+title+'</a></span></li>');
            });
      }

         function processError(data)
           {
               alert("error");
           }


      $(function(){           
var step = 1;
var current = 0;
var maximum = $(".categories ul li").size();
var visible = 2;
var speed = 500;
var liSize = 120;
var height = 60;    
var ulSize = liSize * maximum;
var divSize = liSize * visible; 

$('.categories').css("width", "auto").css("height", height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative");
$(".categories ul li").css("list-style","none").css("display","inline");
$(".categories ul").css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute").css("white-space","nowrap").css("margin","0px").css("padding","5px");

$(".categories").swipeleft(function(event){
    if(current + step < 0 || current + step > maximum - visible) {return; }
    else {
        current = current + step;
        $('.categories ul').animate({left: -(liSize * current)}, speed, null);
    }
    return false;
});

$(".categories").swiperight(function(){
    if(current - step < 0 || current - step > maximum - visible) {return; }
    else {
        current = current - step;
        $('.categories ul').animate({left: -(liSize * current)}, speed, null);
    }
    return false;
});         
 });

在css中: -

   div.sc_menu {
          /* Set it so we could calculate the offsetLeft */
              position: relative;
               height: 145px;
               width: 300px;
               overflow: auto;
               }
ul.sc_menu {
display: block;
height: 110px;
/* max width here, for users without javascript */    
width: 1500px;   
padding: 15px 0 0 15px; 
/* removing default styling */
margin: 0;
background: url('navigation.png');        
list-style: none;
 }
    .sc_menu li {
display: block;
float: left;    
padding: 0 4px;
  }
    .sc_menu a {
display: block;
text-decoration: none;
}
       .sc_menu span {
  /* display: none; */
  margin-top: 3px;
  text-align: center;
  font-size: 12px;    
  color: #fff;
   }
 .sc_menu a:hover span {
display: block;
 }
     .sc_menu img {
    border: 3px #fff solid;    
    -webkit-border-radius: 3px;
     -moz-border-radius: 3px;
      }
       .sc_menu a:hover img {
          filter:alpha(opacity=50);    
           opacity: 0.5;
        }


    /* Here are styles for the back button, don't look at them */
  #back {
   display: block;
    width: 500px;
     text-align: center;
     color: #003469;
     font-size: 16px;
 }

在Html5中: -

    <div data-role="page" data-theme="b" id="jqm-home">            
            <div class="categories">                
                  <ul id="cat_list"></ul>               
            </div>
     </div>

4 个答案:

答案 0 :(得分:1)

刷新你的ul列表

  function processSuccess(data) {
                 $(data).find("category").each(function () {     
                 var id = $(this).find('id').text();
                 var title = $(this).find('title').text();
                $('#cat_list').append('<li><span><a href="#" data-role="button" data-inline="true">'+title+'</a></span></li>');
                });
     $('#cat_list').listview('refresh');
          }

OR

$('#cat_list').append('<li><span><a href="#" data-role="button" data-inline="true">'+title+'</a></span></li>').listview('refresh');

编辑:

function processSuccess(data) {
var data="";
                     $(data).find("category").each(function () {     
                     var id = $(this).find('id').text();
                     var title = $(this).find('title').text();
                   data+='<li><span><a href="#" data-role="button"  data-inline="true">'+title+'</a></span></li>';
                    });
 $("#cat_list").html(data).listview('refresh');

              }

答案 1 :(得分:1)

尝试 jqMobile + iScrollView +无限滚动

有关详细信息,请查看: Infinite Scrolling

此外,您还可以在插件网站https://github.com/watusi/jquery-mobile-iscrollview

上看到演示

答案 2 :(得分:1)

尝试$('#mylist')。listview();强制执行渲染。

答案 3 :(得分:0)

最后我得到了这些

的答案

在HTML5中: -

 <div data-role="page" data-theme="b" id="jqm-home">    
      <div data-role="footer" data-position="fixed" data-theme="c">        
           <div  class="categories" id="cat">                
              <ul id="cat_list" class="cat_list_class"></ul>               
           </div>
      </div>    
</div>

在jquery中: -

 var step = 1;
var current = 0;
 var maximum = 0;
 var visible = 2;
 var speed = 500;
 var liSize = 120;
 var height = 60;    
 var ulSize = liSize * maximum;
  var divSize = liSize * visible;

 $(document).unbind('pageinit').bind('pageinit', function () {    
      callMenuConnection(); 
       $('.categories').css("width", "auto").css("height", height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative");
       $(".categories ul a").css("list-style","none").css("display","inline");
       $(".categories ul").css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute").css("white-space","nowrap").css("margin","0px").css("padding","5px");      
  });

 $(document).unbind('click').bind('click', function () {
        scroll();
 });
   function callMenuConnection() {  
       $.support.cors = true;
       $.ajax({
            type: "GET",
            url: "one.html",
            contentType: "text/xml",
            dataType: "xml",
            data: "",
            cache:false,
            processData:false,
            crossDomain:true,
            success: processSuccess,
            error: processError
        }); 
  }
      var scripts ="";     
  function processSuccess(data) {
         $(data).find("category").each(function () {     
         var id = $(this).find('id').text();
         var title = $(this).find('title').text();
          scripts = scripts+'<a  class="ids_cat" data-role="button" data-transition="slide"  data-inline="true" >' +title+ '</a>';
        });
        $('#cat_list').append(scripts);
        $('#cat_list').trigger('create');
         maximum = $(".categories ul a").size();
  }

     function processError(data)
       {
           alert("error");
       }

 function scroll(){ 
 $(".categories").swipeleft(function(event){
  if(current + step < 0 || current + step > maximum - visible) {return; }
else {
    current = current + step;
    $('.categories ul').animate({left: -(liSize * current)}, speed, null);
}
return false;
 });

  $(".categories").swiperight(function(event){
      if(current - step < 0 || current - step > maximum - visible) {return; }
     else {
         current = current - step;
        $('.categories ul').animate({left: -(liSize * current)}, speed, null);
   }
   return false;
 });         
}