Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions example/example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
body {
background: #f9f9f9;
color: #777;
font-family: sans-serif;
}

h1 { color: #333; }

#container {
width: 85%;
max-width: 600px;
margin: 10% auto 0;
}

/* Reset some list defaults for all lists */
ul, ol {
list-style: none;
margin: 0;
padding: 0;
}

li {
text-indent: 1em;
}

/* The main container */
#sticky-list {
height: 300px;
overflow: hidden;
position: relative;

border: thin solid #dfdfdf;
background: white;
}

/* The main list */
#sticky-list > ul, #sticky-list > ol {
height: 100%;
overflow: auto;
}

/* Section headers, defined through "headlineSelector" */
#sticky-list > ul > li strong,
#sticky-list > ol > li strong {
font-weight: normal;
font-size: 1.1em;
color: #222;
background: #ccc;
display: block;
padding: 0.5em 0;
color: #fff;
text-shadow: 0px -1px 0px #000;
opacity: .75;
background-image: -webkit-gradient( linear, left top, left bottom, color-stop(0, #646262), color-stop(0.5, #484848), color-stop(0.5, #3D3D3D), color-stop(1, #4C4C4C) );
background-image: -moz-linear-gradient( center top, #646262 0%, #484848 50%, #3D3D3D 50%, #4C4C4C 100% );
background-image: linear-gradient( center top, #646262 0%, #484848 50%, #3D3D3D 50%, #4C4C4C 100% );
}

/* Section headers when "sticky", defined through "stickyClass" */
#sticky-list li.sticky strong {
position: absolute;
top: 0;
}

ul ul li,
ol ol li,
ul ol li,
ol ul li {
padding: 1em 0;
border-bottom: thin solid #dfdfdf;
}

ul ul li:last-child,
ol ol li:last-child,
ul ol li:last-child,
ol ul li:last-child {
border-bottom: none;
}
78 changes: 78 additions & 0 deletions example/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!doctype html>
<html class="no-js" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml" lang="en-US">
<head>
<meta charset="utf-8">
<title>Sticky Section Headers example</title>

<link rel="stylesheet" href="example.css">
</head>

<body>

<div id="container">
<header>
<h1>Sticky Section Headers example</h1>
</header>

<div id="sticky-list">
<ol>
<li>
<strong>Section Headline 1</strong>
<ol>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
</ol>
</li>
<li>
<strong>Section Headline 2</strong>
<ol>
<li>Content line</li>
<li>Content line</li>
</ol>
</li>
<li>
<strong>Section Headline 4</strong>
<ol>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
</ol>
</li>
<li>
<strong>Section Headline 5</strong>
<ol>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
<li>Content line</li>
</ol>
</li>
</ol>
</div>

</div> <!-- eo #container -->


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="../src/jquery.stickysectionheaders.js"></script>

<script type="text/javascript">
$(function() {
$('#sticky-list').stickySectionHeaders({
stickyClass : 'sticky',
headlineSelector: 'strong'
});
});
</script>
</body>
</html>
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "stickySectionHeaders",
"version": "1.0.0",
"description": "Makes headers for sections in lists sticky. Known from the iPhone OS tables views.",
"keywords": [
"sticky section headers",
"persistant headers",
"iOS headers",
"scroll headers"
],
"author": "Polarblau <[email protected]>",
"repository": {
"type": "git",
"url": "git://github.com/polarblau/stickySectionHeaders.git"
},
"main": "src/jquery.stickysectionheaders.js",
"jam": {
"main": "src/jquery.stickysectionheaders.js",
"include": ["src/jquery.stickysectionheaders.js"],
"dependencies": {
"jquery": ">=1.3"
},
"shim": {
"deps": ["jquery"]
}
}
}
32 changes: 26 additions & 6 deletions src/jquery.stickysectionheaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
*
*/

(function($){
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {

$.fn.stickySectionHeaders = function(options) {

var settings = $.extend({
Expand All @@ -23,9 +32,19 @@
}, options);

return $(this).each(function() {
var $this = $(this);
$(this).find('ul:first').bind('scroll.sticky', function(e) {
$(this).find('> li').each(function() {
var $this = $(this),
$list = $(this).find('ul,ol').first(),
$children = $list.find('> li');

$children.each(function() {
$(this).data( "width", $(this).outerWidth() );
$(this).data( "pad", $(this).cssSum("paddingLeft", "paddingRight") );
});

$list.on('scroll.sticky', function(e) {
var listTop = $list.scrollTop();

$children.each(function() {
var $this = $(this),
top = $this.position().top,
height = $this.outerHeight(),
Expand All @@ -36,7 +55,7 @@
$this.addClass(settings.stickyClass).css('paddingTop', headHeight);
$head.css({
'top' : (height + top < headHeight) ? (headHeight - (top + height)) * -1 : '',
'width': $this.outerWidth() - $head.cssSum('paddingLeft', 'paddingRight')
'width': $this.data("width") - $this.data("pad")
});
} else {
$this.removeClass(settings.stickyClass).css('paddingTop', '');
Expand All @@ -60,4 +79,5 @@
return sum;
};

})(jQuery);
})
);