0

I'm trying to get the mouse position on mousescroll. I'm also trying to make this compatible with all browsers. So far I have the following javascript which listens for the mouse scroll event:

document.addEventListener('DOMMouseScroll', mouseScroll, false);
function mouseScroll(){
  alert( /* mouse position code here */ );
}

But I'm not sure how to pass the event into the function to then use something like e.pageX and also I'm not sure what event property to use.

2 Answers 2

3

Try this out:- http://jsfiddle.net/adiioo7/sFJ4r/

JS:-

document.addEventListener('scroll', mouseScroll, false);

function mouseScroll(e) {
    console.log(window.scrollY);
}

DOMMouseScroll is not supported on all browser set except Firefox. https://developer.mozilla.org/en-US/docs/DOM/DOM_event_reference/DOMMouseScroll

enter image description here

Also, if you are targeting all browsers you can use http://jsfiddle.net/adiioo7/sFJ4r/1/

window.onmousewheel=document.onmousewheel=mouseScroll;

if(document.addEventListener){
      document.addEventListener('DOMMouseScroll',mouseScroll,false);
 }

function mouseScroll(e) {
    console.log(e.pageY);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Like this :

document.addEventListener('DOMMouseScroll', mouseScroll, false);

function mouseScroll(e){

   // e.clientX or e.pageX is available here.

}

2 Comments

Ah that was more simple than I thought. Although is this fully browser compatible? I read somewhere about using DOMMouseScroll and mousewheel too? What is the most full proof method?
Here is a link that should help you ... sitepoint.com/html5-javascript-mouse-wheel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.