我找到了一些很好的方法来检查滚动条使用jquery的位置,但我想知道你是否可以区分用户是否向上或向下滚动?
答案 0 :(得分:18)
检查最后一个状态。像这样:
保留一个变量,例如last_scroll_position
,如果有滚动,如果last_scroll_position - current_position > 0
,用户向上滚动,如果小于0则向下滚动。
答案 1 :(得分:14)
<script type='text/javascript'>
$(function(){
var CurrentScroll = 0;
$(window).scroll(function(event){
var NextScroll = $(this).scrollTop();
if (NextScroll > CurrentScroll){
//write the codes related to down-ward scrolling here
}
else {
//write the codes related to upward-scrolling here
}
CurrentScroll = NextScroll; //Updates current scroll position
});
});
答案 2 :(得分:9)
要区分jQuery中的向上/向下滚动,您可以使用:
var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
$('#yourDiv').bind(mousewheelevt, function(e){
var evt = window.event || e //equalize event object
evt = evt.originalEvent ? evt.originalEvent : evt; //convert to originalEvent if possible
var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF
if(delta > 0) {
//scroll up
}
else{
//scroll down
}
});
此方法也适用于具有overflow:hidden
。
我在FireFox,IE和Chrome中成功测试了它。
答案 3 :(得分:4)
scroll event 在FF中表现得很奇怪(由于平滑滚动,它很多次被触发)但是它有效。
注意:使用光标键或鼠标滚轮拖动滚动条时,scroll事件实际被触发。
//creates an element to print the scroll position
$("<p id='test'>").appendTo("body").css({
padding: "5px 7px",
background: "#e9e9e9",
position: "fixed",
bottom: "15px",
left: "35px"
});
//binds the "scroll" event
$(window).scroll(function (e) {
var target = e.currentTarget,
$self = $(target),
scrollTop = window.pageYOffset || target.scrollTop,
lastScrollTop = $self.data("lastScrollTop") || 0,
scrollHeight = target.scrollHeight || document.body.scrollHeight,
scrollText = "";
if (scrollTop > lastScrollTop) {
scrollText = "<b>scroll down</b>";
} else {
scrollText = "<b>scroll up</b>";
}
$("#test").html(scrollText +
"<br>innerHeight: " + $self.innerHeight() +
"<br>scrollHeight: " + scrollHeight +
"<br>scrollTop: " + scrollTop +
"<br>lastScrollTop: " + lastScrollTop);
if (scrollHeight - scrollTop === $self.innerHeight()) {
console.log("► End of scroll");
}
//saves the current scrollTop
$self.data("lastScrollTop", scrollTop);
});
您还可以查看MDN,它会显示有关 Wheel Event的详细信息。
注意:仅在使用鼠标滚轮时才会触发滚轮事件 ;光标键并拖动滚动条不会触发事件。
我阅读了文档和示例: Listening to this event across browser
经过FF,IE,Chrome,safari的一些测试后,我最终得到了这个片段:
//creates an element to print the scroll position
$("<p id='test'>").appendTo("body").css({
padding: "5px 7px",
background: "#e9e9e9",
position: "fixed",
bottom: "15px",
left: "15px"
});
//attach the "wheel" event if it is supported, otherwise "mousewheel" event is used
$("html").on(("onwheel" in document.createElement("div") ? "wheel" : "mousewheel"), function (e) {
var evt = e.originalEvent || e;
//this is what really matters
var deltaY = evt.deltaY || (-1 / 40 * evt.wheelDelta), //wheel || mousewheel
scrollTop = $(this).scrollTop() || $("body").scrollTop(), //fix safari
scrollText = "";
if (deltaY > 0) {
scrollText = "<b>scroll down</b>";
} else {
scrollText = "<b>scroll up</b>";
}
//console.log("Event: ", evt);
$("#test").html(scrollText +
"<br>clientHeight: " + this.clientHeight +
"<br>scrollHeight: " + this.scrollHeight +
"<br>scrollTop: " + scrollTop +
"<br>deltaY: " + deltaY);
});
答案 4 :(得分:3)
vanilla javascript示例:
var f = (function(){
var oldPos = window.scrollY;
function fu(e) {
var newPos = window.scrollY;
if (newPos>oldPos) {
console.log('down');
} else if(newPos<oldPos) {
console.log('up');
} else {
console.log('same');
}
oldPos = newPos;
}
return fu;
})();
window.addEventListener('scroll',f);
答案 5 :(得分:2)
有一种简单的方法可以在没有任何类型的全局变量的情况下完成。 MouseWheel事件具有明确指定滚动方向的属性。以下是如何使用它:
$("#scroll_div").bind("mousewheel",function(ev) {
var curScrollLeft = $(this).scrollLeft();
$(this).scrollLeft(curScrollLeft + Math.round(ev.originalEvent.wheelDelta));
});