当滚动到顶部而不是底部时添加元素
Add element when scroll to top instead of bottom
这一定是一些愚蠢的错误,但在我的javascript函数中,当我到达屏幕底部时应该执行的if
语句,当我达到屏幕顶部时正在执行。。。这是我的代码:
$(window).scroll(function() { //detect page scroll
if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page?
{
$('.animation_image').show(); //show loading image
}
});
.animation_image
是一个加载图像,当我到达屏幕底部时会出现,但当我向下滚动然后返回屏幕顶部时会出现。。。
如果有人能解释我做错了什么,那就太好了!
它应该能正常工作。尽管只是出于好奇,但此代码从未隐藏图像。难道它不应该在其他地方藏起来吗?给你,Fiddlehttps://jsfiddle.net/3eg18L8u/
$(window).scroll(function() { //detect page scroll
if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page?
{
$('.animation_image').show(); //show loading image
}
else {
$('.animation_image').hide(); //show loading image
}
});
您尝试过>=而不是==吗?
if ($(window).scrollTop() + $(window).height() >= $(document).height())
{
$('.animation_image').show(); //show loading image
}
怎么样:
$(document).ready(function(){
$(window).scroll(function(e){
e.stopPropagation();
e.preventDefault();
var scrollBottom = $(document).height() - $(window).scrollTop() - $(window).height();
if (scrollBottom == 0) {
$('.animation-image').show();
}
});
});
我似乎不明白为什么即使有你们的帮助,它也不起作用,所以我放弃了,决定使用这个代码,它不是解决方案,而是一个替代方案:
function yHandler(){
var wrap = document.getElementById('wrap');
var contentHeight = wrap.offsetHeight;
var yOffset = window.pageYOffset;
var y = yOffset + window.innerHeight;
if(y >= contentHeight){
$('.animation_image').show();
}
var status = document.getElementById('status');
status.innerHTML = contentHeight+" | "+y;
}window.onscroll = yHandler
html内容结构如下:
<body>
<div id="status">0 | 0</div>
<div id="wrap">
*ALL CONTENT*
</div>
</body>
和CSS:
div#status{position:fixed; font-size:24px;}
相关文章:
- 添加1时页面,滚动到底部
- 当在堆栈底部添加ImageView时,我想删除RelativeLayout中的顶部ImageView,但它正在删除整个布
- 如何在跨度中添加底部填充
- 通过将$resource查询数组添加到AngularJS中另一个数组的底部来进行分页加载
- 在靠近页面底部时添加/删除类
- 如何将现有 ul 的所有 li 添加到另一个 ul 的底部,基于他们的 css 类
- 添加文章标签时,底部的页脚
- 由于固定导航,在扇形框的顶部和底部添加边距
- 添加新消息后滚动到底部
- 添加层移动到底部阶段在动能js
- jQuery UI可排序-将项添加到底部
- 当滚动到顶部而不是底部时添加元素
- 如何在图像底部添加带有“x”按钮的横幅以在 React/HTML 中关闭它
- 在我的网页底部添加弹跳滚动箭头
- 如何使用jQuery在添加文本后删除文本区域底部的空换行符?
- Android WebView在底部添加了奇怪的空白空间
- 不能在Fancybox的底部添加20px的内边距
- jQuery向上滑动一行,并在底部添加另一行
- chrome扩展如何在页面底部添加浮动栏
- 如何在使用jquery的同一表中加载远程数据后,在表的底部添加一行