0

#mydiv is a clickable box Div,a class .openDiv will be added if click on #mydiv.

if has class .openDiv

if($('#mydiv').hasClass('openDiv')){
 $(window).scrollTop(); //value is 300px
}

if just the page loaded (no .openDiv )

$(window).scrollTop(); //value is 200px

so my variable is like

if($('#mydiv').hasClass('openDiv')){
  thisTop =  $(window).scrollTop() - 100;
}else{
  thisTop =  $(window).scrollTop() 
}

as you can see I made it 100 different value hard coded. Is there a way to make it to calculate dynamically? Thanks!

0

2 Answers 2

1

Try this:

var div = $('#mydiv'), scrollTop = $(window).scrollTop();
thisTop = div.hasClass('openDiv') ? scrollTop - div.height() : scrollTop;

If #mydiv has the class openDiv then take away it's height from $(window).scrollTop(), otherwise just return $(window).scrollTop().

Sign up to request clarification or add additional context in comments.

Comments

1

In jQuery you can find the height of every object using the .height() method. I don't know where does the 100px come form but you can replace it by $('InsertASelectorHere').height()

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.