这次给大家带来table固定表头使表单横向滚动,table固定表头使表单横向滚动的注意事项有哪些,下面就是实战案例,一起来看一下。
1、头部用一个table并用一个p包裹着, 表格的具体内容用一个table
2、头部外面的p用positon: relative相对定位
3、获取整个表格的高度
4、获取表格的dom(或者包裹着表格的dom)距离页面顶部的距离 offsettop
5、滚动的零界点的距离 表格的高度+表格距离页面顶部的距离 如果滚动超过这个 就让头部的top值归0或原封不动
当然还有很多可以优化的地方 我只是展示一个小思路 嘿嘿嘿
题外话 为啥用红色表头 因为显眼哇 哈哈
js代码
/**
* 最重要的一点是头和身体是两个table 然后定位用relative 然后通过滚动来计算
* */
function fixedhead (){
if( !(this instanceof fixedhead) ){
return new fixedhead()
};
this.$dom = $('.datatables_scrollhead'); // 表头外层dom
this.offsettop = this.$dom.offset().top; // 表头外层dom距离顶部的高度
this.parents = this.$dom.parents('.datatables_scroll'); // 表头外层dom最外面的盒子(包裹着table的盒子)
this.outboxheight = this.parents.height(); // 表头外层dom最外面的盒子(包裹着table的盒子)的高度
this.maxheight = this.offsettop + this.outboxheight; // 滚动的零界点 最多能滚动到哪里
this.scroll();
}
fixedhead.prototype = {
constructor: fixedhead,
scroll: function(){
var that = this;
$(window).scroll(function(){
var scrolltop = $(this).scrolltop();
if((scrolltop > that.offsettop) && (scrolltop < that.maxheight)){
that.$dom.css('top', (scrolltop - that.offsettop + 50)+'px') // 这个50是因为我的头部导航固定在顶部 高是50 所以要加上
}else {
var topcss = that.$dom.css('top');
if(topcss === '0px' || topcss === 'auto'){
}else {
that.$dom.css('top', '0px')
}
}
})
}
}
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
bootstrap下拉插件dropdown使用技巧
js里tofixed与round使用详解
angularjs做出输入框字数限制提醒
以上就是table固定表头使表单横向滚动的详细内容。