jQuery实现锁定页面元素(表格列)
摘要
在拖动滚动条时,对页面元素进行横向、纵向锁定。
介绍
对于展现内容较多的页面,在滚动时,我们经常需要对一些重要的元素进行锁定。这些元素经常是表格的行、列,也可能是搜索条件,或者是其他重要信息。
对于表格列的锁定,目前主要有三种方法。
1.表格控件
2.js生成fixtable之类填充
3.js+css
第一种,算是最简单的方法。但是用控件的缺点实在太多了,其中一个与本文有直接相关的缺点是,部分控件对多级表头的锁定支持的很差。
第二种,思路很清晰,但是实现起来非常复杂,维护成本过高。
第三种,正是本文所用的方法。目前网上也有多篇相关文章,但是就使用场景来说太局限了,没有对这一类问题进行更高程度的抽象。
我想要的是:不只是表格,只要是想要锁定的元素,只需要添加一个标识,再最多额外写一行代码就可以完成批量锁定。并且内部实现代码要尽量简单。
用法
对需要纵向锁定的元素添加样式lock-col,横向锁定的添加lock-row。在nayiLock方法中传入滚动的div所对应的id。
完整例子
<!DOCTYPE>
<html>
<head>
<title>锁定</title>
<meta http-equiv="X-UA-Compatible" charset="utf-8"/>
<script src="../../js/jquery-1.7.2.min.js" type="text/javascript"></script>
<style type="text/css">
table td, th{
text-align: center;
border:#dee9ef 1px solid;
}
</style>
<script>
jQuery(function(){
nayiLock("aDiv");
//支持多级表头的锁定
nayiLock("bDiv");
//支持对多个div的锁定
})
//scrollDivId 滚动的div的id
function nayiLock(scrollDivId){
jQuery("#" + scrollDivId).scroll(function(){
var left = jQuery("#" + scrollDivId).scrollLeft();
jQuery(this).find(".lock-col").each(function(){
jQuery(this).css({"position":"relative","left":left,"background-color":"white","z-index":jQuery(this).hasClass("lock-row")?20:10});
});
var top = jQuery("#" + scrollDivId).scrollTop();
jQuery(this).find(".lock-row").each(function(){
jQuery(this).css({"position":"relative","top":top,"background-color":"white","z-index":jQuery(this).hasClass("lock-col")?20:9});
});
});
}
</script>
</head>
<body id="myBody">
<div id="aDiv" style="width:600px;height:200px;overflow:auto;">
<div class="lock-row lock-col" >
<span id="span1" >span1</span>
</div>
<table id="table1" style="width:800px;height:250px;" >
<thead>
<tr>
<th id="testTh" class="lock-col lock-row">序号</th>
<th class=" lock-row">表头1</th>
<th class="lock-row">表头2</th>
</tr>
</thead>
<tbody>
<tr>
<td class="lock-col">1</td>
<td class="">test</td>
<td>test</td>
</tr>
<tr>
<td class="lock-col">2</td>
<td class="">test</td>
<td>test</td>
</tr>
</tbody>
</table>
</div>
<div id="bDiv" style="width:600px;height:100px;overflow:auto;">
<table id="table1" style="width:800px;height:150px;">
<thead>
<tr>
<th colspan="2" class="lock-col lock-row">
colspan="2"
</th>
<th class="lock-row">
colspan="1"
</th>
</tr>
<tr>
<th id="testTh" class="lock-col lock-row">序号</th>
<th class="lock-col lock-row">表头1</th>
<th class="lock-row">表头2</th>
</tr>
</thead>
<tbody>
<tr>
<td class="lock-col">1</td>
<td class="lock-col">test</td>
<td>test</td>
</tr>
<tr>
<td class="lock-col">2</td>
<td class="lock-col">test</td>
<td>test</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>注:对低版本ie的兼容还是没找到js上的直接解决方法。建议使用expression方法。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
jQuery悬停文字提示框插件jquery.tooltipster.js用法示例【附demo源码下载】
这篇文章主要介绍了jQuery悬停文字提示框插件jquery.tooltipster.js用法,涉及jQuery文字提示框插件的引入与调用实现技巧,非常简单实用,需要的朋友可以参考下2016-07-07
jquery mobile的触控点击事件会多次触发问题的解决方法
这篇文章主要介绍了jquery mobile的触控点击事件会多次触发问题的解决方法以及替代方法,需要的朋友可以参考下2014-05-05
jQuery EasyUI API 中文文档 - Draggable 可拖拽
jQuery EasyUI API 中文文档 - Draggable 可拖拽,使用jQuery EasyUI的朋友可以参考下。2011-09-09
jQuery模拟完美实现经典FLASH导航动画效果【附demo源码下载】
这篇文章主要介绍了jQuery模拟完美实现经典FLASH导航动画效果,通过jQuery响应鼠标事件动态操作页面元素样式实现flash切换的效果,非常经典实用,文末还提供了demo源码供读者下载学习或使用,需要的朋友可以参考下2016-11-11


最新评论