jQuery: Alert which item was scrolled when done scrolling

I use the following code to trigger an alert when a visitor is done scrolling.  

I want the alert to say which item the scrolling occurred on.

For example, "You are done scrolling item 1" or "You are done scrolling item 2"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<style type="text/css">
h1{text-align:center}
h2{text-align:center; text-size: 13px; padding: 0; margin: 22px 0 5px 0;}
.pane {margin: 0 auto; width: 100px; height: 70px; overflow: auto; background: #eedddd; border: 1px solid #ddcccc;}
</style>
</head>
<body>

<h1>When you scroll an item, an alert should tell you which item was scrolled.</h1>

<h2>Item 0</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<h2>Item 1</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<h2>Item 2</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<script type="text/javascript">
/*<![CDATA[*/

$('.pane').each(function(index) {
	$(this).scroll(function () {
		ScrollAlert();
	});	
});
var zxcTO;
function ScrollAlert()
{
	clearTimeout(zxcTO);
	zxcTO=setTimeout(function(){ alert('You are done scrolling item ' +  '?' ); },1000);
}
/*]]>*/
</script>

</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
Lukasz ChmielewskiConnect With a Mentor Commented:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<style type="text/css">
h1{text-align:center}
h2{text-align:center; text-size: 13px; padding: 0; margin: 22px 0 5px 0;}
.pane {margin: 0 auto; width: 100px; height: 70px; overflow: auto; background: #eedddd; border: 1px solid #ddcccc;}
</style>
</head>
<body>

<h1>When you scroll an item, an alert should tell you which item was scrolled.</h1>

<h2>Item 0</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<h2>Item 1</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<h2>Item 2</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<script type="text/javascript">
/*<![CDATA[*/

$('.pane').each(function(index) {
	$(this).scroll(function () {
		ScrollAlert(index);
	});	
});
var zxcTO;
function ScrollAlert(index)
{
	clearTimeout(zxcTO);
	zxcTO=setTimeout(function(){ alert('You are done scrolling item ' +  index ); },1000);
}
/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
Lukasz ChmielewskiCommented:
And some explanation: you pass the index value in the function after scrolling:

$('.pane').each(function(index) {
	$(this).scroll(function () {
		ScrollAlert(index);
	});	
});

Open in new window


then you just output the index in ScrollAlert function
The index is 0-based, so you would have to add +1
0
 
hankknightAuthor Commented:
Thanks, but please test this:
 - Set the delay to 5 seconds.
 - Scroll all three items.

With the code you provided, I only get an alert for the last item scrolled on.  If a person quickly scrolls all three items, they only get one alert.  They should get one alert for each item.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<style type="text/css">
h1{text-align:center}
h2{text-align:center; text-size: 13px; padding: 0; margin: 22px 0 5px 0;}
.pane {margin: 0 auto; width: 100px; height: 70px; overflow: auto; background: #eedddd; border: 1px solid #ddcccc;}
</style>
</head>
<body>

<h1>When you scroll an item, an alert should tell you which item was scrolled.</h1>

<h2>Item 0</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<h2>Item 1</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<h2>Item 2</h2>
<div class="pane">
DFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskj hdasjk adkslf jadslkj fadlks klFdsfjk hdasjkf hdasjk fhdsakj hfdask hdaskjl
</div>

<script type="text/javascript">
/*<![CDATA[*/

$('.pane').each(function(index) {
	$(this).scroll(function () {
		ScrollAlert(index);
	});	
});
var zxcTO;
function ScrollAlert(index)
{
	clearTimeout(zxcTO);
	zxcTO=setTimeout(function(){ alert('You are done scrolling item ' +  index ); },5000);
}
/*]]>*/
</script>

</body>
</html>

Open in new window

0
 
Lukasz ChmielewskiCommented:
Then you need to set the timeout to less - like 100 and not for the 5 seconds.
The ticking timer outputs the last message and on the other hand scroll function runs in a reatime so I doubt there is a direct way to do what you want.
0
 
Steve KrileConnect With a Mentor Commented:
$.scroll() is tricky.  It will be called in a lot of different scenarios.  Not just when someone clicks on the handle and moves the position of a page or page element.  It can be called when a cursor is placed in side an element and the user presses the arrow keys.  It can be called when a user with a mouse containing a scroll wheel positions their pointer over an element and flicks the wheel.

So, to capture as it happens, every scroll on any element is possible.  The question is, what are you trying to do with that information.  If your plan was to actually alert() people, that has the potential to be very annoying.  $.scroll is called at the BEGINNING of the scroll event.  If a person scrolls down a bit but doesn't release the mouse, the event will have triggered and the timer set to run (according to the code above).

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.