[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jQuery: Alert which item was scrolled when done scrolling

Posted on 2011-04-22
5
Medium Priority
?
338 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:hankknight
  • 3
5 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 1600 total points
ID: 35446944
<!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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35446950
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
 
LVL 16

Author Comment

by:hankknight
ID: 35446957
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35446978
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
 
LVL 16

Assisted Solution

by:Steve Krile
Steve Krile earned 400 total points
ID: 35447030
$.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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question