js 判断滚动条的高度位置

古龙老子 2012-06-20 02:42:06
//这个是根据滚动条分批加载方法,滚动条拉到底部触发
window.onscroll = function () {
var winScroll = document.documentElement.scrollTop || document.body.scrollTop;
if (winScroll == (document.body.offsetHeight - document.documentElement.clientHeight)) {
alter("触发事件了");
}
}

现在的效果是 ,滚动条必须拉到底部才触发。
我想要的效果是 离底部还有 100 px 的时候触发,就是离底部还有一点距离触发
...全文
4083 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
明友乐悠悠 2014-10-14
  • 打赏
  • 举报
回复
?都能实现距离底部的效果了,为什么还纠结这个问题,你把距离底部+X px的地方当新的底部不就好了吗,本质都是页面的一个位置
var winScroll = Math.round(document.documentElement.scrollTop) || Math.round(document.body.scrollTop);
        var loadFlag = Math.round(document.body.offsetHeight - (1.2 * (document.documentElement.clientHeight)));
        if (winScroll >= loadFlag && isLoading == false) {}
ltaord_qiyun 2014-02-18
  • 打赏
  • 举报
回复
请问楼主 我想要的效果是 离底部还有 100 px 的时候触发,就是离底部还有一点距离触发 怎么实现的呢?
  • 打赏
  • 举报
回复
<script type="text/javascript" > var height=0; var scr = 0 $(document).ready(function(e) { height = $("div#container").height(); }); $(window).scroll( function() { src = $(window).scrollTop(); if(src>(height-src-140)){ /*---here your fun----*/ } } ); </script> 据底部140px时 触发/*---here your fun----*/
古龙老子 2012-06-21
  • 打赏
  • 举报
回复
等到花儿都谢了
古龙老子 2012-06-20
  • 打赏
  • 举报
回复
救命呀,来人...
古龙老子 2012-06-20
  • 打赏
  • 举报
回复
继续等待...
Lee165 2012-06-20
  • 打赏
  • 举报
回复
<a id="gotopbtn" style="position:fixed;width:12px;bottom:95px;right:195px !important;display:none;cursor:pointer; _position: absolute;_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight - this.offsetHeight)); _background:none; display:none;)">返回顶部</a>
<script type="text/javascript">goTopEx();</script>
<script type="text/javascript">
function goTopEx() {
var obj = document.getElementById("gotopbtn");
function getScrollTop() {
return document.documentElement.scrollTop || document.body.scrollTop;
}
function setScrollTop(value) {
if (document.documentElement.scrollTop) {
document.documentElement.scrollTop = value;
} else {
document.body.scrollTop = value;
}
}
obj.onclick = function () {
var goTop = setInterval(scrollMove, 1);
function scrollMove() {
setScrollTop(getScrollTop() / 1.1);
if (getScrollTop() < 1) clearInterval(goTop);
}
}
//让返回顶部标签,始终停在footer 之前!
var btmHeight = (-[1, ]) ? 340 : 350;
window.onscroll = function () {
getScrollTop() > 0 ? obj.style.display = "" : obj.style.display = "none";
obj.style.bottom = 85 + "px";
if (getDisFromBottom() <= btmHeight) {
obj.style.bottom = getDisFromBottom() + "px";
obj.style.top = "auto";
}
}
function getDisFromBottom() {
var dis = getScrollTop() + getClientHeight() + btmHeight - getScrollHeight();
return dis > 85 ? dis : 85;
}
function getScrollHeight() {
return getDocEle().scrollHeight;
}
function getClientHeight() {
return getDocEle().clientHeight;
}
function getDocEle() {
return document.compatMode == 'CSS1Compat' ? document.documentElement : document.body;
}
}
</script>


看看这段代码 能帮助你么
古龙老子 2012-06-20
  • 打赏
  • 举报
回复
求救...
古龙老子 2012-06-20
  • 打赏
  • 举报
回复
(document.body。scrollTop + 100) 跟 document.documentElement.clientHeight

是 100 跟 699
fox123871 2012-06-20
  • 打赏
  • 举报
回复
(document.body。scrollTop + 100) 跟 document.documentElement.clientHeight 比较下
fox123871 2012-06-20
  • 打赏
  • 举报
回复
比如你的滚动条在1个div里
那么就是 document.getElement("divID").offsetWidth 这个是获取你目测 这个滚动条的一个高度

如果你想获取 垂直滚动条的 滚动的距离

document.getElement("divID").scrollTop 来获取!
古龙老子 2012-06-20
  • 打赏
  • 举报
回复
拜托了,在线等...

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧