Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Get Viewport Dimensions
  3.  * returns object with viewport dimensions to match css in width and height properties
  4.  * ( source: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript )
  5. */
  6. function updateViewportDimensions() {
  7.     var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
  8.     return { width:x,height:y };
  9. }
  10. // setting the viewport width
  11. var viewport = updateViewportDimensions();
  12.  
  13.  
  14. /*
  15.  * Throttle Resize-triggered Events
  16.  * Wrap your actions in this function to throttle the frequency of firing them off, for better performance, esp. on mobile.
  17.  * ( source: http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed )
  18. */
  19. var waitForFinalEvent = (function () {
  20.     var timers = {};
  21.     return function (callback, ms, uniqueId) {
  22.         if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; }
  23.         if (timers[uniqueId]) { clearTimeout (timers[uniqueId]); }
  24.         timers[uniqueId] = setTimeout(callback, ms);
  25.     };
  26. })();
  27.  
  28. (function($) {
  29.  
  30.   var setBodyClasses = function() {
  31.     waitForFinalEvent(function(){
  32.       var viewport = updateViewportDimensions();
  33.            
  34.       if ( viewport.width >= 768 ) {
  35.         $("body").removeClass("mobile");
  36.       } else {
  37.         $("body").addClass("mobile");
  38.       }  
  39.     });
  40.   }
  41.    
  42.  // Viewport width related actions
  43.   $(window).resize(function() {
  44.     setBodyClasses();
  45.   });
  46.  
  47.   $(document).ready(function() {
  48.     setBodyClasses();
  49.   });
  50. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement