Last active
December 14, 2015 23:39
-
-
Save bakertim/5167600 to your computer and use it in GitHub Desktop.
Fixed headers in html like table views in iOS designed for use in either html or markdown.
- Requires JQuery
- adapted from http://jsfiddle.net/QuVkV/3/
This file contains hidden or 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
<p>Lorem or incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> | |
<h1>This is your facebook like button</h1> | |
<p>Lorem or incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> | |
<h1>Another bar here</h1> | |
<p>Lorem or incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> | |
<p style='height: 5000px;'></p> |
This file contains hidden or 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
$(document).ready(function(){ | |
$('h1').each( function() { | |
$(this).wrap('<div class="bar-container" />');; | |
}); | |
$(document).on('scroll', function(){ | |
$('h1').each( function() { | |
if ($(this)[0].offsetTop < $(document).scrollTop()){ | |
if($(this)[0].offsetTop != 0) { | |
$.data(this, 'offset', $(this)[0].offsetTop); | |
} | |
$(this).css({position: "fixed", top:0}); | |
} | |
if ($(document).scrollTop() < $.data(this, 'offset')){ | |
//$.data(this, 'offset', 1); | |
$(this).css({position: "relative", top: 0}); | |
} | |
}); | |
}); | |
}); |
This file contains hidden or 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
<style> | |
.bar-container { | |
height: 50px; | |
margin-bottom: 50px; | |
} | |
h1 { | |
position: relative; | |
width: 100%; | |
max-width: 720px; | |
height : 50px; | |
margin-bottom: 40px; | |
background-color: #efefef; | |
-webkit-margin-before: 0; | |
padding-top: 30px; | |
border: solid 2px #333; | |
} | |
</style> | |
<script src='http://code.jquery.com/jquery-1.9.1.js'></script> | |
<script> | |
$(document).ready(function(){ | |
$('h1').each( function() { | |
$(this).wrap('<div class="bar-container" />'); | |
}); | |
$(document).on('scroll', function(){ | |
$('h1').each( function() { | |
if ($(this)[0].offsetTop < $(document).scrollTop()){ | |
if($(this).css('position') != 'fixed') { | |
$.data(this, 'offset', $(this)[0].offsetTop); | |
} | |
$(this).css({position: "fixed", top:0}); | |
} | |
if ($(document).scrollTop() < $.data(this, 'offset')){ | |
$(this).css({position: "relative", top: 0}); | |
} | |
}); | |
}); | |
}); | |
</script> |
This file contains hidden or 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
.bar-container { | |
height: 50px; | |
} | |
.bar { | |
position: relative; | |
height : 50px; | |
width: 100%; | |
margin-bottom: 40px; | |
color: #fff; | |
background-color: #000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment