From 146697e6d8f08d9ba1ced9f881af2ac9e8cf09a7 Mon Sep 17 00:00:00 2001 From: zxy1994 <13510460187@163.com> Date: Mon, 17 Jul 2017 16:43:10 +0800 Subject: [PATCH 1/3] To solve sometime '$' invalid --- jquery-fixed-table-header.js | 46 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/jquery-fixed-table-header.js b/jquery-fixed-table-header.js index a1e7a00..437e06e 100644 --- a/jquery-fixed-table-header.js +++ b/jquery-fixed-table-header.js @@ -1,22 +1,24 @@ -$.fn.fixedTableHeader = function () { - var t = $(this); - - var tableOffset = t.offset().top; - var fixed_table = $('
').css({ 'display':'none', 'position':'fixed', 'top':'0px', 'background-color':'white' }); - t.parent().append(fixed_table.append(t.find("thead").clone())); - - fixed_table.find("th").each(function (i) { - $(this).width(t.find("th").eq(i).width() + 1); - }); - - $(window).bind("scroll", function () { - var offset = $(this).scrollTop(); - if (offset >= tableOffset && fixed_table.is(":hidden")) { - fixed_table.show(); - } - else if (offset < tableOffset) { - fixed_table.hide(); - } - }); - return t; -} \ No newline at end of file +(function($){ + $.fn.fixedTableHeader = function () { + var t = $(this); + + var tableOffset = t.offset().top; + var fixed_table = $('
').css({ 'display':'none', 'position':'fixed', 'top':'0px', 'background-color':'white' }); + t.parent().append(fixed_table.append(t.find("thead").clone())); + + fixed_table.find("th").each(function (i) { + $(this).width(t.find("th").eq(i).width() + 1); + }); + + $(window).bind("scroll", function () { + var offset = $(this).scrollTop(); + if (offset >= tableOffset && fixed_table.is(":hidden")) { + fixed_table.show(); + } + else if (offset < tableOffset) { + fixed_table.hide(); + } + }); + return t; + } +}(jQuery)); \ No newline at end of file From fc80df31e78f17cebbf5a85b5d8419a4daf5ac9e Mon Sep 17 00:00:00 2001 From: zxy1994 <13510460187@163.com> Date: Tue, 18 Jul 2017 15:48:29 +0800 Subject: [PATCH 2/3] update --- jquery-fixed-table-header.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/jquery-fixed-table-header.js b/jquery-fixed-table-header.js index 437e06e..7b5efde 100644 --- a/jquery-fixed-table-header.js +++ b/jquery-fixed-table-header.js @@ -1,13 +1,23 @@ (function($){ $.fn.fixedTableHeader = function () { var t = $(this); - var tableOffset = t.offset().top; var fixed_table = $('
').css({ 'display':'none', 'position':'fixed', 'top':'0px', 'background-color':'white' }); t.parent().append(fixed_table.append(t.find("thead").clone())); - + + fixed_table.attr("border",t.attr("border")); + fixed_table.css("border-collapse",t.css("border-collapse")); + fixed_table.attr("cellspacing",t.attr("cellspacing")); + + fixed_table.width(t.outerWidth()); + + if(t.css("border-collapse")=="collapse"){ + var borderWith = parseInt(t.css("border")); + }else{ + var borderWith = 0; + } fixed_table.find("th").each(function (i) { - $(this).width(t.find("th").eq(i).width() + 1); + $(this).width(t.find("th").eq(i).width() + borderWith); }); $(window).bind("scroll", function () { @@ -21,4 +31,4 @@ }); return t; } -}(jQuery)); \ No newline at end of file +}(jQuery)); From df799031cce48908a7fb078e12969066cd232810 Mon Sep 17 00:00:00 2001 From: zxy1994 <13510460187@163.com> Date: Fri, 21 Jul 2017 16:05:27 +0800 Subject: [PATCH 3/3] update --- jquery-fixed-table-header.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/jquery-fixed-table-header.js b/jquery-fixed-table-header.js index 7b5efde..b540649 100644 --- a/jquery-fixed-table-header.js +++ b/jquery-fixed-table-header.js @@ -1,23 +1,33 @@ (function($){ + /** + * 表头悬浮插件 + * @date 2017-07-18 13:17:54 + */ $.fn.fixedTableHeader = function () { var t = $(this); var tableOffset = t.offset().top; + // 创建一个表格,设置样式 var fixed_table = $('
').css({ 'display':'none', 'position':'fixed', 'top':'0px', 'background-color':'white' }); + // 把标题的标题clone过来 t.parent().append(fixed_table.append(t.find("thead").clone())); + // 设置表格属性和样式 fixed_table.attr("border",t.attr("border")); fixed_table.css("border-collapse",t.css("border-collapse")); fixed_table.attr("cellspacing",t.attr("cellspacing")); - + // 设置表格的宽度 fixed_table.width(t.outerWidth()); + // 用于判断宽度该加多少 if(t.css("border-collapse")=="collapse"){ - var borderWith = parseInt(t.css("border")); + var borderWith = parseInt(t.attr("border")); }else{ var borderWith = 0; } + // 设置表格悬浮标题单元格的宽度 fixed_table.find("th").each(function (i) { - $(this).width(t.find("th").eq(i).width() + borderWith); + var width = t.find("th").eq(i).css("width"); + $(this).width(parseInt(width)+borderWith+"px"); }); $(window).bind("scroll", function () { @@ -32,3 +42,4 @@ return t; } }(jQuery)); +