1

Hi all I'm trying to test the size of a window is less than 800px whilst also checking scroll but it's not working I'm sure I just have the syntax incorrect (newbie here).

Here is what I have...

if ($(window).scrollTop() >= origOffsetY && $(window).width() < 801) {
// do something
}

Any ideas?

4
  • did you try put what is either side of the && in brackets? Commented Jan 15, 2014 at 9:48
  • 1
    The syntax is fine. You need to debug this and see what your three dynamic values contain Commented Jan 15, 2014 at 9:49
  • 2
    @Sionnach733: There's absolutely no reason to do that with the above. Commented Jan 15, 2014 at 9:49
  • 1
    @ Mark: jQuery is a library, not a language. JavaScript is the language you're using. Commented Jan 15, 2014 at 9:49

2 Answers 2

1

That syntax is correct. If it's not working, it's because the values aren't what you expect, not because you have the syntax wrong.

Use the debugger built into your browser to figure it out. To make that easier, you can put things in variables first:

var $win = $(window);
var scrollTop = $win.scrollTop();
var width = $win.width();
if (scrollTop >= origOffsetY && width < 801) {
// do something
}

Now you can put a breakpoint on the if statement, and inspect the values of the scrollTop and width and origOffsetY variables. Or alternately, add console.log statements to dump them out (useful for when stopping the script with a breakpoint is awkward).

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

1 Comment

Hi @T.J. Crowder thanks for your great tip on the variables, its something I didn't realise you could do! I did what you said with console log and noticed something I told it to tell me what width is set as and for some reason even though when I am changing the width of the page and with chrome dev tools open the top right shows say 800px the console shows a higher number. For instance when console says width is 800 chrome tools say the window is actually 739px. Not sure why this is but I'll have a look around. But thanks for help TJ :)
0

Try this:

if ($("body").height() > $(window).height() &&  $(window).width() < 800) {
     //your code
}

2 Comments

"Try this" followed by a code dump is not useful. Say what you changed, and why.
Hiya @Era took me a while to figure out how this works (newbie) but I get it and yes it seems a neat way of doing what I am after. Hope you dont mind I set TJs as the solved as his answer helped me work out what was actually going on :)

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.