...

View Full Version : Script to keep table header in view while scrolling



hgs
01-02-2013, 08:59 PM
Hi

I have reworked and shrinked my old script down to 160 lines of commented JavaScript code. Below you will find the script .

A live demo can be found here http://hgsweb.de/floater



/*************************************************************************
float.js 1.0 Copyright (c) 2013 Heinrich Schweitzer
Contact me at hgs@hgsweb.de
This copyright notice MUST stay intact for use.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***************************************************************************/

/*//////////////////////////////////////////////////////////////
Creates divs to mimic the table header.

This header is allways displayed at the top of the table, if
the original header is scrolled out of view.

To use this funtion your table must have an id specified.
Once alle tables are rendered you call for every table: floatHeader(tableId).

For examples look at the http://hgsweb.de/floater

/////////////////////////////////////////////////////////////*/
'use strict';
function floatHeader(tableId){

var obj = document.getElementById(tableId);
var mytable = obj;
var floatoffsetleft = 0;
var startX = mytable.offsetLeft + floatoffsetleft;
var py, i,nc,nr,th, offsettop, top,height;
var k,tf,add,theCell,allHeight=0;

while (obj.offsetParent.tagName !== 'BODY') {
floatoffsetleft += obj.offsetParent.offsetLeft;
obj = obj.offsetParent;
}
obj = mytable;

tf = document.createElement('div');
tf.style.zIndex=10000;
tf.id = 'float' + mytable.id;
tf.className = 'outerFloatHead';
tf.style.width = mytable.clientWidth + 'px';
tf.style.position = 'absolute';
tf.style.left=floatoffsetleft+'px';
tf.style.height = mytable.rows[0].cells[0].clientHeight + 'px';

nr = mytable.rows.length;
top=0;
for(k=0; k<nr ;k++){
if(mytable.rows[k].cells[0].tagName!=='TH'){
break;
}
allHeight+=mytable.rows[k].cells[0].clientHeight;

nc = mytable.rows[k].cells.length;
for (i = 0; i < nc; i++) { // copy content of header cells from table
th = document.createElement('div');

theCell=mytable.rows[k].cells[i];

th.innerHTML = theCell.innerHTML;
th.cellIndex = i; // fake it
th.className='floatHead';
th.style.position = 'absolute';

add =window.getComputedStyle(theCell, null).getPropertyValue('padding-left').split('px')[0]-0;
add+=window.getComputedStyle(theCell, null).getPropertyValue('margin-left').split('px')[0]-0;
add+=window.getComputedStyle(theCell, null).getPropertyValue('border-left').split('px')[0]-0;

th.style.left=theCell.offsetLeft+add+'px';

add =window.getComputedStyle(theCell, null).getPropertyValue('padding-top').split('px')[0]-0;
add+=window.getComputedStyle(theCell, null).getPropertyValue('margin-top').split('px')[0]-0;
add+=window.getComputedStyle(theCell, null).getPropertyValue('border-top').split('px')[0]-0;

th.style.top=theCell.offsetTop+add+'px';
th.style.top=top+add+'px';
th.style.width = theCell.clientWidth + 'px';
tf.appendChild(th);
}
top+=mytable.rows[k].cells[0].clientHeight+add;
}

tf.style.height = allHeight + 'px';
document.body.appendChild(tf);


obj = mytable;
offsettop = obj.offsetTop;
while (obj.offsetParent.tagName !== 'BODY') {
obj = obj.offsetParent;
offsettop += obj.offsetTop;
}
height = mytable.clientHeight;
tf.ybottom = offsettop + height - tf.clientHeight;
tf.tabtop = offsettop;
tf.x = startX;
py = 0;
tf.y = py + offsettop;
tf.py = py;


addEvent(window, 'scroll',function(){
var y;
y = window.pageYOffset;
if (y === 0) {
if(tf.style.display!=='none'){
tf.style.display = 'none';
}
return;
} else {
if (y > tf.ybottom) {
return;
}
if (tf.tabtop - y < 0) {
if (tf.y !== y + 'px') {
tf.style.top = y + 'px';
tf.y = y + 'px'
if(tf.style.display==='none'){
tf.style.display = '';
}
}
return;
} else {
if(tf.style.display!=='none'){
tf.style.display = 'none';
}
return;
}
}
}
);
function addEvent(obj, ev, fu) {
if (obj.addEventListener) {
obj.addEventListener(ev, fu, false);
} else {
var eev = 'on' + ev;
obj.attachEvent(eev, fu);
}
}
}


Regards
Heinz

AndrewGSW
01-02-2013, 11:18 PM
getComputedStyle is not supported in IE < 9. You could incorporate:


var getStyle = function (elem, strRule) {
// robertnyman.com e.g. "font-size"
// Note: IE is 'elem.styleFloat' FF is 'elem.cssFloat'
// Also FF returns pixels for font-size in em, IE returns the 'em' value.

elem = (typeof elem === 'string') ? document.getElementById(elem) : elem;
if (document.defaultView && document.defaultView.getComputedStyle ) {
return document.defaultView.getComputedStyle(elem, '').getPropertyValue(strRule);
} else if ( elem.currentStyle ) {
strRule = strRule.replace(/\-(\w)/g, function (strMatch, p1) {
return p1.toUpperCase();
});
return elem.currentStyle[strRule];
} else {
return '';
}
};



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum