iOS-like stretchy header
For https://news.layervault.com/stories/13207-ask-dn-mimic-ios-apps-stretchy-header-on-the-web
by Hugo Carneiro
HTML
<div id="content">Try dragging me down</div>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
CSS
body {
margin: 0;
padding: 0;
background: purple url(http://farm9.staticflickr.com/8363/8344623421_3d3911f5bb_c.jpg) 50% -200px no-repeat;
background-size: cover;
}
#content {
position: relative;
top: 100px;
height: 500px;
background: #fff;
padding: 20px;
cursor: -moz-grab;
cursor: -webkit-grab;
}
#content.ui-draggable-dragging {
cursor: -moz-grabbing;
cursor: -webkit-grabbing;
}
JavaScript
$(function(){
$('#content').draggable({
axis: 'y',
scroll: false,
drag: function(){
var offset = $(this).position().top - 100
var bkgpos = (offset / 2) - 200
$('body').animate({
'background-position-y': bkgpos
}, 10)
},
stop: function(){
$(this).animate({top: 100}, 200, 'easeOutExpo')
$('body').animate({
'background-position-y': -200
}, 200, 'easeOutExpo')
}
})
})