如何确定用户在可滚动容器中是向上滚动还是向下滚动?
jQuery 是否提供任何机制?
CSS:
#container {
display: block;
width: 250px;
height: 25px;
background: green;
}
#scrolling {
width: 250px;
height: 300px;
backround: red;
overflow: auto;
}
非常感谢!
拍
如何确定用户在可滚动容器中是向上滚动还是向下滚动?
jQuery 是否提供任何机制?
CSS:
#container {
display: block;
width: 250px;
height: 25px;
background: green;
}
#scrolling {
width: 250px;
height: 300px;
backround: red;
overflow: auto;
}
非常感谢!
拍
$('#somediv').scrollTop()
会告诉你从上到下的位置
-编辑-
$('#somediv').scroll(function(){
if($('#somediv').scrollTop()==0){
console.log('top')
}
})
是的,您可以访问可滚动容器的当前滚动顶部值。
$('#scrolling').scroll(function() {
var scrollTop = $(this).scrollTop();
console.log(scrollTop);
});
可以通过比较div的高度、可滚动高度和滚动偏移量来测试滚动位置:
$(document).ready(function(){
$('#scrolling').scroll(function(){
var scrollTop = $(this).scrollTop();
var iheight = $(this).innerHeight();
var sheight = this.scrollHeight;
var text = '';
if (scrollTop <= 5) {
text = 'top';
}
else if (scrollTop+5 >= sheight-iheight) {
text = 'bottom';
}
else {
text = scrollTop+' middle ('+iheight+','+sheight+')';
}
$('#result').text(text);
});
});
fiddle
这个例子从 div 的边距的顶部和底部保留了 5 个像素。