Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    81
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Script to keep table header in view while scrolling

    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

    Code:
    /*************************************************************************
    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

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    getComputedStyle is not supported in IE < 9. You could incorporate:

    Code:
            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 '';
                }
            };
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •