Last active
August 29, 2015 14:03
-
-
Save swashcap/8fbaeb1f8ffc4d7df164 to your computer and use it in GitHub Desktop.
Example of responsive formatting an "Onboard" section.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global Modernizr */ | |
jQuery(function ($) { | |
'use strict'; | |
var SCREEN_SMALL = '(min-width: 768px)'; | |
/** | |
* "Welcome" onboarding message. | |
*/ | |
var $onboard = $('.onboard'); | |
var $site = $('.site'); | |
var formatOnboard = function () { | |
if ( | |
$onboard.length && | |
Modernizr.mq(SCREEN_SMALL) && | |
$onboard.height() < $(window).height() | |
) { | |
$onboard.css({ | |
position: 'fixed', | |
width: '100%' | |
}); | |
$site.css({ | |
top: $onboard.height(), | |
position: 'relative' | |
}); | |
} else { | |
$onboard.removeAttr('style'); | |
$site.removeAttr('style'); | |
} | |
}; | |
// Initialize | |
formatOnboard(); | |
$(window).resize($.debounce(125, formatOnboard)); | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couple things:
Modernizr.mq(SCREEN_SMALL)
runs Modernizr’s polyfill formatchMedia()
. It returns a boolean based on the argument. Not all installs of Modernizr come with it; check your download.You’ll need to tear down what you’re setting up. Note the block:
The function needs to initialize, so call
formatOnboard()
(line 35).$.debounce(125, formatOnboard)
debounces the window’sonresize
event using jQuery-Throttle-Debounce. It will need to be included on the page.