How to keep same header/footer across pages in jQueryMobile?

前端 未结 3 1621
天涯浪人
天涯浪人 2020-12-25 14:12

Is there an easy method to keep the same header/footer while navigating jQueryMobile pages? The only solutions I came across so far relies on injecting it dynamicly on page-

3条回答
  •  隐瞒了意图╮
    2020-12-25 15:06

    A built in solution for your problem doesn't exist. jQuery Mobile doesn't have a solution that will share a header and footer among loaded pages.

    The only thing you can do is dynamical inject them or have them from the beginning. In your case you are doing it at the wrong time. If you want to correctly add a header and footer you need to do it during the correct page event.

    A working example: http://jsfiddle.net/Gajotres/xwrqn/

    Swipe to change pages and see how it works (I didn't want to bother with adding buttons on every page).

    $(document).on('pagebeforecreate', '#article2, #article3', function(){ 
        $('
    ').attr({'data-role':'header','data-theme':'b','data-position':'fixed','data-id':'footer'}).append('

    Article

    ').appendTo($(this)); $('
    ').attr({'data-role':'footer','data-theme':'b','data-position':'fixed','data-id':'footer'}).append('

    Article footer

    ').appendTo($(this)); });

    If you do it during the pagebeforecreate this will trigger ONLY once when page is created for a first time. Dynamic content will be added and because pagebeforecreate is triggered before content markup is enhanced you will not need to worry about header and footer styling.

    Notice a attribute 'data-id':'footer' added to every header and footer, because of it only content will animate during the page transition, header and footer will look the same. Also, jsFiddle has a bug, when you swipe through pages they will jump 1-2px. This will not happen in a real life example.

提交回复
热议问题