0

I would like to detect when the user scrolls past a certain point in the window.

[Meta]
X
scroll below = alert() 

I'm quite new to CoffeeScript, so any help would be great!

What I currently have is this:

$("#area_1").bind('scroll', ->
  scrollPosition = $(this).scrollTop() + $(this).outerHeight()
  divTotalHeight = $(this)[0].scrollHeight()
  if (scrollPosition >= divTotalHeight)
    alert('end reached')
  )

And no alert is showing up. Any tips?

Update I suspect that my problem have to do with my two variables. Here is an example:

$("#area_1").bind($(window).scroll ->
  alert('before 1') # Gets called
  scrollPosition = $(this).scrollTop() + $(this).outerHeight()
  divTotalHeight = $(this)[0].scrollHeight()
  alert('before 2') # Never gets called
  if (1 == 1)
    alert('should get called but dosn't')
)
Anders
  • 2,903
  • 7
  • 58
  • 114
  • The coffeescript part seems OK, did you try printing out scrollPosition and divTotalHeight or setting a breakpoint there? – arghbleargh Aug 17 '13 at 18:49
  • Does `#area_1` scroll or does the whole `window`? – mu is too short Aug 17 '13 at 19:07
  • @muistooshort the whole window. So when X (in the question) gets "invisible" then I want the alert. Or it could also be when another div below X becomes visible, if thats easier. – Anders Aug 17 '13 at 19:46
  • @Anders, do you get any error in the console? – Dogbert Aug 17 '13 at 20:16
  • Have you tried binding to `$(window).scroll ->` instead? – mu is too short Aug 17 '13 at 20:16
  • @muistooshort It seems like the code inside the if statement never gets called for some reason. I did bind to `$(window).scroll ->` instead, added an `alert` message before the if statement. That gets called. But even if I make sure that the condition in the if statement is true `(1 == 1)`, the code inside never gets called. – Anders Aug 17 '13 at 20:36
  • @Dogbert, yes. No error what I can see. – Anders Aug 17 '13 at 20:38
  • I have added an update to my question. – Anders Aug 17 '13 at 20:48
  • I meant `$(window).scroll -> ...` instead of `$('#area_1').bind('scroll', -> ...`. And [`scrollHeight` is a property, not a function](http://stackoverflow.com/a/6853354/479863). – mu is too short Aug 17 '13 at 21:00
  • @muistooshort Got it to work using `$(window).scroll ->` instead. – Anders Aug 18 '13 at 07:49

0 Answers0