How can I determine the direction of a jQuery scroll event?
up vote
312
down vote
favorite
I'm looking for something to this effect:
$(window).scroll(function(event){
if (/* magic code*/ ){
// upscroll code
} else {
// downscroll code
}
});
Any ideas?
javascript jquery
add a comment |
up vote
312
down vote
favorite
I'm looking for something to this effect:
$(window).scroll(function(event){
if (/* magic code*/ ){
// upscroll code
} else {
// downscroll code
}
});
Any ideas?
javascript jquery
1
Easiest to use thewheelevent these days : stackoverflow.com/a/33334461/3168107.
– Shikkediel
Oct 29 '15 at 11:41
add a comment |
up vote
312
down vote
favorite
up vote
312
down vote
favorite
I'm looking for something to this effect:
$(window).scroll(function(event){
if (/* magic code*/ ){
// upscroll code
} else {
// downscroll code
}
});
Any ideas?
javascript jquery
I'm looking for something to this effect:
$(window).scroll(function(event){
if (/* magic code*/ ){
// upscroll code
} else {
// downscroll code
}
});
Any ideas?
javascript jquery
javascript jquery
edited Feb 3 '13 at 5:33
ThinkingStiff
57k26129227
57k26129227
asked Dec 1 '10 at 16:57
Zach
9,408134767
9,408134767
1
Easiest to use thewheelevent these days : stackoverflow.com/a/33334461/3168107.
– Shikkediel
Oct 29 '15 at 11:41
add a comment |
1
Easiest to use thewheelevent these days : stackoverflow.com/a/33334461/3168107.
– Shikkediel
Oct 29 '15 at 11:41
1
1
Easiest to use the
wheel event these days : stackoverflow.com/a/33334461/3168107.– Shikkediel
Oct 29 '15 at 11:41
Easiest to use the
wheel event these days : stackoverflow.com/a/33334461/3168107.– Shikkediel
Oct 29 '15 at 11:41
add a comment |
22 Answers
22
active
oldest
votes
up vote
619
down vote
accepted
Check current scrollTop vs previous scrollTop
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st;
});
13
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
6
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
3
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a startinglastScrollTopat 0 or will it be properly initialised ??
– TCHdvlp
Aug 23 '13 at 12:57
6
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by settingvar lastScrollTop = $(window).scrollTop()after the browser updates the scroll position on page load.
– Josiah Ruddell
Aug 26 '13 at 17:07
2
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
|
show 14 more comments
up vote
158
down vote
You can do it without having to keep track of the previous scroll top, as all the other examples require:
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
I am not an expert on this so feel free to research it further, but it appears that when you use $(element).scroll, the event being listened for is a 'scroll' event.
But if you specifically listen for a mousewheel event by using bind, the originalEvent attribute of the event parameter to your callback contains different information. Part of that information is wheelDelta. If it's positive, you moved the mousewheel up. If it's negative, you moved the mousewheel down.
My guess is that mousewheel events will fire when the mouse wheel turns, even if the page does not scroll; a case in which 'scroll' events probably are not fired. If you want, you can call event.preventDefault() at the bottom of your callback to prevent the page from scrolling, and so that you can use the mousewheel event for something other than a page scroll, like some type of zoom functionality.
11
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
8
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, soscrollTopdidn't update. In fact,$(window).scroll()didn't fire at all.
– Martti Laine
Dec 14 '12 at 14:16
9
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
11
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
4
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
|
show 7 more comments
up vote
28
down vote
Store the previous scroll location, then see if the new one is greater than or less than that.
Here's a way to avoid any global variables (fiddle available here):
(function () {
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if (currentScroll > previousScroll){
alert('down');
} else {
alert('up');
}
previousScroll = currentScroll;
});
}()); //run this anonymous function immediately
add a comment |
up vote
25
down vote
Existing Solution
There could be 3 solution from this posting and other answer.
Solution 1
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('up 1');
}
else {
console.log('down 1');
}
lastScrollTop = st;
});
Solution 2
$('body').on('DOMMouseScroll', function(e){
if(e.originalEvent.detail < 0) {
console.log('up 2');
}
else {
console.log('down 2');
}
});
Solution 3
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('up 3');
}
else {
console.log('down 3');
}
});
Multi Browser Test
I couldn't tested it on Safari
chrome 42 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Firefox 37 (Win 7)
- Solution 1
- Up : 20 events per 1 scroll
- Down : 20 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : 1 event per 1 scroll
- Solution 3
- Up : Not working
- Down : Not working
IE 11 (Win 8)
- Solution 1
- Up : 10 events per 1 scroll (side effect : down scroll occured at last)
- Down : 10 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : Not working
- Down : 1 event per 1 scroll
IE 10 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 9 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 8 (Win 7)
- Solution 1
- Up : 2 events per 1 scroll (side effect : down scroll occured at last)
- Down : 2~4 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Combined Solution
I checked that side effect from IE 11 and IE 8 is come from
if elsestatement. So, I replaced it withif else ifstatement as following.
From the multi browser test, I decided to use Solution 3 for common browsers and Solution 1 for firefox and IE 11.
I referred this answer to detect IE 11.
// Detect IE version
var iev=0;
var ieold = (/MSIE (d+.d+);/.test(navigator.userAgent));
var trident = !!navigator.userAgent.match(/Trident/7.0/);
var rv=navigator.userAgent.indexOf("rv:11.0");
if (ieold) iev=new Number(RegExp.$1);
if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
if (trident&&rv!=-1) iev=11;
// Firefox or IE 11
if(typeof InstallTrigger !== 'undefined' || iev == 11) {
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('Up');
}
else if(st > lastScrollTop) {
console.log('Down');
}
lastScrollTop = st;
});
}
// Other browsers
else {
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('Up');
}
else if(e.originalEvent.wheelDelta < 0) {
console.log('Down');
}
});
}
If someone could add result ofSafari browser, it would be helpful to supplement solution.
– Chemical Programmer
Apr 18 '15 at 18:07
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
add a comment |
up vote
12
down vote
I understand there has already been an accepted answer, but wanted to post what I am using in case it can help anyone. I get the direction like cliphex with the mousewheel event but with support for Firefox. It's useful doing it this way in case you are doing something like locking scroll and can't get the current scroll top.
See a live version here.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var direction = (function () {
var delta = (e.type === 'DOMMouseScroll' ?
e.originalEvent.detail * -40 :
e.originalEvent.wheelDelta);
return delta > 0 ? 0 : 1;
}());
if(direction === 1) {
// scroll down
}
if(direction === 0) {
// scroll up
}
});
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
add a comment |
up vote
8
down vote
Scroll Event
The scroll event behaves oddly in FF (it is fired a lot of times because of the smoothness scrolling) but it works.
Note: The scroll event actually is fired when dragging the scroll bar, using cursor keys or mousewheel.
//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);
});
Wheel Event
You also may take a look at MDN, it exposes a great information about the Wheel Event.
Note: The wheel event is fired only when using the mousewheel; cursor keys and dragging the scroll bar does not fire the event.
I read the document and the example: Listening to this event across browser
and after some tests with FF, IE, chrome, safari, I ended up with this snippet:
//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);
});
2
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
1
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
add a comment |
up vote
6
down vote
In case you just want to know if you scroll up or down using a pointer device (mouse or track pad) you can use the deltaY property of the wheel event.
$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
add a comment |
up vote
3
down vote
var tempScrollTop, currentScrollTop = 0;
$(window).scroll(function(){
currentScrollTop = $("#div").scrollTop();
if (tempScrollTop > currentScrollTop ) {
// upscroll code
}
else if (tempScrollTop < currentScrollTop ){
// downscroll code
}
tempScrollTop = currentScrollTop;
}
or use the mousewheel extension, see here.
add a comment |
up vote
3
down vote
I have seen many version of good answers here but it seems some folks are having cross browser issues so this is my fix.
I have used this successfully to detect direction in FF, IE and Chrome ... I haven't tested it in safari as I use windows typically.
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll':
function(e) {
if (e.target.id == 'el') return;
e.preventDefault();
e.stopPropagation();
//Determine Direction
if (e.originalEvent.wheelDelta && e.originalEvent.wheelDelta >= 0) {
//Up
alert("up");
} else if (e.originalEvent.detail && e.originalEvent.detail <= 0) {
//Up
alert("up");
} else {
//Down
alert("down");
}
}
});
Keep in mind I also use this to stop any scrolling so if you want scrolling to still occur you must remove the e.preventDefault(); e.stopPropagation();
1
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
add a comment |
up vote
3
down vote
To ignore any snap / momentum / bounce back at the top and bottom of the page, here is a modified version of Josiah's accepted answer:
var prevScrollTop = 0;
$(window).scroll(function(event){
var scrollTop = $(this).scrollTop();
if ( scrollTop < 0 ) {
scrollTop = 0;
}
if ( scrollTop > $('body').height() - $(window).height() ) {
scrollTop = $('body').height() - $(window).height();
}
if (scrollTop >= prevScrollTop && scrollTop) {
// scrolling down
} else {
// scrolling up
}
prevScrollTop = scrollTop;
});
add a comment |
up vote
3
down vote
You can determin mousewhell direction.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var delta = e.originalEvent.wheelDelta ?
e.originalEvent.wheelDelta : -e.originalEvent.detail;
if (delta >= 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
});
add a comment |
up vote
2
down vote
Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.
var scrollableElement = document.getElementById('scrollableElement');
scrollableElement.addEventListener('wheel', findScrollDirectionOtherBrowsers);
function findScrollDirectionOtherBrowsers(event){
var delta;
if (event.wheelDelta){
delta = event.wheelDelta;
}else{
delta = -1 * event.deltaY;
}
if (delta < 0){
console.log("DOWN");
}else if (delta > 0){
console.log("UP");
}
}
Example
add a comment |
up vote
1
down vote
stock an increment in the .data () of element scrolled, you will then be able to test number of times the scroll reached top.
add a comment |
up vote
1
down vote
Keep it super simple:
jQuery Event Listener Way:
$(window).on('wheel', function(){
whichDirection(event);
});
Vanilla JavaScript Event Listener Way:
if(window.addEventListener){
addEventListener('wheel', whichDirection, false);
} else if (window.attachEvent) {
attachEvent('wheel', whichDirection, false);
}
Function Remains The Same:
function whichDirection(event){
console.log(event + ' WheelEvent has all kinds of good stuff to work with');
var scrollDirection = event.deltaY;
if(scrollDirection === 1){
console.log('meet me at the club, going down', scrollDirection);
} else if(scrollDirection === -1) {
console.log('Going up, on a tuesday', scrollDirection);
}
}
I wrote a more indepth post on it here
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
add a comment |
up vote
1
down vote
You can use both scroll and mousewheel option to track up and down movement at once.
$('body').bind('scroll mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('moving down');
}
else {
console.log('moving up');
}
});
You can replace 'body' with (window) as well.
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
add a comment |
up vote
1
down vote
this code work fine with IE, Firefox, Opera and Chrome:
$(window).bind('wheel mousewheel', function(event) {
if (event.originalEvent.deltaY >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
'wheel mousewheel' and the property deltaY must be use in bind() function.
Remember : You're user must update their system and browsers for security reasons. In 2018, the excuses of "I have IE 7" is a nonsense. We must educate users.
Have a good day :)
add a comment |
up vote
0
down vote
For those having problems with elastic scrolling, please use this answer
How to detect scroll direction
add a comment |
up vote
0
down vote
in the .data() of the element you can store a JSON and test values to launch events
{ top : 1,
first_top_event: function(){ ...},
second_top_event: function(){ ...},
third_top_event: function(){ ...},
scroll_down_event1: function(){ ...},
scroll_down_event2: function(){ ...}
}
add a comment |
up vote
0
down vote
This is simple and easy detection for when the user scrolls away from the top of the page and for when they return to the top.
$(window).scroll(function() {
if($(window).scrollTop() > 0) {
// User has scrolled
} else {
// User at top of page
}
});
add a comment |
up vote
0
down vote
This is an optimal solution for detecting the direction just when the user end scrolling.
var currentScrollTop = 0 ;
$(window).bind('scroll', function () {
scrollTop = $(this).scrollTop();
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
if(scrollTop > currentScrollTop){
// downscroll code
$('.mfb-component--bl').addClass('mfbHide');
}else{
// upscroll code
$('.mfb-component--bl').removeClass('mfbHide');
}
currentScrollTop = scrollTop;
}, 250));
});
add a comment |
up vote
0
down vote
You Should try this
var scrl
$(window).scroll(function(){
if($(window).scrollTop() < scrl){
//some code while previous scroll
}else{
if($(window).scrollTop() > 200){
//scroll while downward
}else{//scroll while downward after some specific height
}
}
scrl = $(window).scrollTop();
});
add a comment |
up vote
0
down vote
Why nobody use the event object returned by jQuery on scroll ?
$window.on('scroll', function (event) {
console.group('Scroll');
console.info('Scroll event:', event);
console.info('Position:', this.pageYOffset);
console.info('Direction:', event.originalEvent.dir); // Here is the direction
console.groupEnd();
});
I'm using chromium and I didn't checked on other browsers if they have the dir property.
add a comment |
22 Answers
22
active
oldest
votes
22 Answers
22
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
619
down vote
accepted
Check current scrollTop vs previous scrollTop
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st;
});
13
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
6
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
3
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a startinglastScrollTopat 0 or will it be properly initialised ??
– TCHdvlp
Aug 23 '13 at 12:57
6
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by settingvar lastScrollTop = $(window).scrollTop()after the browser updates the scroll position on page load.
– Josiah Ruddell
Aug 26 '13 at 17:07
2
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
|
show 14 more comments
up vote
619
down vote
accepted
Check current scrollTop vs previous scrollTop
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st;
});
13
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
6
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
3
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a startinglastScrollTopat 0 or will it be properly initialised ??
– TCHdvlp
Aug 23 '13 at 12:57
6
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by settingvar lastScrollTop = $(window).scrollTop()after the browser updates the scroll position on page load.
– Josiah Ruddell
Aug 26 '13 at 17:07
2
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
|
show 14 more comments
up vote
619
down vote
accepted
up vote
619
down vote
accepted
Check current scrollTop vs previous scrollTop
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st;
});
Check current scrollTop vs previous scrollTop
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
// downscroll code
} else {
// upscroll code
}
lastScrollTop = st;
});
edited Mar 21 '13 at 17:50
Glenn Nelson
2,99273555
2,99273555
answered Dec 1 '10 at 17:04
Josiah Ruddell
25.1k75162
25.1k75162
13
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
6
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
3
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a startinglastScrollTopat 0 or will it be properly initialised ??
– TCHdvlp
Aug 23 '13 at 12:57
6
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by settingvar lastScrollTop = $(window).scrollTop()after the browser updates the scroll position on page load.
– Josiah Ruddell
Aug 26 '13 at 17:07
2
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
|
show 14 more comments
13
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
6
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
3
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a startinglastScrollTopat 0 or will it be properly initialised ??
– TCHdvlp
Aug 23 '13 at 12:57
6
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by settingvar lastScrollTop = $(window).scrollTop()after the browser updates the scroll position on page load.
– Josiah Ruddell
Aug 26 '13 at 17:07
2
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
13
13
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
Any way to set a sensibility for this ?
– cocoa coder
Nov 30 '12 at 0:24
6
6
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
I made an example at this codepen. Maybe I'll update update this answer with a jQuery plugin because of the popularity.
– Josiah Ruddell
Nov 30 '12 at 20:15
3
3
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a starting
lastScrollTop at 0 or will it be properly initialised ??– TCHdvlp
Aug 23 '13 at 12:57
Have you tried this if you come back to a previous page having this code ? If you scroll down your page, let say 500PX. Go to another page and then back to initial page. Some browsers keep the scroll position and will bring you back down the page. Will you have a starting
lastScrollTop at 0 or will it be properly initialised ??– TCHdvlp
Aug 23 '13 at 12:57
6
6
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by setting
var lastScrollTop = $(window).scrollTop() after the browser updates the scroll position on page load.– Josiah Ruddell
Aug 26 '13 at 17:07
@TCHdvlp - well worst case scenario the first scroll event would update the variable and the second event would be accurate (.. it would only matter on an upscroll after back navigation). This could be fixed by setting
var lastScrollTop = $(window).scrollTop() after the browser updates the scroll position on page load.– Josiah Ruddell
Aug 26 '13 at 17:07
2
2
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
An update on this: Be careful that some browsers, specially IE 11 on Windows 8, can fire a scroll event subpixel-based (smooth scrolling). But because it reports scrollTop as an integer, your previous scroll value could be the same as the current one.
– Renato
Nov 26 '14 at 0:10
|
show 14 more comments
up vote
158
down vote
You can do it without having to keep track of the previous scroll top, as all the other examples require:
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
I am not an expert on this so feel free to research it further, but it appears that when you use $(element).scroll, the event being listened for is a 'scroll' event.
But if you specifically listen for a mousewheel event by using bind, the originalEvent attribute of the event parameter to your callback contains different information. Part of that information is wheelDelta. If it's positive, you moved the mousewheel up. If it's negative, you moved the mousewheel down.
My guess is that mousewheel events will fire when the mouse wheel turns, even if the page does not scroll; a case in which 'scroll' events probably are not fired. If you want, you can call event.preventDefault() at the bottom of your callback to prevent the page from scrolling, and so that you can use the mousewheel event for something other than a page scroll, like some type of zoom functionality.
11
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
8
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, soscrollTopdidn't update. In fact,$(window).scroll()didn't fire at all.
– Martti Laine
Dec 14 '12 at 14:16
9
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
11
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
4
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
|
show 7 more comments
up vote
158
down vote
You can do it without having to keep track of the previous scroll top, as all the other examples require:
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
I am not an expert on this so feel free to research it further, but it appears that when you use $(element).scroll, the event being listened for is a 'scroll' event.
But if you specifically listen for a mousewheel event by using bind, the originalEvent attribute of the event parameter to your callback contains different information. Part of that information is wheelDelta. If it's positive, you moved the mousewheel up. If it's negative, you moved the mousewheel down.
My guess is that mousewheel events will fire when the mouse wheel turns, even if the page does not scroll; a case in which 'scroll' events probably are not fired. If you want, you can call event.preventDefault() at the bottom of your callback to prevent the page from scrolling, and so that you can use the mousewheel event for something other than a page scroll, like some type of zoom functionality.
11
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
8
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, soscrollTopdidn't update. In fact,$(window).scroll()didn't fire at all.
– Martti Laine
Dec 14 '12 at 14:16
9
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
11
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
4
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
|
show 7 more comments
up vote
158
down vote
up vote
158
down vote
You can do it without having to keep track of the previous scroll top, as all the other examples require:
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
I am not an expert on this so feel free to research it further, but it appears that when you use $(element).scroll, the event being listened for is a 'scroll' event.
But if you specifically listen for a mousewheel event by using bind, the originalEvent attribute of the event parameter to your callback contains different information. Part of that information is wheelDelta. If it's positive, you moved the mousewheel up. If it's negative, you moved the mousewheel down.
My guess is that mousewheel events will fire when the mouse wheel turns, even if the page does not scroll; a case in which 'scroll' events probably are not fired. If you want, you can call event.preventDefault() at the bottom of your callback to prevent the page from scrolling, and so that you can use the mousewheel event for something other than a page scroll, like some type of zoom functionality.
You can do it without having to keep track of the previous scroll top, as all the other examples require:
$(window).bind('mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
I am not an expert on this so feel free to research it further, but it appears that when you use $(element).scroll, the event being listened for is a 'scroll' event.
But if you specifically listen for a mousewheel event by using bind, the originalEvent attribute of the event parameter to your callback contains different information. Part of that information is wheelDelta. If it's positive, you moved the mousewheel up. If it's negative, you moved the mousewheel down.
My guess is that mousewheel events will fire when the mouse wheel turns, even if the page does not scroll; a case in which 'scroll' events probably are not fired. If you want, you can call event.preventDefault() at the bottom of your callback to prevent the page from scrolling, and so that you can use the mousewheel event for something other than a page scroll, like some type of zoom functionality.
edited May 11 '12 at 9:15
CharlesB
57.9k19145167
57.9k19145167
answered May 11 '12 at 4:46
cilphex
3,42442128
3,42442128
11
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
8
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, soscrollTopdidn't update. In fact,$(window).scroll()didn't fire at all.
– Martti Laine
Dec 14 '12 at 14:16
9
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
11
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
4
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
|
show 7 more comments
11
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
8
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, soscrollTopdidn't update. In fact,$(window).scroll()didn't fire at all.
– Martti Laine
Dec 14 '12 at 14:16
9
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
11
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
4
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
11
11
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
Nice but sadly the one and only Firefox is not supporting it developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/… (tested in FF v15). :C
– yoshi
Sep 17 '12 at 14:07
8
8
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, so
scrollTop didn't update. In fact, $(window).scroll() didn't fire at all.– Martti Laine
Dec 14 '12 at 14:16
This was useful for me: I needed to detect scrolling, even though the page itself didn't actually scroll, so
scrollTop didn't update. In fact, $(window).scroll() didn't fire at all.– Martti Laine
Dec 14 '12 at 14:16
9
9
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
It's nice that you don't need the old state. However this technique will not work on mobiles or tablets, since they don't have a mousewheel!
– joeytwiddle
Aug 1 '13 at 4:43
11
11
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
This approach won't work if I scroll with the keyboard. It's definitely an interesting approach for things like zoom, but I don't think it addresses the scroll direction question.
– chmac
Feb 4 '14 at 9:33
4
4
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll': function(e) { //... }); works for me in detecting all browser scroll data
– GoreDefex
Nov 4 '14 at 19:15
|
show 7 more comments
up vote
28
down vote
Store the previous scroll location, then see if the new one is greater than or less than that.
Here's a way to avoid any global variables (fiddle available here):
(function () {
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if (currentScroll > previousScroll){
alert('down');
} else {
alert('up');
}
previousScroll = currentScroll;
});
}()); //run this anonymous function immediately
add a comment |
up vote
28
down vote
Store the previous scroll location, then see if the new one is greater than or less than that.
Here's a way to avoid any global variables (fiddle available here):
(function () {
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if (currentScroll > previousScroll){
alert('down');
} else {
alert('up');
}
previousScroll = currentScroll;
});
}()); //run this anonymous function immediately
add a comment |
up vote
28
down vote
up vote
28
down vote
Store the previous scroll location, then see if the new one is greater than or less than that.
Here's a way to avoid any global variables (fiddle available here):
(function () {
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if (currentScroll > previousScroll){
alert('down');
} else {
alert('up');
}
previousScroll = currentScroll;
});
}()); //run this anonymous function immediately
Store the previous scroll location, then see if the new one is greater than or less than that.
Here's a way to avoid any global variables (fiddle available here):
(function () {
var previousScroll = 0;
$(window).scroll(function(){
var currentScroll = $(this).scrollTop();
if (currentScroll > previousScroll){
alert('down');
} else {
alert('up');
}
previousScroll = currentScroll;
});
}()); //run this anonymous function immediately
edited Dec 1 '10 at 17:18
answered Dec 1 '10 at 17:04
Skilldrick
50.7k31157222
50.7k31157222
add a comment |
add a comment |
up vote
25
down vote
Existing Solution
There could be 3 solution from this posting and other answer.
Solution 1
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('up 1');
}
else {
console.log('down 1');
}
lastScrollTop = st;
});
Solution 2
$('body').on('DOMMouseScroll', function(e){
if(e.originalEvent.detail < 0) {
console.log('up 2');
}
else {
console.log('down 2');
}
});
Solution 3
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('up 3');
}
else {
console.log('down 3');
}
});
Multi Browser Test
I couldn't tested it on Safari
chrome 42 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Firefox 37 (Win 7)
- Solution 1
- Up : 20 events per 1 scroll
- Down : 20 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : 1 event per 1 scroll
- Solution 3
- Up : Not working
- Down : Not working
IE 11 (Win 8)
- Solution 1
- Up : 10 events per 1 scroll (side effect : down scroll occured at last)
- Down : 10 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : Not working
- Down : 1 event per 1 scroll
IE 10 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 9 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 8 (Win 7)
- Solution 1
- Up : 2 events per 1 scroll (side effect : down scroll occured at last)
- Down : 2~4 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Combined Solution
I checked that side effect from IE 11 and IE 8 is come from
if elsestatement. So, I replaced it withif else ifstatement as following.
From the multi browser test, I decided to use Solution 3 for common browsers and Solution 1 for firefox and IE 11.
I referred this answer to detect IE 11.
// Detect IE version
var iev=0;
var ieold = (/MSIE (d+.d+);/.test(navigator.userAgent));
var trident = !!navigator.userAgent.match(/Trident/7.0/);
var rv=navigator.userAgent.indexOf("rv:11.0");
if (ieold) iev=new Number(RegExp.$1);
if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
if (trident&&rv!=-1) iev=11;
// Firefox or IE 11
if(typeof InstallTrigger !== 'undefined' || iev == 11) {
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('Up');
}
else if(st > lastScrollTop) {
console.log('Down');
}
lastScrollTop = st;
});
}
// Other browsers
else {
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('Up');
}
else if(e.originalEvent.wheelDelta < 0) {
console.log('Down');
}
});
}
If someone could add result ofSafari browser, it would be helpful to supplement solution.
– Chemical Programmer
Apr 18 '15 at 18:07
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
add a comment |
up vote
25
down vote
Existing Solution
There could be 3 solution from this posting and other answer.
Solution 1
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('up 1');
}
else {
console.log('down 1');
}
lastScrollTop = st;
});
Solution 2
$('body').on('DOMMouseScroll', function(e){
if(e.originalEvent.detail < 0) {
console.log('up 2');
}
else {
console.log('down 2');
}
});
Solution 3
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('up 3');
}
else {
console.log('down 3');
}
});
Multi Browser Test
I couldn't tested it on Safari
chrome 42 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Firefox 37 (Win 7)
- Solution 1
- Up : 20 events per 1 scroll
- Down : 20 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : 1 event per 1 scroll
- Solution 3
- Up : Not working
- Down : Not working
IE 11 (Win 8)
- Solution 1
- Up : 10 events per 1 scroll (side effect : down scroll occured at last)
- Down : 10 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : Not working
- Down : 1 event per 1 scroll
IE 10 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 9 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 8 (Win 7)
- Solution 1
- Up : 2 events per 1 scroll (side effect : down scroll occured at last)
- Down : 2~4 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Combined Solution
I checked that side effect from IE 11 and IE 8 is come from
if elsestatement. So, I replaced it withif else ifstatement as following.
From the multi browser test, I decided to use Solution 3 for common browsers and Solution 1 for firefox and IE 11.
I referred this answer to detect IE 11.
// Detect IE version
var iev=0;
var ieold = (/MSIE (d+.d+);/.test(navigator.userAgent));
var trident = !!navigator.userAgent.match(/Trident/7.0/);
var rv=navigator.userAgent.indexOf("rv:11.0");
if (ieold) iev=new Number(RegExp.$1);
if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
if (trident&&rv!=-1) iev=11;
// Firefox or IE 11
if(typeof InstallTrigger !== 'undefined' || iev == 11) {
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('Up');
}
else if(st > lastScrollTop) {
console.log('Down');
}
lastScrollTop = st;
});
}
// Other browsers
else {
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('Up');
}
else if(e.originalEvent.wheelDelta < 0) {
console.log('Down');
}
});
}
If someone could add result ofSafari browser, it would be helpful to supplement solution.
– Chemical Programmer
Apr 18 '15 at 18:07
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
add a comment |
up vote
25
down vote
up vote
25
down vote
Existing Solution
There could be 3 solution from this posting and other answer.
Solution 1
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('up 1');
}
else {
console.log('down 1');
}
lastScrollTop = st;
});
Solution 2
$('body').on('DOMMouseScroll', function(e){
if(e.originalEvent.detail < 0) {
console.log('up 2');
}
else {
console.log('down 2');
}
});
Solution 3
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('up 3');
}
else {
console.log('down 3');
}
});
Multi Browser Test
I couldn't tested it on Safari
chrome 42 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Firefox 37 (Win 7)
- Solution 1
- Up : 20 events per 1 scroll
- Down : 20 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : 1 event per 1 scroll
- Solution 3
- Up : Not working
- Down : Not working
IE 11 (Win 8)
- Solution 1
- Up : 10 events per 1 scroll (side effect : down scroll occured at last)
- Down : 10 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : Not working
- Down : 1 event per 1 scroll
IE 10 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 9 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 8 (Win 7)
- Solution 1
- Up : 2 events per 1 scroll (side effect : down scroll occured at last)
- Down : 2~4 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Combined Solution
I checked that side effect from IE 11 and IE 8 is come from
if elsestatement. So, I replaced it withif else ifstatement as following.
From the multi browser test, I decided to use Solution 3 for common browsers and Solution 1 for firefox and IE 11.
I referred this answer to detect IE 11.
// Detect IE version
var iev=0;
var ieold = (/MSIE (d+.d+);/.test(navigator.userAgent));
var trident = !!navigator.userAgent.match(/Trident/7.0/);
var rv=navigator.userAgent.indexOf("rv:11.0");
if (ieold) iev=new Number(RegExp.$1);
if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
if (trident&&rv!=-1) iev=11;
// Firefox or IE 11
if(typeof InstallTrigger !== 'undefined' || iev == 11) {
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('Up');
}
else if(st > lastScrollTop) {
console.log('Down');
}
lastScrollTop = st;
});
}
// Other browsers
else {
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('Up');
}
else if(e.originalEvent.wheelDelta < 0) {
console.log('Down');
}
});
}
Existing Solution
There could be 3 solution from this posting and other answer.
Solution 1
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('up 1');
}
else {
console.log('down 1');
}
lastScrollTop = st;
});
Solution 2
$('body').on('DOMMouseScroll', function(e){
if(e.originalEvent.detail < 0) {
console.log('up 2');
}
else {
console.log('down 2');
}
});
Solution 3
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('up 3');
}
else {
console.log('down 3');
}
});
Multi Browser Test
I couldn't tested it on Safari
chrome 42 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Firefox 37 (Win 7)
- Solution 1
- Up : 20 events per 1 scroll
- Down : 20 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : 1 event per 1 scroll
- Solution 3
- Up : Not working
- Down : Not working
IE 11 (Win 8)
- Solution 1
- Up : 10 events per 1 scroll (side effect : down scroll occured at last)
- Down : 10 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : Not working
- Down : 1 event per 1 scroll
IE 10 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 9 (Win 7)
- Solution 1
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
IE 8 (Win 7)
- Solution 1
- Up : 2 events per 1 scroll (side effect : down scroll occured at last)
- Down : 2~4 events per 1 scroll
- Soltion 2
- Up : Not working
- Down : Not working
- Solution 3
- Up : 1 event per 1 scroll
- Down : 1 event per 1 scroll
Combined Solution
I checked that side effect from IE 11 and IE 8 is come from
if elsestatement. So, I replaced it withif else ifstatement as following.
From the multi browser test, I decided to use Solution 3 for common browsers and Solution 1 for firefox and IE 11.
I referred this answer to detect IE 11.
// Detect IE version
var iev=0;
var ieold = (/MSIE (d+.d+);/.test(navigator.userAgent));
var trident = !!navigator.userAgent.match(/Trident/7.0/);
var rv=navigator.userAgent.indexOf("rv:11.0");
if (ieold) iev=new Number(RegExp.$1);
if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
if (trident&&rv!=-1) iev=11;
// Firefox or IE 11
if(typeof InstallTrigger !== 'undefined' || iev == 11) {
var lastScrollTop = 0;
$(window).on('scroll', function() {
st = $(this).scrollTop();
if(st < lastScrollTop) {
console.log('Up');
}
else if(st > lastScrollTop) {
console.log('Down');
}
lastScrollTop = st;
});
}
// Other browsers
else {
$('body').on('mousewheel', function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('Up');
}
else if(e.originalEvent.wheelDelta < 0) {
console.log('Down');
}
});
}
edited May 23 '17 at 10:31
Community♦
11
11
answered Apr 18 '15 at 17:46
Chemical Programmer
2,29422437
2,29422437
If someone could add result ofSafari browser, it would be helpful to supplement solution.
– Chemical Programmer
Apr 18 '15 at 18:07
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
add a comment |
If someone could add result ofSafari browser, it would be helpful to supplement solution.
– Chemical Programmer
Apr 18 '15 at 18:07
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
If someone could add result of
Safari browser, it would be helpful to supplement solution.– Chemical Programmer
Apr 18 '15 at 18:07
If someone could add result of
Safari browser, it would be helpful to supplement solution.– Chemical Programmer
Apr 18 '15 at 18:07
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Oops! I found that Mobile Chrome and Android default browser are only covered by Solution 1. So it would be better to use both Solution 1 and 3 together to cover various browsers.
– Chemical Programmer
Apr 18 '15 at 18:25
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Solution 1 doesn't work on Firefox & IE 11
– Mohamed Salem Lamiri
Nov 28 '15 at 23:29
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
Will not work if the design is a fixed layout design. If you want to detect the scroll direction on a static no-scroll website, Firefox and IE11 will not work
– Shannon Hochkins
Jan 5 '16 at 22:04
add a comment |
up vote
12
down vote
I understand there has already been an accepted answer, but wanted to post what I am using in case it can help anyone. I get the direction like cliphex with the mousewheel event but with support for Firefox. It's useful doing it this way in case you are doing something like locking scroll and can't get the current scroll top.
See a live version here.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var direction = (function () {
var delta = (e.type === 'DOMMouseScroll' ?
e.originalEvent.detail * -40 :
e.originalEvent.wheelDelta);
return delta > 0 ? 0 : 1;
}());
if(direction === 1) {
// scroll down
}
if(direction === 0) {
// scroll up
}
});
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
add a comment |
up vote
12
down vote
I understand there has already been an accepted answer, but wanted to post what I am using in case it can help anyone. I get the direction like cliphex with the mousewheel event but with support for Firefox. It's useful doing it this way in case you are doing something like locking scroll and can't get the current scroll top.
See a live version here.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var direction = (function () {
var delta = (e.type === 'DOMMouseScroll' ?
e.originalEvent.detail * -40 :
e.originalEvent.wheelDelta);
return delta > 0 ? 0 : 1;
}());
if(direction === 1) {
// scroll down
}
if(direction === 0) {
// scroll up
}
});
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
add a comment |
up vote
12
down vote
up vote
12
down vote
I understand there has already been an accepted answer, but wanted to post what I am using in case it can help anyone. I get the direction like cliphex with the mousewheel event but with support for Firefox. It's useful doing it this way in case you are doing something like locking scroll and can't get the current scroll top.
See a live version here.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var direction = (function () {
var delta = (e.type === 'DOMMouseScroll' ?
e.originalEvent.detail * -40 :
e.originalEvent.wheelDelta);
return delta > 0 ? 0 : 1;
}());
if(direction === 1) {
// scroll down
}
if(direction === 0) {
// scroll up
}
});
I understand there has already been an accepted answer, but wanted to post what I am using in case it can help anyone. I get the direction like cliphex with the mousewheel event but with support for Firefox. It's useful doing it this way in case you are doing something like locking scroll and can't get the current scroll top.
See a live version here.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var direction = (function () {
var delta = (e.type === 'DOMMouseScroll' ?
e.originalEvent.detail * -40 :
e.originalEvent.wheelDelta);
return delta > 0 ? 0 : 1;
}());
if(direction === 1) {
// scroll down
}
if(direction === 0) {
// scroll up
}
});
edited Jan 28 '15 at 20:15
answered Jan 3 '15 at 5:56
souporserious
1,03311639
1,03311639
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
add a comment |
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
@EtienneMartin the above code relies on jQuery if that's what was causing your error. Please see the attached fiddle to see it working.
– souporserious
Jan 28 '15 at 20:16
add a comment |
up vote
8
down vote
Scroll Event
The scroll event behaves oddly in FF (it is fired a lot of times because of the smoothness scrolling) but it works.
Note: The scroll event actually is fired when dragging the scroll bar, using cursor keys or mousewheel.
//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);
});
Wheel Event
You also may take a look at MDN, it exposes a great information about the Wheel Event.
Note: The wheel event is fired only when using the mousewheel; cursor keys and dragging the scroll bar does not fire the event.
I read the document and the example: Listening to this event across browser
and after some tests with FF, IE, chrome, safari, I ended up with this snippet:
//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);
});
2
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
1
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
add a comment |
up vote
8
down vote
Scroll Event
The scroll event behaves oddly in FF (it is fired a lot of times because of the smoothness scrolling) but it works.
Note: The scroll event actually is fired when dragging the scroll bar, using cursor keys or mousewheel.
//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);
});
Wheel Event
You also may take a look at MDN, it exposes a great information about the Wheel Event.
Note: The wheel event is fired only when using the mousewheel; cursor keys and dragging the scroll bar does not fire the event.
I read the document and the example: Listening to this event across browser
and after some tests with FF, IE, chrome, safari, I ended up with this snippet:
//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);
});
2
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
1
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
add a comment |
up vote
8
down vote
up vote
8
down vote
Scroll Event
The scroll event behaves oddly in FF (it is fired a lot of times because of the smoothness scrolling) but it works.
Note: The scroll event actually is fired when dragging the scroll bar, using cursor keys or mousewheel.
//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);
});
Wheel Event
You also may take a look at MDN, it exposes a great information about the Wheel Event.
Note: The wheel event is fired only when using the mousewheel; cursor keys and dragging the scroll bar does not fire the event.
I read the document and the example: Listening to this event across browser
and after some tests with FF, IE, chrome, safari, I ended up with this snippet:
//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);
});
Scroll Event
The scroll event behaves oddly in FF (it is fired a lot of times because of the smoothness scrolling) but it works.
Note: The scroll event actually is fired when dragging the scroll bar, using cursor keys or mousewheel.
//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);
});
Wheel Event
You also may take a look at MDN, it exposes a great information about the Wheel Event.
Note: The wheel event is fired only when using the mousewheel; cursor keys and dragging the scroll bar does not fire the event.
I read the document and the example: Listening to this event across browser
and after some tests with FF, IE, chrome, safari, I ended up with this snippet:
//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);
});
edited Mar 31 '15 at 16:57
community wiki
5 revs
jherax
2
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
1
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
add a comment |
2
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
1
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
2
2
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
I have a fixed panel view which disables the actual scroll bar, so I needed to detect the direction from the mouse wheel. Your mousewheel solution worked perfectly cross browser so thankyou for that!
– Shannon Hochkins
Jan 5 '16 at 22:39
1
1
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
This should be the accepted answer.
– Felipe Francisco
Sep 6 '17 at 11:37
add a comment |
up vote
6
down vote
In case you just want to know if you scroll up or down using a pointer device (mouse or track pad) you can use the deltaY property of the wheel event.
$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
add a comment |
up vote
6
down vote
In case you just want to know if you scroll up or down using a pointer device (mouse or track pad) you can use the deltaY property of the wheel event.
$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
add a comment |
up vote
6
down vote
up vote
6
down vote
In case you just want to know if you scroll up or down using a pointer device (mouse or track pad) you can use the deltaY property of the wheel event.
$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>In case you just want to know if you scroll up or down using a pointer device (mouse or track pad) you can use the deltaY property of the wheel event.
$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>$('.container').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
$('.result').append('Scrolled down!<br>');
} else {
$('.result').append('Scrolled up!<br>');
}
});.container {
height: 200px;
width: 400px;
margin: 20px;
border: 1px solid black;
overflow-y: auto;
}
.content {
height: 300px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
Scroll me!
</div>
</div>
<div class="result">
<p>Action:</p>
</div>answered Jan 12 '16 at 14:41
Yuri
306412
306412
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
add a comment |
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
This link says not to rely on deltaY developer.mozilla.org/en-US/docs/Web/Events/wheel
– Charlie Martin
Apr 27 at 5:39
add a comment |
up vote
3
down vote
var tempScrollTop, currentScrollTop = 0;
$(window).scroll(function(){
currentScrollTop = $("#div").scrollTop();
if (tempScrollTop > currentScrollTop ) {
// upscroll code
}
else if (tempScrollTop < currentScrollTop ){
// downscroll code
}
tempScrollTop = currentScrollTop;
}
or use the mousewheel extension, see here.
add a comment |
up vote
3
down vote
var tempScrollTop, currentScrollTop = 0;
$(window).scroll(function(){
currentScrollTop = $("#div").scrollTop();
if (tempScrollTop > currentScrollTop ) {
// upscroll code
}
else if (tempScrollTop < currentScrollTop ){
// downscroll code
}
tempScrollTop = currentScrollTop;
}
or use the mousewheel extension, see here.
add a comment |
up vote
3
down vote
up vote
3
down vote
var tempScrollTop, currentScrollTop = 0;
$(window).scroll(function(){
currentScrollTop = $("#div").scrollTop();
if (tempScrollTop > currentScrollTop ) {
// upscroll code
}
else if (tempScrollTop < currentScrollTop ){
// downscroll code
}
tempScrollTop = currentScrollTop;
}
or use the mousewheel extension, see here.
var tempScrollTop, currentScrollTop = 0;
$(window).scroll(function(){
currentScrollTop = $("#div").scrollTop();
if (tempScrollTop > currentScrollTop ) {
// upscroll code
}
else if (tempScrollTop < currentScrollTop ){
// downscroll code
}
tempScrollTop = currentScrollTop;
}
or use the mousewheel extension, see here.
answered Dec 1 '10 at 17:06
Adam
28.8k1383122
28.8k1383122
add a comment |
add a comment |
up vote
3
down vote
I have seen many version of good answers here but it seems some folks are having cross browser issues so this is my fix.
I have used this successfully to detect direction in FF, IE and Chrome ... I haven't tested it in safari as I use windows typically.
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll':
function(e) {
if (e.target.id == 'el') return;
e.preventDefault();
e.stopPropagation();
//Determine Direction
if (e.originalEvent.wheelDelta && e.originalEvent.wheelDelta >= 0) {
//Up
alert("up");
} else if (e.originalEvent.detail && e.originalEvent.detail <= 0) {
//Up
alert("up");
} else {
//Down
alert("down");
}
}
});
Keep in mind I also use this to stop any scrolling so if you want scrolling to still occur you must remove the e.preventDefault(); e.stopPropagation();
1
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
add a comment |
up vote
3
down vote
I have seen many version of good answers here but it seems some folks are having cross browser issues so this is my fix.
I have used this successfully to detect direction in FF, IE and Chrome ... I haven't tested it in safari as I use windows typically.
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll':
function(e) {
if (e.target.id == 'el') return;
e.preventDefault();
e.stopPropagation();
//Determine Direction
if (e.originalEvent.wheelDelta && e.originalEvent.wheelDelta >= 0) {
//Up
alert("up");
} else if (e.originalEvent.detail && e.originalEvent.detail <= 0) {
//Up
alert("up");
} else {
//Down
alert("down");
}
}
});
Keep in mind I also use this to stop any scrolling so if you want scrolling to still occur you must remove the e.preventDefault(); e.stopPropagation();
1
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
add a comment |
up vote
3
down vote
up vote
3
down vote
I have seen many version of good answers here but it seems some folks are having cross browser issues so this is my fix.
I have used this successfully to detect direction in FF, IE and Chrome ... I haven't tested it in safari as I use windows typically.
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll':
function(e) {
if (e.target.id == 'el') return;
e.preventDefault();
e.stopPropagation();
//Determine Direction
if (e.originalEvent.wheelDelta && e.originalEvent.wheelDelta >= 0) {
//Up
alert("up");
} else if (e.originalEvent.detail && e.originalEvent.detail <= 0) {
//Up
alert("up");
} else {
//Down
alert("down");
}
}
});
Keep in mind I also use this to stop any scrolling so if you want scrolling to still occur you must remove the e.preventDefault(); e.stopPropagation();
I have seen many version of good answers here but it seems some folks are having cross browser issues so this is my fix.
I have used this successfully to detect direction in FF, IE and Chrome ... I haven't tested it in safari as I use windows typically.
$("html, body").bind({'mousewheel DOMMouseScroll onmousewheel touchmove scroll':
function(e) {
if (e.target.id == 'el') return;
e.preventDefault();
e.stopPropagation();
//Determine Direction
if (e.originalEvent.wheelDelta && e.originalEvent.wheelDelta >= 0) {
//Up
alert("up");
} else if (e.originalEvent.detail && e.originalEvent.detail <= 0) {
//Up
alert("up");
} else {
//Down
alert("down");
}
}
});
Keep in mind I also use this to stop any scrolling so if you want scrolling to still occur you must remove the e.preventDefault(); e.stopPropagation();
edited Nov 13 '14 at 13:10
answered Nov 4 '14 at 18:31
GoreDefex
737625
737625
1
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
add a comment |
1
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
1
1
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
always returns down on android GS5
– SISYN
Sep 11 '16 at 17:05
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
This worked great! I had an issue with the top voted answer on IE. This does not have that issue! +1 from me.
– Radmation
Oct 22 at 22:31
add a comment |
up vote
3
down vote
To ignore any snap / momentum / bounce back at the top and bottom of the page, here is a modified version of Josiah's accepted answer:
var prevScrollTop = 0;
$(window).scroll(function(event){
var scrollTop = $(this).scrollTop();
if ( scrollTop < 0 ) {
scrollTop = 0;
}
if ( scrollTop > $('body').height() - $(window).height() ) {
scrollTop = $('body').height() - $(window).height();
}
if (scrollTop >= prevScrollTop && scrollTop) {
// scrolling down
} else {
// scrolling up
}
prevScrollTop = scrollTop;
});
add a comment |
up vote
3
down vote
To ignore any snap / momentum / bounce back at the top and bottom of the page, here is a modified version of Josiah's accepted answer:
var prevScrollTop = 0;
$(window).scroll(function(event){
var scrollTop = $(this).scrollTop();
if ( scrollTop < 0 ) {
scrollTop = 0;
}
if ( scrollTop > $('body').height() - $(window).height() ) {
scrollTop = $('body').height() - $(window).height();
}
if (scrollTop >= prevScrollTop && scrollTop) {
// scrolling down
} else {
// scrolling up
}
prevScrollTop = scrollTop;
});
add a comment |
up vote
3
down vote
up vote
3
down vote
To ignore any snap / momentum / bounce back at the top and bottom of the page, here is a modified version of Josiah's accepted answer:
var prevScrollTop = 0;
$(window).scroll(function(event){
var scrollTop = $(this).scrollTop();
if ( scrollTop < 0 ) {
scrollTop = 0;
}
if ( scrollTop > $('body').height() - $(window).height() ) {
scrollTop = $('body').height() - $(window).height();
}
if (scrollTop >= prevScrollTop && scrollTop) {
// scrolling down
} else {
// scrolling up
}
prevScrollTop = scrollTop;
});
To ignore any snap / momentum / bounce back at the top and bottom of the page, here is a modified version of Josiah's accepted answer:
var prevScrollTop = 0;
$(window).scroll(function(event){
var scrollTop = $(this).scrollTop();
if ( scrollTop < 0 ) {
scrollTop = 0;
}
if ( scrollTop > $('body').height() - $(window).height() ) {
scrollTop = $('body').height() - $(window).height();
}
if (scrollTop >= prevScrollTop && scrollTop) {
// scrolling down
} else {
// scrolling up
}
prevScrollTop = scrollTop;
});
edited May 23 '17 at 12:18
Community♦
11
11
answered Jan 28 '15 at 22:15
Andrew Tibbetts
1,07321125
1,07321125
add a comment |
add a comment |
up vote
3
down vote
You can determin mousewhell direction.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var delta = e.originalEvent.wheelDelta ?
e.originalEvent.wheelDelta : -e.originalEvent.detail;
if (delta >= 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
});
add a comment |
up vote
3
down vote
You can determin mousewhell direction.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var delta = e.originalEvent.wheelDelta ?
e.originalEvent.wheelDelta : -e.originalEvent.detail;
if (delta >= 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
});
add a comment |
up vote
3
down vote
up vote
3
down vote
You can determin mousewhell direction.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var delta = e.originalEvent.wheelDelta ?
e.originalEvent.wheelDelta : -e.originalEvent.detail;
if (delta >= 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
});
You can determin mousewhell direction.
$(window).on('mousewheel DOMMouseScroll', function (e) {
var delta = e.originalEvent.wheelDelta ?
e.originalEvent.wheelDelta : -e.originalEvent.detail;
if (delta >= 0) {
console.log('scroll up');
} else {
console.log('scroll down');
}
});
edited Dec 31 '16 at 11:07
answered Sep 11 '16 at 20:43
J. Kovacevic
16415
16415
add a comment |
add a comment |
up vote
2
down vote
Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.
var scrollableElement = document.getElementById('scrollableElement');
scrollableElement.addEventListener('wheel', findScrollDirectionOtherBrowsers);
function findScrollDirectionOtherBrowsers(event){
var delta;
if (event.wheelDelta){
delta = event.wheelDelta;
}else{
delta = -1 * event.deltaY;
}
if (delta < 0){
console.log("DOWN");
}else if (delta > 0){
console.log("UP");
}
}
Example
add a comment |
up vote
2
down vote
Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.
var scrollableElement = document.getElementById('scrollableElement');
scrollableElement.addEventListener('wheel', findScrollDirectionOtherBrowsers);
function findScrollDirectionOtherBrowsers(event){
var delta;
if (event.wheelDelta){
delta = event.wheelDelta;
}else{
delta = -1 * event.deltaY;
}
if (delta < 0){
console.log("DOWN");
}else if (delta > 0){
console.log("UP");
}
}
Example
add a comment |
up vote
2
down vote
up vote
2
down vote
Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.
var scrollableElement = document.getElementById('scrollableElement');
scrollableElement.addEventListener('wheel', findScrollDirectionOtherBrowsers);
function findScrollDirectionOtherBrowsers(event){
var delta;
if (event.wheelDelta){
delta = event.wheelDelta;
}else{
delta = -1 * event.deltaY;
}
if (delta < 0){
console.log("DOWN");
}else if (delta > 0){
console.log("UP");
}
}
Example
Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.
var scrollableElement = document.getElementById('scrollableElement');
scrollableElement.addEventListener('wheel', findScrollDirectionOtherBrowsers);
function findScrollDirectionOtherBrowsers(event){
var delta;
if (event.wheelDelta){
delta = event.wheelDelta;
}else{
delta = -1 * event.deltaY;
}
if (delta < 0){
console.log("DOWN");
}else if (delta > 0){
console.log("UP");
}
}
Example
answered Jun 15 '17 at 16:08
Vasi
475214
475214
add a comment |
add a comment |
up vote
1
down vote
stock an increment in the .data () of element scrolled, you will then be able to test number of times the scroll reached top.
add a comment |
up vote
1
down vote
stock an increment in the .data () of element scrolled, you will then be able to test number of times the scroll reached top.
add a comment |
up vote
1
down vote
up vote
1
down vote
stock an increment in the .data () of element scrolled, you will then be able to test number of times the scroll reached top.
stock an increment in the .data () of element scrolled, you will then be able to test number of times the scroll reached top.
edited Dec 11 '13 at 16:32
Robert
4,0321251106
4,0321251106
answered Dec 11 '13 at 16:03
Spacefrog
111
111
add a comment |
add a comment |
up vote
1
down vote
Keep it super simple:
jQuery Event Listener Way:
$(window).on('wheel', function(){
whichDirection(event);
});
Vanilla JavaScript Event Listener Way:
if(window.addEventListener){
addEventListener('wheel', whichDirection, false);
} else if (window.attachEvent) {
attachEvent('wheel', whichDirection, false);
}
Function Remains The Same:
function whichDirection(event){
console.log(event + ' WheelEvent has all kinds of good stuff to work with');
var scrollDirection = event.deltaY;
if(scrollDirection === 1){
console.log('meet me at the club, going down', scrollDirection);
} else if(scrollDirection === -1) {
console.log('Going up, on a tuesday', scrollDirection);
}
}
I wrote a more indepth post on it here
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
add a comment |
up vote
1
down vote
Keep it super simple:
jQuery Event Listener Way:
$(window).on('wheel', function(){
whichDirection(event);
});
Vanilla JavaScript Event Listener Way:
if(window.addEventListener){
addEventListener('wheel', whichDirection, false);
} else if (window.attachEvent) {
attachEvent('wheel', whichDirection, false);
}
Function Remains The Same:
function whichDirection(event){
console.log(event + ' WheelEvent has all kinds of good stuff to work with');
var scrollDirection = event.deltaY;
if(scrollDirection === 1){
console.log('meet me at the club, going down', scrollDirection);
} else if(scrollDirection === -1) {
console.log('Going up, on a tuesday', scrollDirection);
}
}
I wrote a more indepth post on it here
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
add a comment |
up vote
1
down vote
up vote
1
down vote
Keep it super simple:
jQuery Event Listener Way:
$(window).on('wheel', function(){
whichDirection(event);
});
Vanilla JavaScript Event Listener Way:
if(window.addEventListener){
addEventListener('wheel', whichDirection, false);
} else if (window.attachEvent) {
attachEvent('wheel', whichDirection, false);
}
Function Remains The Same:
function whichDirection(event){
console.log(event + ' WheelEvent has all kinds of good stuff to work with');
var scrollDirection = event.deltaY;
if(scrollDirection === 1){
console.log('meet me at the club, going down', scrollDirection);
} else if(scrollDirection === -1) {
console.log('Going up, on a tuesday', scrollDirection);
}
}
I wrote a more indepth post on it here
Keep it super simple:
jQuery Event Listener Way:
$(window).on('wheel', function(){
whichDirection(event);
});
Vanilla JavaScript Event Listener Way:
if(window.addEventListener){
addEventListener('wheel', whichDirection, false);
} else if (window.attachEvent) {
attachEvent('wheel', whichDirection, false);
}
Function Remains The Same:
function whichDirection(event){
console.log(event + ' WheelEvent has all kinds of good stuff to work with');
var scrollDirection = event.deltaY;
if(scrollDirection === 1){
console.log('meet me at the club, going down', scrollDirection);
} else if(scrollDirection === -1) {
console.log('Going up, on a tuesday', scrollDirection);
}
}
I wrote a more indepth post on it here
edited Dec 29 '16 at 22:05
frzsombor
612927
612927
answered Nov 8 '16 at 23:32
CR Rollyson
651510
651510
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
add a comment |
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
Is jquery-wheel required to use the jquery version?
– Hastig Zusammenstellen
Sep 26 '17 at 8:06
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
jquery-wheel is not required for this @HastigZusammenstellen
– CR Rollyson
Sep 26 '17 at 16:39
add a comment |
up vote
1
down vote
You can use both scroll and mousewheel option to track up and down movement at once.
$('body').bind('scroll mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('moving down');
}
else {
console.log('moving up');
}
});
You can replace 'body' with (window) as well.
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
add a comment |
up vote
1
down vote
You can use both scroll and mousewheel option to track up and down movement at once.
$('body').bind('scroll mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('moving down');
}
else {
console.log('moving up');
}
});
You can replace 'body' with (window) as well.
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use both scroll and mousewheel option to track up and down movement at once.
$('body').bind('scroll mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('moving down');
}
else {
console.log('moving up');
}
});
You can replace 'body' with (window) as well.
You can use both scroll and mousewheel option to track up and down movement at once.
$('body').bind('scroll mousewheel', function(event) {
if (event.originalEvent.wheelDelta >= 0) {
console.log('moving down');
}
else {
console.log('moving up');
}
});
You can replace 'body' with (window) as well.
answered Mar 11 '17 at 23:13
Zeeshan Rasool
133
133
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
add a comment |
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
Seems to work fine in Chrome but not in FF (55.0.2, Ubuntu)
– Hastig Zusammenstellen
Sep 26 '17 at 7:45
add a comment |
up vote
1
down vote
this code work fine with IE, Firefox, Opera and Chrome:
$(window).bind('wheel mousewheel', function(event) {
if (event.originalEvent.deltaY >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
'wheel mousewheel' and the property deltaY must be use in bind() function.
Remember : You're user must update their system and browsers for security reasons. In 2018, the excuses of "I have IE 7" is a nonsense. We must educate users.
Have a good day :)
add a comment |
up vote
1
down vote
this code work fine with IE, Firefox, Opera and Chrome:
$(window).bind('wheel mousewheel', function(event) {
if (event.originalEvent.deltaY >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
'wheel mousewheel' and the property deltaY must be use in bind() function.
Remember : You're user must update their system and browsers for security reasons. In 2018, the excuses of "I have IE 7" is a nonsense. We must educate users.
Have a good day :)
add a comment |
up vote
1
down vote
up vote
1
down vote
this code work fine with IE, Firefox, Opera and Chrome:
$(window).bind('wheel mousewheel', function(event) {
if (event.originalEvent.deltaY >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
'wheel mousewheel' and the property deltaY must be use in bind() function.
Remember : You're user must update their system and browsers for security reasons. In 2018, the excuses of "I have IE 7" is a nonsense. We must educate users.
Have a good day :)
this code work fine with IE, Firefox, Opera and Chrome:
$(window).bind('wheel mousewheel', function(event) {
if (event.originalEvent.deltaY >= 0) {
console.log('Scroll up');
}
else {
console.log('Scroll down');
}
});
'wheel mousewheel' and the property deltaY must be use in bind() function.
Remember : You're user must update their system and browsers for security reasons. In 2018, the excuses of "I have IE 7" is a nonsense. We must educate users.
Have a good day :)
answered Oct 2 at 13:54
Cyril Morales
111
111
add a comment |
add a comment |
up vote
0
down vote
For those having problems with elastic scrolling, please use this answer
How to detect scroll direction
add a comment |
up vote
0
down vote
For those having problems with elastic scrolling, please use this answer
How to detect scroll direction
add a comment |
up vote
0
down vote
up vote
0
down vote
For those having problems with elastic scrolling, please use this answer
How to detect scroll direction
For those having problems with elastic scrolling, please use this answer
How to detect scroll direction
edited May 23 '17 at 12:18
Community♦
11
11
answered Nov 20 '13 at 7:48
Timothy Dalton
3961519
3961519
add a comment |
add a comment |
up vote
0
down vote
in the .data() of the element you can store a JSON and test values to launch events
{ top : 1,
first_top_event: function(){ ...},
second_top_event: function(){ ...},
third_top_event: function(){ ...},
scroll_down_event1: function(){ ...},
scroll_down_event2: function(){ ...}
}
add a comment |
up vote
0
down vote
in the .data() of the element you can store a JSON and test values to launch events
{ top : 1,
first_top_event: function(){ ...},
second_top_event: function(){ ...},
third_top_event: function(){ ...},
scroll_down_event1: function(){ ...},
scroll_down_event2: function(){ ...}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
in the .data() of the element you can store a JSON and test values to launch events
{ top : 1,
first_top_event: function(){ ...},
second_top_event: function(){ ...},
third_top_event: function(){ ...},
scroll_down_event1: function(){ ...},
scroll_down_event2: function(){ ...}
}
in the .data() of the element you can store a JSON and test values to launch events
{ top : 1,
first_top_event: function(){ ...},
second_top_event: function(){ ...},
third_top_event: function(){ ...},
scroll_down_event1: function(){ ...},
scroll_down_event2: function(){ ...}
}
answered Dec 30 '13 at 8:47
Spacefrog
111
111
add a comment |
add a comment |
up vote
0
down vote
This is simple and easy detection for when the user scrolls away from the top of the page and for when they return to the top.
$(window).scroll(function() {
if($(window).scrollTop() > 0) {
// User has scrolled
} else {
// User at top of page
}
});
add a comment |
up vote
0
down vote
This is simple and easy detection for when the user scrolls away from the top of the page and for when they return to the top.
$(window).scroll(function() {
if($(window).scrollTop() > 0) {
// User has scrolled
} else {
// User at top of page
}
});
add a comment |
up vote
0
down vote
up vote
0
down vote
This is simple and easy detection for when the user scrolls away from the top of the page and for when they return to the top.
$(window).scroll(function() {
if($(window).scrollTop() > 0) {
// User has scrolled
} else {
// User at top of page
}
});
This is simple and easy detection for when the user scrolls away from the top of the page and for when they return to the top.
$(window).scroll(function() {
if($(window).scrollTop() > 0) {
// User has scrolled
} else {
// User at top of page
}
});
answered Aug 4 '15 at 19:33
Kbam7
6613
6613
add a comment |
add a comment |
up vote
0
down vote
This is an optimal solution for detecting the direction just when the user end scrolling.
var currentScrollTop = 0 ;
$(window).bind('scroll', function () {
scrollTop = $(this).scrollTop();
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
if(scrollTop > currentScrollTop){
// downscroll code
$('.mfb-component--bl').addClass('mfbHide');
}else{
// upscroll code
$('.mfb-component--bl').removeClass('mfbHide');
}
currentScrollTop = scrollTop;
}, 250));
});
add a comment |
up vote
0
down vote
This is an optimal solution for detecting the direction just when the user end scrolling.
var currentScrollTop = 0 ;
$(window).bind('scroll', function () {
scrollTop = $(this).scrollTop();
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
if(scrollTop > currentScrollTop){
// downscroll code
$('.mfb-component--bl').addClass('mfbHide');
}else{
// upscroll code
$('.mfb-component--bl').removeClass('mfbHide');
}
currentScrollTop = scrollTop;
}, 250));
});
add a comment |
up vote
0
down vote
up vote
0
down vote
This is an optimal solution for detecting the direction just when the user end scrolling.
var currentScrollTop = 0 ;
$(window).bind('scroll', function () {
scrollTop = $(this).scrollTop();
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
if(scrollTop > currentScrollTop){
// downscroll code
$('.mfb-component--bl').addClass('mfbHide');
}else{
// upscroll code
$('.mfb-component--bl').removeClass('mfbHide');
}
currentScrollTop = scrollTop;
}, 250));
});
This is an optimal solution for detecting the direction just when the user end scrolling.
var currentScrollTop = 0 ;
$(window).bind('scroll', function () {
scrollTop = $(this).scrollTop();
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
if(scrollTop > currentScrollTop){
// downscroll code
$('.mfb-component--bl').addClass('mfbHide');
}else{
// upscroll code
$('.mfb-component--bl').removeClass('mfbHide');
}
currentScrollTop = scrollTop;
}, 250));
});
answered Oct 21 '15 at 16:24
Mahmoud
505616
505616
add a comment |
add a comment |
up vote
0
down vote
You Should try this
var scrl
$(window).scroll(function(){
if($(window).scrollTop() < scrl){
//some code while previous scroll
}else{
if($(window).scrollTop() > 200){
//scroll while downward
}else{//scroll while downward after some specific height
}
}
scrl = $(window).scrollTop();
});
add a comment |
up vote
0
down vote
You Should try this
var scrl
$(window).scroll(function(){
if($(window).scrollTop() < scrl){
//some code while previous scroll
}else{
if($(window).scrollTop() > 200){
//scroll while downward
}else{//scroll while downward after some specific height
}
}
scrl = $(window).scrollTop();
});
add a comment |
up vote
0
down vote
up vote
0
down vote
You Should try this
var scrl
$(window).scroll(function(){
if($(window).scrollTop() < scrl){
//some code while previous scroll
}else{
if($(window).scrollTop() > 200){
//scroll while downward
}else{//scroll while downward after some specific height
}
}
scrl = $(window).scrollTop();
});
You Should try this
var scrl
$(window).scroll(function(){
if($(window).scrollTop() < scrl){
//some code while previous scroll
}else{
if($(window).scrollTop() > 200){
//scroll while downward
}else{//scroll while downward after some specific height
}
}
scrl = $(window).scrollTop();
});
edited Aug 1 '17 at 11:21
Rob♦
23.4k115072
23.4k115072
answered Aug 1 '17 at 11:18
thakurdev
10115
10115
add a comment |
add a comment |
up vote
0
down vote
Why nobody use the event object returned by jQuery on scroll ?
$window.on('scroll', function (event) {
console.group('Scroll');
console.info('Scroll event:', event);
console.info('Position:', this.pageYOffset);
console.info('Direction:', event.originalEvent.dir); // Here is the direction
console.groupEnd();
});
I'm using chromium and I didn't checked on other browsers if they have the dir property.
add a comment |
up vote
0
down vote
Why nobody use the event object returned by jQuery on scroll ?
$window.on('scroll', function (event) {
console.group('Scroll');
console.info('Scroll event:', event);
console.info('Position:', this.pageYOffset);
console.info('Direction:', event.originalEvent.dir); // Here is the direction
console.groupEnd();
});
I'm using chromium and I didn't checked on other browsers if they have the dir property.
add a comment |
up vote
0
down vote
up vote
0
down vote
Why nobody use the event object returned by jQuery on scroll ?
$window.on('scroll', function (event) {
console.group('Scroll');
console.info('Scroll event:', event);
console.info('Position:', this.pageYOffset);
console.info('Direction:', event.originalEvent.dir); // Here is the direction
console.groupEnd();
});
I'm using chromium and I didn't checked on other browsers if they have the dir property.
Why nobody use the event object returned by jQuery on scroll ?
$window.on('scroll', function (event) {
console.group('Scroll');
console.info('Scroll event:', event);
console.info('Position:', this.pageYOffset);
console.info('Direction:', event.originalEvent.dir); // Here is the direction
console.groupEnd();
});
I'm using chromium and I didn't checked on other browsers if they have the dir property.
answered Sep 26 at 0:44
Jiab77
739
739
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4326845%2fhow-can-i-determine-the-direction-of-a-jquery-scroll-event%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Easiest to use the
wheelevent these days : stackoverflow.com/a/33334461/3168107.– Shikkediel
Oct 29 '15 at 11:41