Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

502
Vistas
How to add or remove classes based on browser height resize using jquery?

I would like to know if there's a way to add or remove classes based on the browser's height. Right now, I am comparing a div against the browser height and adding a class to it if the browser's height is higher than the div height by doing this:

if (($(window).height()) > $(".sidebar").height()) {
    $("#widget-area").addClass("fixed");
} else {

}

This works since the class is being added when the condition is met. The problem is that if the user resizes the browser's height, the class that was added will keep added even if the condition is not met anymore.

Is there a way to listen to the browser's height and add or remove this class no matter if the browser is resized later on?

EDIT:

I know a lot of you might suggest doing this by media queries but I need this to be done using jQuery.

I have added the window on resize function as suggested. The problem is that the script will only run if the browser is resized. I need it to run as soon as the document is ready and if the browser is resized as well. Here is my code:

$(window).on('resize', function(){
    if (($(window).height()) > $(".sidebar").height()) {
        $("#widget-area").addClass("fixed");
    } else {

    }
});
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The CSS media queries is a good way to do this work. But if you want to use jQuery, you should run the code when window resize.

$(window).resize(function(){
  if ($(window).height() > $(".sidebar").height())
    $("#widget-area").addClass("fixed");
  else
    $("#widget-area").removeClass("fixed");
});

Also if you want to run code when page load, use .on() and add two event handler to it.

$(window).on('load resize', function(){
  if ($(window).height() > $(".sidebar").height())
    $("#widget-area").addClass("fixed");
  else
    $("#widget-area").removeClass("fixed");
});

See code result in demo

about 4 years ago · Santiago Trujillo Denunciar

0

Solution via javascript:

If .sidebar has a dynamic height, here is one approach using javascript:

function updateWidgetAreaClassList() {
    var widgetArea = document.getElementById('widget-area');
    var sideBar = document.getElementsByClassName('sidebar')[0];

    if (window.innerHeight > sideBar.offsetHeight) {
        widgetArea.classList.add('fixed');
    }

    else {
        widgetArea.classList.remove('fixed');
    }
}

window.addEventListener('resize', updateWidgetAreaClassList, false);

Solution via CSS @media query:

If .sidebar has a fixed height of 400px (for example), the CSS @media query would be the following:

@media screen and (min-height: 401px ) {

#widget-area { [... STYLES HERE...] }

}
about 4 years ago · Santiago Trujillo Denunciar

0

Create a function for manipulating your dom in your case add and remove classes. And call that funciton in document.ready function and window.resize function. See below a working example

http://codepen.io/harishgadiya/pen/VpNXmB

HTML

<div id="wrapper"></div>

css

#wrapper{
  height:300px;
  width:300px;
  background:#999
}
.red{
  background:red !important;

}

JS

function resize(){
  var windowHeight = $(window).height();
  var wrapperHeight = $('#wrapper').height();
  console.log(windowHeight , wrapperHeight)
  if(windowHeight > wrapperHeight) $('#wrapper').addClass("red");
  else $('#wrapper').removeClass("red");
}
$(document).ready(function(){
  resize()
});

$(window).resize(resize)
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda