Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Probando media queries

Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes.

El DOM proporciona características que hacen posible probar los resultados de un media query estructuradamente. Esto es hecho usando la interfaz MediaQueryList y sus métodos y propiedades. Una vez que hayas creado el objeto MediaQueryList, puedes revisar el resultado del query o, como alternativa, recibir notificaciones automáticamente cuando el resultado cambie.

Creando una media query list

Before you can evaluate the results of a query, you need to create the MediaQueryList object representing the media query. To do this, use the window.matchMedia method.

For example, if you want to set up a query list that determines whether the device is in landscape or portrait orientation, you can do so like this:

var mql = window.matchMedia("(orientation: portrait)");

Revisando el resultado de un query

Once your media query list has been created, you can check the result of the query by looking at the value of its matches property, which reflects the result of the query:

if (mql.matches) {
  /* The device is currently in portrait orientation */
} else {
  /* The device is currently in landscape orientation */
}

Recibiendo notificaciones query

If you need to be aware of changes to the evaluated result of the query on an ongoing basis, it's more efficient to register a listener than to poll the query's result. To do this, you can call the addListener() method on the MediaQueryList object, specifying an observer that implements the MediaQueryListListener interface:

var mql = window.matchMedia("(orientation: portrait)");
mql.addListener(handleOrientationChange);
handleOrientationChange(mql); 

This code creates the orientation testing media query list, mql, then adds a listener to it. Note that after adding the listener, we actually invoke the listener directly once. This lets our listener perform initial adjustments based on the current device orientation (otherwise, if our code assumes the device is in portrait mode but it's actually in landscape mode at startup, we could have inconsistencies).

The handleOrientationChange() method we implement then would look at the result of the query and handle whatever we need to do on an orientation change:

function handleOrientationChange(mql) {
  if (mql.matches) {
    /* The device is currently in portrait orientation */
  } else {
    /* The device is currently in landscape orientation */
  }
}

Terminando con las notificaciones query 

Cuando ya no vayas a necesitar recibir las notificaciones sobre los cambios de valro de tu media query, simplemente llama al removeListener() en el MediaQueryList:

mql.removeListener(handleOrientationChange);

Compatibilidad con los navegadores

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Soporte básico 9 6.0 (6.0) 10 12.1 5.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Soporte básico 3.0 ? 10 12.1 5

Ver también

Etiquetas y colaboradores del documento

 Colaboradores en esta página: TibicenasDesign
 Última actualización por: TibicenasDesign,