如何检查滚动条是否在底部 [英] How to check if scrollbar is at the bottom

查看:99
本文介绍了如何检查滚动条是否在底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查滚动条是否位于底部?使用JQuery或JavaScript

How can you check if the scroll bar is positioned at the bottom? Using JQuery or JavaScript

推荐答案

您可以找到滚动容器的高度,然后将其与滚动位置进行比较。如果它们是相同的,那么你已经到了底部。

You find the height of the scrolling container, then compare that to the scroll position. If they are the same, then you have reached the bottom.

<div style="overflow: auto; height: 500px">
</div>

$(document).ready(function()
{
   $('div').scroll(function()
   {
      var div = $(this);
      if (div.height() == div.scrollTop() + 1) //scrollTop is 0 based
      {
          alert('Reached the bottom!");
      }
   });
});

编辑:在js小提琴中进行一点测试,我意识到以前的版本是不正确的。
你可以使用DOM属性来找出有多少滚动执行一点数学与元素的高度如此

a little testing in a js fiddle and I realized the previous version is incorrect. You can use a DOM property to find out how much scrolling there is a perform a little math with the height of the element like so

      var div = $(this);
      if (div[0].scrollHeight - div.scrollTop() == div.height())
      {
          alert('Reached the bottom!');
      }

http://jsfiddle.net/Aet2x/1/

这篇关于如何检查滚动条是否在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆