Advertisement
dimaslanjaka

jQuery Mobile Fixed Toolbar

Mar 9th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.06 KB | None | 0 0
  1. /*!
  2.  jQuery Mobile fixedtoolbar polyfill for blacklisted browsers that don't natively support position:fixed
  3.  Author @scottjehl
  4.  Copyright 2012 Filament Group, Inc.
  5.  License Dual MIT or GPLv2
  6. */
  7. (function( $, undefined ) {
  8.  
  9.     // If the supportBlacklist is returning true, it's a blacklisted browser.
  10.     if( $.mobile.fixedtoolbar.prototype.options.supportBlacklist() && $.support.scrollTop ){
  11.  
  12.         // Keep a reference to the original _create method
  13.         var oldcreate = $.mobile.fixedtoolbar.prototype._create,
  14.  
  15.             // Additional scripting to add to the _create method for polyfilling unsupported browsers
  16.             createPolyfill = function(){
  17.  
  18.                 if( this.options.polyfillSupport === true ){
  19.  
  20.                     var toolbar = this.element,
  21.                         tbType = toolbar.hasClass( "ui-header-fixed") ? "header" : "footer",
  22.                         page = toolbar.closest( ":jqmData(role='page')" );
  23.  
  24.                         // Add faux support class to toolbar
  25.                         toolbar.addClass( "ui-fixed-faux" );
  26.  
  27.                         // set up a function that resets the top or bottom value, depending on toolbar type
  28.                         var resetPos = (function(){
  29.                             if( tbType === "header" ){
  30.                                 return function(){
  31.                                     toolbar.css( "top", $( window ).scrollTop() + "px" );
  32.                                 }; 
  33.                             }
  34.                             else {
  35.                                 return function(){
  36.                                    
  37.                                     toolbar.css( "bottom", page.outerHeight() - $( window).scrollTop() - $.mobile.getScreenHeight() + "px" );
  38.                                 }
  39.                                
  40.                             }
  41.                         })();
  42.                        
  43.                         $.mobile.fixedtoolbar.prototype.updatePosition = resetPos;
  44.  
  45.                         // Per page show, re-set up the event handling
  46.                         page.bind( "pagebeforeshow", function( e ){
  47.                             var visible;
  48.  
  49.                             // Normalize proper object for scroll event
  50.                             ( ( $( document ).scrollTop() === 0 ) ? $( window ) : $( document ) )
  51.                                 .bind( "scrollstart.fixedtoolbarpolyfill", function(){
  52.                                     visible = toolbar.not( ".ui-fixed-hidden" ).fixedtoolbar( "hide", true );
  53.                                 })
  54.                                 .bind( "scrollstop.fixedtoolbarpolyfill", function(){
  55.                                     resetPos();
  56.                                     visible.fixedtoolbar( "show" );
  57.                                 });
  58.  
  59.                             // on resize, reset positioning
  60.                             $( window ).bind( "throttledresize.fixedtoolbarpolyfill", resetPos );
  61.  
  62.                             // on pagehide, unbind the event handlers
  63.                             page.one( "pagehide", function(){
  64.                                 $( this ).add( this ).add( document ).unbind( ".fixedtoolbarpolyfill" );
  65.                             });
  66.  
  67.                             // align for pageshow
  68.                             resetPos();
  69.                     });
  70.                 }
  71.             };
  72.  
  73.         // Set the blacklist test return false, letting any normally-blacklisted browsers in to be polyfilled
  74.         $.mobile.fixedtoolbar.prototype.options.supportBlacklist = function(){
  75.             return false;
  76.         };
  77.  
  78.         // Define a new option for polyfillSupport, which can be disabled per call or via data attr data-polyfill-support
  79.         $.mobile.fixedtoolbar.prototype.options.polyfillSupport = true;
  80.  
  81.         // Overwrite the _create method with the old and the new
  82.         $.mobile.fixedtoolbar.prototype._create = function(){
  83.  
  84.             // Call the old _create method.
  85.             oldcreate.call( this );
  86.  
  87.             // Call the polyfill scripting
  88.             createPolyfill.call( this );
  89.         };
  90.     }
  91.  
  92. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement