how to alert if user scrolls down the page?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

how to alert if user scrolls down the page?

Post by eshban »

Hi,

Can anyone please tell me that

" how to write a JavaScript function to pop up an alert() if a user scrolls down the webpage faster than a certain speed"

Looking forward for a urgent reply

Thanks
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: how to alert if user scrolls down the page?

Post by kaszu »

If some website would not allow me to scroll as fast as I want (even for T&C) I would leave immediately. But here it is:

Code: Select all

var max_px_per_second = 1000;
var last_pos = 0;
var prev_pos = null;

//Check every 250ms if user has scrolled more than max_px_per_second/4 pixels
setInterval(function () {
	var dif = Math.abs(last_pos - prev_pos);
	if (dif > max_px_per_second / 4) {
		alert('LIMITING USER IS EVIL AND REALLY SHOULDN\'T BE DONE!');
	}
	
	prev_pos = last_pos;
}, 250);

window.onscroll = function () {
    var pos = window.scrollY || window.pageYOffset || document.body.parentElement.scrollTop;
    //Save scroll position
    last_pos = pos;

    //Make sure popup doesn't appear after page refresh
    if (prev_pos === null) prev_pos = last_pos;
};
Can you tell us why you need this (since I wrote code for you)?
Post Reply