diff --git a/floating_block.js b/floating_block.js index b597651aee8de4fe503b6cc2a3f82951d726ad47..57a848f6e989a7c2af4ea78b66afa35d01c57cee 100755 --- a/floating_block.js +++ b/floating_block.js @@ -7,6 +7,9 @@ * This code is based on tableheader.js */ +// Keep track of all floating blocks. +var floating_blocks = []; + Drupal.blockFloatDoScroll = function () { if (typeof(Drupal.blockFloatOnScroll) == 'function') { Drupal.blockFloatOnScroll(); @@ -17,38 +20,26 @@ Drupal.blockFloatDoScroll = function () { * Attaches the floating_block behavior. */ Drupal.behaviors.blockFloat = function (context) { + var doResize = true; // This breaks in anything less than IE 7. Prevent it from running. if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) { return; } - - // Keep track of all cloned blocks. - var blocks = []; + if (jQuery.isFunction(context.parent)) { + context = context.parent(); + doResize = false; + } $(Drupal.settings.floating_block).each(function (i, selector) { $(selector.toString() + ':not(.blockFloat-processed)', context).each(function (j, block) { - // Clone thead so it inherits original jQuery properties. - var blockClone = $(block).clone(true).insertBefore(block).addClass('sticky-block').css({ - position: 'fixed', - top: '0px', - width: $(this).width(), - height: $(this).height(), - visibility: 'hidden' - }); - - if ($(block).attr('id').length > 0) { - $(blockClone).attr('id',$(block).attr('id') + '-clone'); - } - blockClone = $(blockClone)[0]; - blockClone.original_position = $(block).offset(); - blockClone.original_identifier = 'blockFloat-' + blocks.length; - blocks.push(blockClone); - - // Finish initialzing header positioning. - tracker(blockClone); - - //Maybe need to attach soemthing to parent div? - //$(table).addClass('sticky-table'); - $(block).addClass('blockFloat-processed ' + blockClone.original_identifier); + var blockInfo = []; + blockInfo.original_position = $(block).offset(); + blockInfo.original_css_position = $(block).css("position"); + blockInfo.original_identifier = 'blockFloat-' + floating_blocks.length; + blockInfo.viewHeight = 0; + floating_blocks.push(blockInfo); + // Initialzing block positioning. + tracker(blockInfo); + $(block).addClass('blockFloat-processed ' + blockInfo.original_identifier); }); }); @@ -56,34 +47,39 @@ Drupal.behaviors.blockFloat = function (context) { function tracker(e) { // Save positioning data. var viewHeight = document.documentElement.scrollHeight || document.body.scrollHeight; - if (e.viewHeight != viewHeight) { - e.viewHeight = viewHeight; - e.docHeight = $(document).height(); - e.vPosition = e.original_position.top; - e.hPosition = e.original_position.left; - e.vLength = e.clientHeight; - } - var current_position = $(e).offset(); - // Track horizontal positioning relative to the viewport and set visibility. - var hScroll = document.documentElement.scrollLeft || document.body.scrollLeft; - var vOffset = (document.documentElement.scrollTop || document.body.scrollTop) - e.vPosition; - - var topPosition = 0; - if (vOffset > 0) { - visState = 'visible'; - $('.' + e.original_identifier).css({visibility: 'hidden'}); - //Don't let the bottom of the block go beneath the document height - if ((current_position.top + e.vLength) > e.docHeight) { - topPosition = e.docHeight - current_position.top - e.vLength; + var block = $('.' + e.original_identifier); + if (block.length > 0) { + if (e.viewHeight != viewHeight) { + if (e.reset) { + //reset block so we can calculate new position + block.css({left: e.original_position.left + 'px', position: e.original_css_position, top: e.original_position.top + 'px'}); + e.original_position = $(block).offset(); + e.original_css_position = $(block).css("position"); + } + e.viewHeight = viewHeight; + e.docHeight = $(document).height(); + e.vPosition = e.original_position.top; + e.hPosition = e.original_position.left; + e.blockHeight = block.height(); } - } - else { - visState = 'hidden'; - $('.' + e.original_identifier).css({visibility: 'visible'}); - } - $(e).css({left: -hScroll + e.hPosition + 'px', visibility: visState, top: topPosition + 'px'}); + // Track horizontal positioning relative to the viewport and set position. + var current_position = block.offset(); + var hScroll = document.documentElement.scrollLeft || document.body.scrollLeft; + var vOffset = (document.documentElement.scrollTop || document.body.scrollTop) - e.vPosition; + if (vOffset > 0) { + //Don't let the bottom of the block go beneath the document height + var topPosition = 0; + if ((current_position.top + e.blockHeight) > e.docHeight) { + topPosition = block.docHeight - current_position.top - e.blockHeight; + } + block.css({left: -hScroll + e.hPosition + 'px', position: 'fixed', top: topPosition + 'px'}); + } + else { + block.css({left: e.original_position.left + 'px', position: e.original_css_position, top: e.original_position.top + 'px'}); + } + } } // Only attach to scrollbars once, even if Drupal.attachBehaviors is called @@ -96,7 +92,7 @@ Drupal.behaviors.blockFloat = function (context) { // Track scrolling. Drupal.blockFloatOnScroll = function() { - $(blocks).each(function () { + $(floating_blocks).each(function () { tracker(this); }); }; @@ -109,13 +105,17 @@ Drupal.behaviors.blockFloat = function (context) { return; } time = setTimeout(function () { - $('.sticky-block').each(function () { + $(floating_blocks).each(function () { this.viewHeight = 0; + this.reset = true; tracker(this); }); // Reset timer time = null; }, 250); }; - $(window).resize(resize); + + if (doResize) { + $(window).resize(resize); + } };