aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/jquery.fixedthead.js
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2015-08-26 13:43:10 +0200
committerMarius Halden <marius.h@lden.org>2015-08-26 13:43:10 +0200
commitcc0acdd34052e79f3df368ac1f524de31df19a1b (patch)
tree505f561b5f16c5b78f07514e8c2b2bdc18fef51d /web/js/jquery.fixedthead.js
parent1c5c685d0b0904e7ddc6e764e58e8fae08632d1d (diff)
parent6b84622fb7d58531baa7943abdcc7620999c34ee (diff)
Merge tag 'v1.6.1' into fiksgatami-dev
Diffstat (limited to 'web/js/jquery.fixedthead.js')
-rw-r--r--web/js/jquery.fixedthead.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/web/js/jquery.fixedthead.js b/web/js/jquery.fixedthead.js
new file mode 100644
index 000000000..33e60f721
--- /dev/null
+++ b/web/js/jquery.fixedthead.js
@@ -0,0 +1,81 @@
+/*
+ * jQuery.fixedThead.js
+ * By Zarino at mySociety
+ */
+
+(function ($) {
+
+ // Call this on a <thead> element and it'll be given a class
+ // of '.js-fixed-thead__clone' when you scroll down. eg:
+ // $('#my-table thead').fixedThead()
+ //
+ // You'll probably want to specify some CSS styles like:
+ // .js-fixed-thead__clone { position: fixed; background: #fff; }
+
+ $.fn.fixedThead = function() {
+
+ var calculateCloneDimensions = function calculateCloneDimensions($originalThead, $cloneThead){
+ $cloneThead.css({
+ width: $originalThead.width()
+ });
+
+ $('tr', $originalThead).each(function(tr_index, tr){
+ $('th', tr).each(function(th_index, th){
+ $cloneThead.find('tr:eq(' + tr_index + ') th:eq(' + th_index + ')').css({
+ width: $(th).width()
+ });
+ });
+ });
+ }
+
+ var showOrHideClone = function showOrHideClone($table, $originalThead, $cloneThead){
+ var bounds = $table[0].getBoundingClientRect();
+
+ // First we detect whether *any* of the table is visible,
+ // then, if it is, we position the fixed thead so that it
+ // never extends outside of the table bounds even when the
+ // visible portion of the table is shorter than the thead.
+
+ if(bounds.top <= 0 && bounds.bottom >= 0){
+ $cloneThead.css('display', $originalThead.css('display'));
+
+ var rowHeight = $cloneThead.outerHeight();
+ if(bounds.bottom < rowHeight){
+ $cloneThead.css({
+ top: (rowHeight - bounds.bottom) * -1
+ });
+ } else {
+ $cloneThead.css({
+ top: 0
+ });
+ }
+
+ } else {
+ $cloneThead.css('display', 'none');
+ }
+ }
+
+ return this.each(function() {
+ var $originalThead = $(this);
+ var $table = $originalThead.parent('table');
+ var $cloneThead = $originalThead.clone().addClass('js-fixed-thead__clone');
+
+ $cloneThead.insertAfter($originalThead);
+ $cloneThead.css('display', 'none');
+
+ calculateCloneDimensions($originalThead, $cloneThead);
+ showOrHideClone($table, $originalThead, $cloneThead);
+
+ $(window).resize(function(){
+ calculateCloneDimensions($originalThead, $cloneThead);
+ showOrHideClone($table, $originalThead, $cloneThead);
+ });
+
+ $(window).scroll(function(){
+ showOrHideClone($table, $originalThead, $cloneThead);
+ });
+ });
+
+ };
+
+}(jQuery));