0

My problem is the following: I would like to get x and y coordinates on window scroll, in order to determine whether if I'm into a determined section or not, to perform some effects.

I'm trying to do this with this code:

$(window).scroll(function(e) {
       console.log(e.pageX + " " + e.pageY);
    });

Obviously this is only an example. I've already calculated the precise coordinates of all my sections, and all I need are the current coordinates when the scroll event is triggered.

I don't know why, but with the code posted above, in the console, I get "undefined undefined".

If anyone can help me, I would appreciate it very much. Thanks in advance for the future answers.

  • possible duplicate of [How do I get the browser scroll position in jQuery?](http://stackoverflow.com/questions/10615845/how-do-i-get-the-browser-scroll-position-in-jquery) – nalply Aug 23 '14 at 20:05

2 Answers2

2

e.pageX and e.pageY will be set on mouse event not on scroll event. May be you can use scrollLeft and scrollTop to retrieve scroll position and use that to determine area.

var left = $(document).scrollLeft();
var top = $(document).scrollTop();
punitdam
  • 66
  • 3
1

You need to track $(window).scrollTop() value, like this:

$(document).scroll(function() {
    console.log($(window).scrollTop());
});

Docs: http://api.jquery.com/scrolltop/

Fiddle: http://jsfiddle.net/QvMLA/ (take a look at the console output)

If you know the scroll position from the top of the page, you can trigger actions for your sections. Good luck.

lesssugar
  • 15,486
  • 18
  • 65
  • 115