0

Using the "resizing" event listener I'd like to check if a browserwidth is getting smaller or larger.

I can use window.innerWidth to get the current width of the browser, but how can I check if the value is getting bigger or smaller than the previous one?

MFA86
  • 185
  • 1
  • 1
  • 9
  • Does this answer your question? [Detect when a window is resized using JavaScript ?](https://stackoverflow.com/questions/2996431/detect-when-a-window-is-resized-using-javascript) – Samball Mar 14 '22 at 12:05
  • 1
    _"but how can I check if the value is getting bigger or smaller than the previous one?"_ - sounds like you would possibly have to "remember" that previous value then, doesn't it? And you know what's good for "remembering" values inside of a script? Variables! – CBroe Mar 14 '22 at 12:08
  • @CBroe Yes I could return a variable, but that gets overwritten each time I'm resizing the window again. How can I store the previous value, so I can compare it with the current one? – MFA86 Mar 14 '22 at 12:14
  • Well you access it for the comparison, _before_ you overwrite it. You need to put it into a scope outside of the resize handler callback function, initialize it there with `var previousWith = (current window width);` Inside your resize handler, you check `(current window width) > previousWith` first, _before_ you overwrite the variable again with the current width, `previousWith = (current window width);` (no `var` keyword in that place, because you want to access the variable from the outer scope, not create a local one.) – CBroe Mar 14 '22 at 12:19

1 Answers1

1
window.addEventListener('resize', (size) => {
 // Code here
});
narF
  • 70
  • 4
  • You haven't addressed the main point of the question *"how can I check if the value is getting bigger or smaller than the previous one?"* – Felix Kling Mar 14 '22 at 12:46