user1365010
user1365010

Reputation: 3343

Mouse position onscroll

This is working : http://jsfiddle.net/9NDXF/1/ when I write onmousemove, but this is not : http://jsfiddle.net/9NDXF/7/ when I write onscroll.

How can I have the mouse position onscroll ?

Upvotes: 0

Views: 788

Answers (3)

Sinetheta
Sinetheta

Reputation: 9429

Because the scroll event has no pageX property. Scrolls suck. Here's a nice article on mouse scroll events, and a jsFiddle of the demo.

The only way to "get the mouse position on scroll" is to have recorded it at some earlier time, then access it when needed. I would put a lot of thought into why you think this is a good idea first though: jsFiddle

var mousePostion = {clientX:0,clientY:0};

function record(e)
{
    mousePostion = {
        clientX: e.clientX,
        clientY: e.clientY
    };
}
function print(e)
{
   document.getElementById("x").value=mousePostion.clientX;
   document.getElementById("y").value=mousePostion.clientY;
}
window.onmousemove=record;
window.onscroll=print;​

Upvotes: 1

shareef
shareef

Reputation: 9581

read this refernence hope it help
http://javascriptmagic.blogspot.com/2006/09/getting-scrolling-position-using.html

Upvotes: 0

Jonathan Payne
Jonathan Payne

Reputation: 2223

onscroll is when the user scrolls the screen. minimize the jsFiddle so there's a scroll bar, and then scroll with it. You'll see values populated. "e" is undefined.

function test(e)
{
    alert(e);

    var doc=document.documentElement;
    var body=document.body;
    if(e.pageX)
    {
        document.getElementById("x").value=e.pageX;
        document.getElementById("y").value=e.pageY;
    }
    else
    {
        document.getElementById("x").value=e.clientX+(doc&&doc.scrollLeft||body&&body.scrollLeft||0)-(doc&&doc.clientLeft||body&&body.clientLeft||0);
        document.getElementById("y").value=e.clientY+(doc&&doc.scrollTop||body&&body.scrollTop||0)-(doc&&doc.clientTop||body&&body.clientTop||0);
    }
}
window.onscroll=test;​

Upvotes: 0

Related Questions