I have a different function I want to run each time a user scrolls to another div or to another section of the page. Is there anyway I can do that?
Asked
Active
Viewed 1.5k times
6
-
Are those section located below each other? – patrick Dec 31 '13 at 04:25
2 Answers
8
Allready Answered here -->> Dynamic Scroll Position in Jquery
There is a great plugin to do this called WAYPOINT
I have setup an example on jsfiddle check it out
HTML
<div class="first"></div>
<div class="second"></div>
CSS
.first {
background:green;
height: 600px;
width:100%;
}
.second {
background:red;
height: 600px;
width:100%;
}
JS
$('.second').waypoint(function() {
alert('You have scrolled to an entry.');
}, {
offset: '100%'
});

Community
- 1
- 1

Vikas Ghodke
- 6,602
- 5
- 27
- 38
-
hmm I'm trying waypoint but when I scroll to the div the function does not run – kduan Dec 31 '13 at 13:30
-
Mine is not also working.... :( What did you do to make it work @kduan – sphoenix Mar 20 '18 at 06:59
2
There're many ways to do this. The most simplest method:
var element = $('#my-div');
function myFunction() {
alert('I am here!');
}
$(window).scroll(function(){
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
if ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)) {
myFunction();
}
});
Also there're jQuery Appear plugin and Waypoints plugin:
$('#my-div').appear(function() {
alert('I am here!');
});
$('#my-div').waypoint(function() {
alert('I am here!');
});

emerkulova
- 182
- 5