Persistent header in jQuery Mobile

后端 未结 10 761
慢半拍i
慢半拍i 2020-12-16 01:49

Couldn\'t figure out a way to put a bounty on my old question, so I\'m reposting it because maybe it was a bug.

Short version: I want a persistent header in a PhoneG

10条回答
  •  天命终不由人
    2020-12-16 02:17

    I've been banging my head against this problem for several days, and for once Google was no help. I finally came up with the following solution. It copies the header HTML onto a new page before the transition begins, then removes the code from the previous page once the transition completes. The header and footer will still move with the page transition, but they will persist even while navigating nested lists.

    // dynamically move the header and footer between pages on load events
    $('div.ui-page').live('pagebeforeshow', function(event, ui) {
        // avoid duplicating the header on the first page load
        if (ui.prevPage.length == 0) return;
    
        // remove the jQuery Mobile-generated header
        $('.ui-header').addClass('to-remove-now');
        $('#header').removeClass('to-remove-now');
        $('.to-remove-now').remove();
    
        // grab the code from the current header and footer
        header = $('#header')[0].outerHTML;
        footer = $('#footer')[0].outerHTML;
    
        // mark the existing header and footer for deletion
        $('#header').addClass('to-remove');
        $('#footer').addClass('to-remove');
    
        // prepend the header and append the footer to the generated HTML
        event.currentTarget.innerHTML = header + event.currentTarget.innerHTML + footer;
    });
    
    // remove header from previous page 
    $('div.ui-page').live('pagehide', function(event, ui) {
        $('.to-remove').remove();
    });
    

    Then just add id="header" to the header div and id="footer" to the footer, and place them as you normally would in your content.

提交回复
热议问题