473,408 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Event after scrolling

Hello everybody,

I have one problem. I want to do something after the user finished
scrolling. The scroll event fires whenever the user is scrolling. I
don't want this actually. Does anyone has any idea or trick of how to
achieve this? Appreciate your ideas.....

Thanks
Chamnap

Jun 13 '07 #1
3 11703
On Jun 13, 8:12 pm, Chamnap <chamnapchh...@gmail.comwrote:
Hello everybody,

I have one problem. I want to do something after the user finished
scrolling.
How do you define "finished scrolling"? Is it a period of say 0.5
seconds of no scrolling after the last scroll event? How do you know
the user isn't about to start scrolling again?

--
Rob
Jun 13 '07 #2
Chamnap wrote:

Hi,
I have one problem. I want to do something after the user finished
scrolling. The scroll event fires whenever the user is scrolling. I
don't want this actually. Does anyone has any idea or trick of how to
achieve this? Appreciate your ideas.....
As Rob has pointed out, defining a scroll-stop event is not that an
obvious task, especially in regards of user's scrolling behaviors: there
is a risk that your users would end up being confused, especially if
they're used to scrolling in a slow fashion.

However, if you can define some acceptable delay, after which, no scroll
having occurred, you may consider the user has stopped scrolling, then
you can perform the task.

There are two main approaches. The first one consists in setting up a
timer, using the setInterval host method, and watch regularly scroll
offsets (scrollTop, scrollLeft). The second one consists in launching
and stopping the process directly after the study of the scroll event -
see below (tested IE7, FF2 on Windows).
---
<head>
<title>doAfterScroll Example</title>
<style type="text/css">body div {height:2000px;padding:100px}</style>

<!-- the following script part
can be externalized in some javascript file -->
<script type="text/javascript">
var doAfterScroll = (function(){

// Configure
var SCROLL_DELAY = 1000 ;

// Do not edit the following
var
baseId = 0 ,
scrollId = 0 ,
currentlyScrolling = false ,
funcs = [] ;

// --- Init ---
addEvent(
window,
"onscroll",
function(evt){
// update the scroll identifier
scrollId = new Date().getTime();

if(!currentlyScrolling) { //Scroll has just started

// set the info
currentlyScrolling=true;

// Launch the watch process
setTimeout(
function() {
if(scrollId==baseId) { // Scrolling has stopped

// set the info
currentlyScrolling = false;

// Execute the registered handlers
for(var ii=funcs.length; ii--; ) funcs[ii]() ;

} else { // Still scrolling
// set a milestone
baseId=scrollId ;

// wait
setTimeout(
arguments.callee,
SCROLL_DELAY
);
}
},
SCROLL_DELAY
);

} else {
// a process has already been launched
// and is waiting to be completed
// - Do nothing
}

}
);

// --- return the registering function ---
return function (func) { funcs.push(func); }

// --- Helpers ---
function addEvent(obj, evt, func){
if(obj[evt]) {
obj[evt]=(function(handler){
return function(evt){
func.call(this, evt);
return handler.call(this, evt);
}
})(obj[evt]);
} else {
obj[evt]=func;
}
}
})();
</script>

<!-- Test -->
<script type="text/javascript">
window.onload = function(evt){
doAfterScroll(
function(){
document.body.firstChild.innerHTML = "Scrolled at "+new Date() ;
}
);
}
</script>
</head>
<body><div>Hello, World !</div></body>
---
HTH,
Elegie.
Jun 13 '07 #3
dd
On Jun 13, 12:12 pm, Chamnap <chamnapchh...@gmail.comwrote:
I have one problem. I want to do something after the user finished
scrolling. The scroll event fires whenever the user is scrolling. I
don't want this actually. Does anyone has any idea or trick of how to
achieve this? Appreciate your ideas.....
Your scroll event handler should do a setTimeout call
to a final scroll handler. If you decide that the last
scroll timer should be half a second, then set the time
to 500 (ms). Each time your scroll event handler is
called, it clears the current timer and starts it new.
As long as you keep getting scroll events, you'll keep
stopping and restarting the timer. Only when they finally
leave it alone for 500ms will the setTimeout actually get
to call your real event handler.

If you want to stop the scrolling from happening then
you'll need to cancel the event from bubbling to it's
normal default handler. So your scroll event handler
would look something like this:

function ScrollEventHandler(e){
clearTimeout(myScrollTimer);
myScrollTimer=setTimeout(FinalScrollEventHandler,5 00);
//cancel the event if desired using
//something like this below
if(window.event){
window.event.returnValue=false;
window.event.cancelBubble=true;
}
else if(e&&e.preventDefault&&e.stopPropagation){
e.preventDefault();
e.stopPropagation();
}
}

function FinalScrollEventHandler(){
//OK they finally let go of the scrollbar
//let's do some stuff now
}

Of course Safari won't like this - it's too complicated
for it. Sure it'll run the code and appear to have worked
but it won't cancel the event.

I haven't tried using this method on scroll events,
so no guarantees here, but I have done it with link
clicked events (i.e. you clicked on a link). You can
use it to intercept any link on a page and warn users
that they're about to navigate to a link in another
domain for example.

Jun 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how...
7
by: Pete | last post by:
I'm working (playing) on a mouse following script. Yes, the sort no one likes but I'm having great fun tinkering with it - sad. Anyway, if there's enough page content to cause scrolling and I...
1
by: Arvind | last post by:
Hi I edit a cell in a DataGridView, and then would move to another cell in the DataGridView using mouse. Then the DataGridView as of now does scroll down to the last visited cell. But when I try...
4
by: peppeprof | last post by:
Hi- I need to use self.scrollTo(x,y) as the last action in a js function called by a onClick event. It seems that the scrolling actually takes place, but the document is then scrolling back to...
3
by: Vikram Bhatia | last post by:
1. Is there an event to capture scrolling using mouse wheel in Netscape 6.x? 2. When arrow keys are used to scroll a page in Netscape 6.x, the scrolling offsets obtained using...
2
by: Phil Galey | last post by:
I have a Panel control docked on all sides on a form and the panel control contains a PictureBox. I'm using the KeyDown event of the form to respond to the and keys for resizing the image and the...
3
by: Brian Tkatch | last post by:
I have a form with two DataGrids, which are kept in sync manually via Stored PROCEDURE calls. That is, when a record is selected on the first grid, a stored PROCEDURE is CALLed to Fill() the next...
3
by: j0rd4n | last post by:
I have a user control that needs to allow vertical scrolling but not horizontal scrolling. In the user control's resize event, I need to adjust the size of all the children controls. For this to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.