我希望检测指针滚动是否发生在某个元素中,然后执行某些操作。我尝试过这段代码,但它似乎不起作用:
var lastScrollTop = 0;
$(".class-name").scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
console.log("scroll down");
} else {
console.log("scroll up");
}
lastScrollTop = st;
});
你能帮我理解我做错了什么吗?
谢谢!
发布于 2020-12-28 14:27:37
编辑:
scrollTop
为您提供元素的滚动条的当前位置,而不是页面的滚动条。
因此,如果他没有滚动条,它将是0。:
或如果元素不可滚动,则此数字将为0。
对于.scroll()
也是如此,如果元素没有滚动条,则永远不会发生这种情况。
var lastScrollTop = 0;
$(".class-name").scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
console.log("scroll down");
} else {
console.log("scroll up");
}
lastScrollTop = st;
});
.class-name{
width:100px;
height:200px;
overflow:auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="class-name">hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello </div>
https://stackoverflow.com/questions/65479029
复制相似问题