How do I detect horizontal scrolling with jQuery?
This will get all scrolls:
$(window).scroll(function () {
alert('in');
});
I want just the horizontal one.
How do I detect horizontal scrolling with jQuery?
This will get all scrolls:
$(window).scroll(function () {
alert('in');
});
I want just the horizontal one.
This seems to work.
var lastScrollLeft = 0;
$(window).scroll(function() {
var documentScrollLeft = $(document).scrollLeft();
if (lastScrollLeft != documentScrollLeft) {
console.log('scroll x');
lastScrollLeft = documentScrollLeft;
}
});
An update that shows direction left or right in a horizontal scroll based on code above:
var lastPos = 0;
$(window).scroll(function() {
var currPos = $(document).scrollLeft();
if (lastPos < currPos) {
console.log('scroll right');
}
if (lastPos > currPos)
{
console.log('scroll left');
}
lastPos = currPos;
});
Test it at http://jsfiddle.net/EsPk7/7/
dunderwood's jsFiddle link didn't actually contain his code. I've forked his jsFiddle with what a mixture of what he posted here and what's on the jsFiddle:
var lastPos = 0;
$(window).scroll(function() {
var currPos = $(document).scrollLeft();
if (lastPos < currPos) {
$('#current').html('Right');
}
if (lastPos > currPos) {
$('#current').html('Left');
}
lastPos = currPos;
});
window.onresize = function() {
if ( $("div").outerWidth() < $("div").get(0).scrollWidth ) {
console.log("foo bar scrollbar");
} else {
console.log(" no scrolling ");
}
};