Do I have to create a new panel for every page?

后端 未结 11 1496
Happy的楠姐
Happy的楠姐 2020-12-13 22:30

I would like to use a Panel in a jqm site for my Choose Language component. So that component will need to be present on every page. Here is the setup for a single-page pane

11条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 23:06

    with inspiration from Gajotres and the way AppFramework handles panels I've made this. It works by copying defined panels to the active page, the panels are defined by id in right-panel and left-panel attributes for the page div:

    $(document).on('pagebeforeshow', '[data-role="page"]', function(){
        // remove unused tmp panels from DOM
        $("#tmpRightPanel").remove();
        $("#tmpLeftPanel").remove();
    
        // Hide buttons by default (I'm using a static header and footer on every page too)
        $("#openRightPanel").css("display", "none");
        $("#openLeftPanel").css("display", "none");
    
        // check if right-panel attribute is set on the page
        if ($(this).attr("right-panel")) {
            // if it is, it should append the defined right-panel to the page
            $("#"+$(this).attr("right-panel")).clone().appendTo($(this));
    
            // rename it to tmpRightPanel
            $.mobile.activePage.find('#'+$(this).attr("right-panel")).attr("id", "tmpRightPanel");
    
            // make it a panel
            $.mobile.activePage.find('#tmpRightPanel').panel();
    
            // make it visible (the original right panel is set to display: none)
            $.mobile.activePage.find('#tmpRightPanel').css("display", "block");
    
            // make the button to open the panel visible
            $("#openRightPanel").css("display", "block");
        }
    
        // same as right-panel above
        if ($(this).attr("left-panel")) {
            $("#"+$(this).attr("left-panel")).clone().appendTo($(this));
            $.mobile.activePage.find('#'+$(this).attr("left-panel")).attr("id", "tmpLeftPanel");
            $.mobile.activePage.find('#tmpLeftPanel').panel();
            $.mobile.activePage.find('#tmpLeftPanel').css("display","block");
            $("#openLeftPanel").css("display", "block");
        }
    });
    
    // make the open panel buttons clickable
    $(document).on('click', '#openRightPanel', function(){   
        $.mobile.activePage.find('#tmpRightPanel').panel("open");
    });
    
    $(document).on('click', '#openLeftPanel', function(){
        $.mobile.activePage.find('#tmpLeftPanel').panel("open");
    });
    

    Make a page like this:

        
    some page

    and place the panels somewhere outside a page, and hide them like this:

        
        
        
    
        
        
        
    

提交回复
热议问题