Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
Run script when window is bigger than x, also when window is resized

I have a jQuery script that I want to run only if the window is wider than 1245px.

if ($(window).width() > 1245) { 
    // script
}

This is for responsive reasons. But let's say I am on desktop and start with a window smaller than 1245px; I want to be able to resize it and run that function and vise versa if I start with a larger window.

I have tried using 'resize' like this:

$(window).on('resize', function(){
    var win = $(this); // this = window
    if (win.height() > 1245) { /* ... */ }
}

But I run into problems with the script not running at the initial window size, what's the best way to approach this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Also add load to the list of events:

$(window).on('load resize', ...

Be advised that resize event is fired continuously while window is being resized so for example if you resize it from 1000 to 1600px in one second it would fire 600 times. Using any expensive function will bog down the browser. I recommend using "debouncing" trick for resize events.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda