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

206
Visualizações
pure JS foundation breakpoint changes event listener

The foundation documentation use JQuery to listen to breakpoint changes

https://get.foundation/sites/docs/media-queries.html

$(window).on('changed.zf.mediaquery', function(event, newSize, oldSize) {
     if (
    (oldSize === "small" && newSize === "large") ||
    (oldSize === "small" && newSize === "xxlarge") ||
    (oldSize === "medium" && newSize === "large") ||
    (oldSize === "medium" && newSize === "xxlarge")
  ) {
    //my custom function
    displayFacets("desktop");
  } else if (
    (oldSize === "xxlarge" && newSize === "small") ||
    (oldSize === "large" && newSize === "small") ||
    (oldSize === "large" && newSize === "medium") ||
    (oldSize === "xxlarge" && newSize === "medium")
  ) {
    displayFacets("mobile");
  }
});

How to convert this in pure JS ?

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

0

It's not quite clear what exactly you have in mind. But you can achieve something like that with matchMedia. For example you can create an EventListener that reacts on whether a certain MediaQuery matches or not. See this example: https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/onchange

EDIT: ok after your changes to the example it can be seen that you actually only want to distinguish between mobile and desktop. You can implement this quite easily with the functions mentioned:

const mql = window.matchMedia('(max-width: 600px)');
mql.onchange = (e) => {
  if (e.matches) {
    /* the viewport is 600 pixels wide or less */
    displayFacets("mobile");
  } else {
    /* the viewport is more than than 600 pixels wide */
    displayFacets("desktop");
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Foundation is open-source so you can always look into their source code to figure out how they solved specific tasks.

The relevant part to your question can be found here: foundation.util.mediaQuery.js

What they do is to listen for the resize event of the window:

  _watcher() {
    $(window).off('resize.zf.mediaquery').on('resize.zf.mediaquery', () => {
      var newSize = this._getCurrentSize(), currentSize = this.current;

      if (newSize !== currentSize) {
        // Change the current media query
        this.current = newSize;

        // Broadcast the media query change on the window
        $(window).trigger('changed.zf.mediaquery', [newSize, currentSize]);
      }
    });
  }

and check which media query is currently matching by using window.matchMedia(…).matches:

  _getCurrentSize() {
    var matched;

    for (var i = 0; i < this.queries.length; i++) {
      var query = this.queries[i];

      if (window.matchMedia(query.value).matches) {
        matched = query;
      }
    }

    return matched && this._getQueryName(matched);
  },

Replacing the on, off and trigger in these parts of the code should be straightforward. Extracting the breakpoints might be a bit more problematic but that is also in that file in the _init function.

Alternatively you can also use MediaQueryList.onchange:

var mql = window.matchMedia('(max-width: 600px)');

mql.onchange = (e) => {
    if (e.matches) {
    /* the viewport is 600 pixels wide or less */
    console.log('This is a narrow screen — less than 600px wide.')
  } else {
    /* the viewport is more than than 600 pixels wide */
    console.log('This is a wide screen — more than 600px wide.')
  }
}

To get an event if a media query matches or not.

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