0

I want to detect the top of '#select' element when scrolling. I have following codes:

$(document).ready(function(){
  $(window).scroll(function(){
    console.log($('#select').offset().top);
  });
});
<div id="select"></div>

But the thing it prints is not fixed during scrolling and I think it is the value of $(window).scrollTop(). when I put 'console.log ...' outside of '$(window)....' function everything works fine. What is the problem?

I found now the problem is from selector. In some cases when I changed the id it works fine, but for the 'select' id it not works and all the time it prints $(window).scrollTop() . Any one can help in this case? (Perhaps there is some problem with the css of '#select' element)

10
  • Syntax error, missing just about everything ! Commented Jan 14, 2015 at 22:51
  • 2
    And it seems to be fixed for me -> jsfiddle.net/8tj34c4w Commented Jan 14, 2015 at 22:52
  • @adeneo, I fixed the coding errors in the OP's question (based on your feedback) and the snippet is now running as expected, ;-) Commented Jan 14, 2015 at 23:10
  • @Marventus - Please do not edit the code of a question. See, How far can I refactor the code in someone else's question? and When is it appropriate to edit someone else's code? Commented Jan 14, 2015 at 23:55
  • Ooops!! It was an typing error here. My real code is the code you are thinking but the problem is not solved. Commented Jan 15, 2015 at 8:12

1 Answer 1

1

You are probably missing some brackets...

Try this...

$(document).ready(function(){
  $(window).scroll(function(){
    console.log($('#select').offset().top);
  });
});
Sign up to request clarification or add additional context in comments.

Comments

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.