【问题标题】:jquery mobile switch between grid and list viewjquery mobile在网格和列表视图之间切换
【发布时间】:2013-04-12 11:24:50
【问题描述】:

我正在尝试创建一个 jquery 函数,该函数在我的 jqm 页面上的元素的网格和列表类型布局之间切换。这是我的html结构:

<div data-role="page" id="portfolio">
<div data-role="header">
    <a href="#main" data-role="button" data-icon="arrow-l" data-iconpos="notext" data-theme="b" data-inline="true"></a>
    <h1>Projects</h1>
    <a href="javascript:changeLayout()" id="changelayout" class="ui-btn-right" data-role="button" data-icon="grid" data-iconpos="notext" data-theme="b" data-inline="true"></a>
</div>
<div data-role="content">
    <ul id="projects">
        <li><a href="#incarpi"><img src="images/incarpi.jpg" ><div class="portfoliotext">Raspberry Pi In-car computer</div></a></li>

...

这是我的功能:

function changeLayout() {
if ($('#changelayout').attr('data-icon') == 'grid'){
    $('#changelayout').attr('data-icon', 'bars');
    $('#changelayout .ui-icon').addClass('ui-icon-bars').removeClass('ui-icon-grid');
    $('#changelayout').buttonMarkup('refresh');
    $('#projects li img').width('100%');
    $('#projects li').display('none');
    $('.portfoliotext').show();
}
else {
    $('#changelayout').attr('data-icon', 'grid');
    $('#changelayout .ui-icon').addClass('ui-icon-grid').removeClass('ui-icon-bars');
    $('#changelayout').buttonMarkup('refresh');
    $('#projects li img').width('20%');
    $('#projects li').margin('0');
    $('#projects li').display('inline-block');
    $('.portfoliotext').hide();
}

}

但是,这些元素仍然只是一个在另一个之上。我做错了什么?

谢谢

【问题讨论】:

    标签: jquery css jquery-mobile jquery-mobile-listview jquery-mobile-grid


    【解决方案1】:

    工作示例:http://jsfiddle.net/Gajotres/PMrDn/

    $(document).on('pagebeforeshow', '#index', function(){ 
        $(document).on('click', '#changelayout', function(){             
            if ($('#changelayout').attr('data-icon') == 'grid'){
                $('#changelayout').buttonMarkup({ icon: "bars" });
                $( "#projects li" ).each(function( index ) {
                    $(this).width('100%');
                    $(this).css('float','clear');            
                    $(this).find('.portfoliotext').css('display','none');
                });            
            } else {
                $('#changelayout').buttonMarkup({ icon: "grid" });            
                $( "#projects li" ).each(function( index ) {            
                    $(this).width('20%');
                    $(this).css({'float':'left','margin':'0'});                        
                    $(this).find('.portfoliotext').css('display','block');                   
                });                         
            }
        });        
    });
    

    我希望就是这样。

    【讨论】:

      【解决方案2】:

      你试过了吗?

      $("#changelayout").attr('data-icon','bars').button().trigger("refresh");                             
      

      或者你也可以这样做。

      $("#changelayout").find(".ui-icon").removeClass("ui-icon-bars").addClass("ui-icon-grid");
      

      【讨论】:

      • 我想你误解了我的意思,按钮图标变化很好,但 HTML 元素 (#projects li) 改变大小但仍保留为列表,我希望它们在网格中并排显示
      猜你喜欢
      • 2023-04-04
      • 2020-03-27
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多