写了个插件,用来固定表的头部和尾部。

/*!
* smartFloat v1.0.1
* Copyright 2019- Richard
* Licensed under MIT
*/
$.fn.extend({
smartFloat: function (_newpos = {
top: 0
}, _stopElement = undefined) {
function position(element) {
var csstop = element.offset().top;
var csspos = element.css("position");
var csswidth = element.width();
$(window).scroll(function () {
var scroll = $(this).scrollTop(); //滚动条所在位置
if ((scroll > csstop //如果滚动到页面超出了当前元素element的相对页面顶部的高度
||
$(this).height() < csstop) //如果元素超过窗口高度
) {
element.css({
position: "fixed",
'z-index': 999,
width: csswidth
}).css(_newpos); //如果碰到了停止浮动的元素
if (_stopElement) {
_stopElement = $(_stopElement)
var secsstop = _stopElement.offset().top;
try {
if (secsstop >= scroll && secsstop < (scroll + $(window).height())) { //元素在可视区域
restoreCss();
}
} catch (error) {
console.log(error);
}
} //调整表头每个th的宽度,使其和td宽度一致
if (element.parent()[0].tagName.toLowerCase() == 'table' && !element.data('widthIsSeted')) {
var arrChildWidth = Array();
element.next().find('tr').eq(0).children().each(function (i) {
arrChildWidth[i] = $(this).width(); //保存数据行每个td的宽度
});
element.find('th').each(function (i, th) {
$(th).width(arrChildWidth[i]); //设置每个th的宽度和td一致
});
console.log('调整表头每个th的宽度');
element.data('widthIsSeted', true);
}
} else {
restoreCss();
}
}); function restoreCss() {
element.css('position', csspos);
}
};
return $(this).each(function () {
position($(this));
});
}
});

调用方式:

    $(".card-close").smartFloat({
'top': '10px',
'right': '50px',
'z-index': 999
});

$("thead").smartFloat({
'top': 0,
'z-index': 990
}); //浮动表头

$("#tablefooter").smartFloat({
bottom: 0
}, "footer"); //浮动功能区,footer可见时取消浮动

最新文章

  1. codevs2777 栅栏的木料
  2. 初学者在ubuntu下安装使用git(下)
  3. 移动POS机
  4. Winpcap
  5. Winfrom 基于TCP的Socket 编程
  6. XE5 Android 开发数据访问手机端 解决乱码的办法
  7. 安装Numpy和matplotlib
  8. RAC,客户端连接失败ORA-12514
  9. Python3 字符编码
  10. YUV数据格式
  11. ERP售前顾问形象寓意
  12. (转) C# Activator.CreateInstance()方法使用
  13. 添加PDF文件对照度的粗浅原理,及方法
  14. (89c51)16x16点阵屏幕的实现
  15. [UWP]了解模板化控件(1):基础知识
  16. 中高级JavaScript易错面试题
  17. 盘点CSS中可以和不可以继承的属性
  18. C#之冒泡排序
  19. 【HDU - 4349】Xiao Ming&#39;s Hope
  20. Javascript 高级程序设计--总结【二】

热门文章

  1. JS对象创建的几种方式整理
  2. Ethernaut靶场练习(0-5)
  3. 2.1获取Git仓库-2.2记录每次更新到仓库
  4. 腾讯云--cdn静态内容上传刷新
  5. Android测试三件套:传文件、抓包、看日志
  6. CentOS6.x 安装 nginx-1.19.4
  7. layuiu按钮
  8. 今天的一个SQL题-case语句和decode函数
  9. ERP的协议管理的操作与设计--开源软件诞生27
  10. 云原生haproxy 代理-ebpf