看看这个小提琴 https://jsfiddle.net/wzwaj0k0/1/
我希望在滚动条到达结尾但不能
时触发警报JS
function GetScrollerEndPoint()
{
var scrollHeight = $("#Mytable").prop('scrollHeight');
var divHeight = $("#Mytable").height();
var scrollerEndPoint = scrollHeight - divHeight;
var divScrollerTop = $("#Mytable").scrollTop();
if(divScrollerTop === scrollerEndPoint)
{
alert("you reached the end");
}
}
CSS
table {
display: block;
height: 100px;
overflow-y: auto;
}
table.GeneratedTable thead {
background-color: #959595;
}
HTML
<table id="Mytable" >
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
</table>
尝试了一切。但它应该只是那个表,因为我尝试使用window.scroll函数,但是我必须通过ajax向这个表添加行。所以当只有表滚动条到达结尾时需要转到该函数
答案 0 :(得分:2)
jQuery有.scroll
个事件可以添加到你的表中。
var maxScrollHeight = $('#Mytable tbody').height() + $('#Mytable thead').height();
var scrollFromTop = Math.ceil($('#Mytable').scrollTop()) + $('#Mytable').height();
使用Math.ceil()
,因为Stackoverflow上的scrollTop()返回的值略低于正常值。
(jsfiddle正常工作)https://jsfiddle.net/dalinhuang/5d219rmh/
$(document).ready(function() {
$('#Mytable').scroll(function() {
var maxScrollHeight = $('#Mytable tbody').height() + $('#Mytable thead').height();
var scrollFromTop = Math.ceil($('#Mytable').scrollTop()) + $('#Mytable').height();
if (scrollFromTop == maxScrollHeight) {
alert("you reached the end");
}
});
});
&#13;
table {
display: block;
height: 100px;
overflow-y: auto;
}
table.GeneratedTable thead {
background-color: #959595;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="Mytable">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
</table>
&#13;
答案 1 :(得分:1)
问题是你永远不会打电话给 GetScrollerEndPoint 。一种选择是在表格中添加 onscroll 事件。
<table id="Mytable" onscroll="GetScrollerEndPoint()">
...
</table>
即使您向表中添加新行,这也应该有效。只有在滚动表格时才会调用 GetScrollerEndPoint 。
这里有一个例子。
function GetScrollerEndPoint()
{
var scrollHeight = $("#Mytable").prop('scrollHeight');
var divHeight = $("#Mytable").height();
var scrollerEndPoint = scrollHeight - divHeight;
var divScrollerTop = $("#Mytable").scrollTop();
if(divScrollerTop === scrollerEndPoint)
{
alert("you reached the end");
}
}
&#13;
table {
display: block;
height: 100px;
overflow-y: auto;
}
table.GeneratedTable thead {
background-color: #959595;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="Mytable" onscroll="GetScrollerEndPoint()">
<thead>
<tr>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr> <tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</tbody>
</table>
&#13;