Does anyone know how to detect a scroll-down event in javascript/jquery?
Many thanks.
Does anyone know how to detect a scroll-down event in javascript/jquery?
Many thanks.
DEMO: MY FIDDLE
You need to set unique Ids on the elements. When the element is at a certain point of the screen, run the scroll function and set it to stop at the next element's ID. Try this to detect scroll:
JS:
$(window).scroll(function() { //when window is scrolled
var target1_top = $('#target1').offset().top;
var window_top = $(window).scrollTop();
var diff = target1_top - window_top;
console.log(diff)
if (diff < 0) {// this you need to specify; the # of pixels you need for the auto scroll to occur
//
$('html, body').animate({
scrollTop: $("#target2").offset().top //scroll to this element when condition matches
}, 2000);
}
});
HTML:
<body>
<div id="target1">
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
target1<br>
</div>
<div id="target2">
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
target2<br>
</div>
<div id="target3">
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
target3<br>
</div>
</body>
Based on this: How can I determine the direction of a jQuery scroll event?
var lastScrollTop = 0;
var currDiv = $('#firstDiv');
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
currDiv = scrollToPrevDiv();
} else {
currDiv = scrollToNextDiv();
}
lastScrollTop = st;
});
This will save your currentDiv and allow the user to scroll up and down
function scrollToNextDiv() {
$("html").scrollTop(currDiv.next().scrollTop());
}
function scrollToPrevDiv() {
$("html").scrollTop(currDiv.prev().scrollTop());
}
You will want to update the current div if the user does something else like grab the scrollbar to move the page. If that happens you can do something like this:
function getDivLocations() {
var scrollLocations = [];
$('.myContentDivClass').each(function(i, div) {
scrollLocations.push($(div).scrollTop());
});
return scrollLocations;
}
Then with those div locations you can figure out which one is closest to the top and update it accordingly.
Clearly the selectors I used might change based on however you set up your html.
Good luck!
You can't do it with just HTML, you'll need to use JQuery or JavaScript. JQuery solution can look like this:
// This code is just to detect scroll event.
$(document).ready(function() {
$(window).scroll(function() {
// do whatever you need here.
});
});
If you don't want to use jQuery (which you might not do for a very simple HTML page), you can accomplish this using regular Javascript:
<script>
// This code is just to detect scroll event.
function scrollFunction() {
// do your stuff here;
}
window.onscroll = scrollFunction;
</script>
You mentioned that you wanted to do something when they scroll down the page - the onscroll event fires off scrolling in any direction, in either the x- or y-axis, so your function will be called any time they scroll.
If you really want it to only run your code when they scrolled down the page, you'd need to preserve the previous scroll position to compare against whenever onscroll gets called.