Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!