buli
2023-06-27 8e07b5f23afd4c93d7e8d1f04d2e8dbca2628e5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * jQuery TableFix plugin ver 1.0.1
 * Copyright (c) 2010 Otchy
 * This source file is subject to the MIT license.
 * http://www.otchy.net/javascript/tablefix/
 */
(function($){
    $.fn.tablefix = function(options) {
        var sbwidth = scrollbarWidth();
        return this.each(function(index){
            // 処理継続の判定
            var opts = $.extend({}, options);
            var baseTable = $(this);
            var withWidth = (opts.width > 0);
            var withHeight = (opts.height > 0);
            if (withWidth) {
                withWidth = (opts.width < baseTable.width());
            } else {
                opts.width = baseTable.width();
            }
            if (withHeight) {
                withHeight = (opts.height < baseTable.height());
            } else {
                opts.height = baseTable.height();
            }
            if (withWidth || withHeight) {
                if (withWidth) {
                    opts.height -= sbwidth;
                }
                if (withHeight) {
                    opts.width -= sbwidth;
                }
            } else {
                return;
            }
            // 外部 div の設定
            baseTable.wrap("<div></div>");
            var div = baseTable.parent();
            div.css({position: "relative"});
            // スクロール部オフセットの取得、tdにwidthとheightを固定
            var fixRows = (opts.fixRows > 0) ? opts.fixRows : 0;
            var fixCols = (opts.fixCols > 0) ? opts.fixCols : 0;
            var offsetX = 0;
            var offsetY = 0;
            baseTable.find('tr').each(function(indexY) {
                var trThis = $(this);
                if(jQuery.browser.msie) {
                    trThis.height(trThis.height() + 1);
                } else {
                    trThis.height(trThis.height());
                }
                trThis.find('td,th').each(function(indexX) {
                    // 先頭の行
                    if (indexY <= fixRows) {
                        var cell = $(this);
                        if (indexY == fixRows && indexX == fixCols) {
                            offsetX = cell.position().left;
                            offsetY = cell.parent('tr').position().top;
                        }
                        cell.width(cell.width());
                        cell.height(cell.height());
                    }
                    // 先頭の列
                    else if (indexX <= fixCols) {
                        var cell = $(this);
                        cell.width(cell.width());
                        cell.height(cell.height());
                    }
                    else {
                        return false;
                    }
                });
            });
            // テーブルの分割と初期化
            var bodyTable = baseTable.wrap('<div name="bodyTable"></div>');
            var rowTable = baseTable.shallowClone().removeAttr('id').wrap('<div name="rowTable"></div>');
            var colTable = baseTable.shallowClone().removeAttr('id').wrap('<div name="colTable"></div>');
            var crossTable = baseTable.shallowClone().removeAttr('id').wrap('<div name="crossTable"></div>');
            baseTable.find('tr').each(function(indexY) {
                var trThis = $(this);
                var trRow, trCol, trCross;
                if (indexY < fixRows) {
                    trRow = trThis.shallowClone().removeAttr('id').appendTo(rowTable);
                    trCross = trThis.shallowClone().removeAttr('id').appendTo(crossTable);
                }
                trThis.find('td,th').each(function(indexX) {
                    var cell = $(this);
                    // 先頭の行
                    if (indexY < fixRows) {
                        // 同時に先頭の列
                        if (indexX < fixCols) {
                            trCross.append(cell);
                        }
                        // 単なる先頭の行
                        else {
                            trRow.append(cell);
                        }
                    }
                    // 先頭の列
                    else if (indexX < fixCols) {
                        if (!trCol) {
                            trCol = trThis.shallowClone().removeAttr('id').appendTo(colTable);
                        }
                        trCol.append(cell);
                    }
                    else {
                        return false;
                    }
                });
                if (indexY < fixRows) {
                    trThis.remove();
                }
            });
            var crossDiv = crossTable.parent().css({position: "absolute", overflow: "hidden"});
            var rowDiv = rowTable.parent().css({position: "absolute", overflow: "hidden"});
            var colDiv = colTable.parent().css({position: "absolute", overflow: "hidden"});
            var bodyDiv = bodyTable.parent().css({position: "absolute", overflow: "auto"});
            // クリップ領域の設定
            var bodyWidth = opts.width - offsetX;
            var bodyHeight = opts.height - offsetY;
            crossDiv
                .width(offsetX)
                .height(offsetY);
            crossTable.width(offsetX);
            rowDiv
                .width(bodyWidth)
                .height(offsetY)
                .css({left: offsetX + 'px'});
            colDiv
                .width(offsetX)
                .height(bodyHeight)
                .css({top: offsetY + 'px'});
            colTable.width(offsetX);
            bodyDiv
                .width(bodyWidth + (withHeight ? sbwidth : 0))
                .height(bodyHeight + (withWidth ? sbwidth : 0))
                .css({left: offsetX + 'px', top: offsetY + 'px'});
            div.append(rowDiv).append(colDiv).append(crossDiv);
            // スクロール連動
            bodyDiv.scroll(function() {
                rowDiv.scrollLeft(bodyDiv.scrollLeft());
                colDiv.scrollTop(bodyDiv.scrollTop());
            });
            rowDiv.scroll(function() {
                bodyDiv.scrollLeft(rowDiv.scrollLeft());
            });
            colDiv.scroll(function() {
                bodyDiv.scrollLeft(colDiv.scrollLeft());
            });
            // 外部 div の設定
            div
                .width(opts.width + (withHeight ? sbwidth : 0))
                .height(opts.height + (withWidth ? sbwidth : 0));
        });
    }
})(jQuery);
 
jQuery.fn.extend({
    shallowClone: function() {
        return this.map(function () {
            return jQuery.shallowClone(this);
        });
    }
});
jQuery.extend({
    shallowClone: function(elem) {
        var clone;
        clone = elem.cloneNode(false);
        return clone;
    }
});
// http://chris-spittles.co.uk/jquery-calculate-scrollbar-width/
function scrollbarWidth() {
    var $inner = jQuery('<div style="width: 100%; height:200px;">test</div>'),
        $outer = jQuery('<div style="width:200px;height:150px; position: absolute; top: 0; left: 0; visibility: hidden; overflow:hidden;"></div>').append($inner),
        inner = $inner[0],
        outer = $outer[0];
     
    jQuery('body').append(outer);
    var width1 = inner.offsetWidth;
    $outer.css('overflow', 'scroll');
    var width2 = outer.clientWidth;
    $outer.remove();
 
    return (width1 - width2);
}