Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Get Viewport Dimensions
- * returns object with viewport dimensions to match css in width and height properties
- * ( source: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript )
- */
- function updateViewportDimensions() {
- 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;
- return { width:x,height:y };
- }
- // setting the viewport width
- var viewport = updateViewportDimensions();
- /*
- * Throttle Resize-triggered Events
- * Wrap your actions in this function to throttle the frequency of firing them off, for better performance, esp. on mobile.
- * ( source: http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed )
- */
- var waitForFinalEvent = (function () {
- var timers = {};
- return function (callback, ms, uniqueId) {
- if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; }
- if (timers[uniqueId]) { clearTimeout (timers[uniqueId]); }
- timers[uniqueId] = setTimeout(callback, ms);
- };
- })();
- // how long to wait before deciding the resize has stopped, in ms. Around 50-100 should work ok.
- var timeToWaitForLast = 100;
- /************** EXAMPLES *******************************************/
- /*
- * Responsive viewport AND resize throttling example....
- */
- // check window resize, close mmenu if window resized above mobile menu breakpoint
- $(window).resize(function () {
- // wait until resizing is done...
- waitForFinalEvent( function() {
- // update the viewport, in case the window size has changed
- viewport = updateViewportDimensions();
- // if we're above or equal to 992px (mobile menu breakpoint), then fire!
- if( viewport.width >= 992 ) {
- var API = $("#MobileMenuWrap").data( "mmenu" );
- API.close();
- } else {
- // console.log('No worries.');
- }
- }, timeToWaitForLast, "resize-mobile-menu-checker");
- });
- /*
- * Responsive viewport Gravatars....Call this function in doc.ready to hide Gravatars
- * until an appropriate viewport size. This does NOT throttle frequency of resize check.
- */
- function loadGravatars() {
- // set the viewport using the function above
- viewport = updateViewportDimensions();
- // if the viewport is tablet or larger, we load in the gravatars
- if (viewport.width >= 768) {
- jQuery('.comment img[data-gravatar]').each(function(){
- jQuery(this).attr('src',jQuery(this).attr('data-gravatar'));
- });
- }
- } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement