vertical content centering with jQuery Mobile
vertically center jQuery mobile content using declarative data tag.
by Stuart Gillibrand
HTML
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
<section data-role="page" id="page1">
<header data-role="header">
header stuff...
</header>
<div data-role="content">
hi there
<a href="#page2" data-role="button">clicky</a>
</div>
<footer data-role="footer" data-position="fixed">
footer stuff...
</footer>
</section>
<section data-role="page" id="page2">
<header data-role="header">
more header stuff...
</header>
<div data-role="content" data-vertical-centred>
hi there in the middle!
<a href="#page1" data-role="button">clicky</a>
</div>
<footer data-role="footer" data-position="fixed">
more footer stuff...
</footer>
</section>
JavaScript
applyVerticalCentering = function() {
$(this).on('pagebeforeshow',function(e,data){
$('[data-vertical-centred]').hide();
});
$(this).on('pageshow resize',function(e,data){
$('[data-vertical-centred]').each(function(index){
var _this = $(this);
_this.css('margin-top',
($(window).height() -
$('header:visible').height() -
$('footer:visible').height() -
_this.outerHeight())/2);
_this.show();
});
});
}();