Page 1 of 1

how to alert if user scrolls down the page?

Posted: Wed Apr 28, 2010 2:21 am
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

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

Posted: Wed Apr 28, 2010 12:17 pm
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)?