简体   繁体   中英

How to get window width in javascript to match the css media?

In css I have this

@media screen and (max-width: 860px) {

}

what would be the equivalent javascript code to get the same width value that the css uses for @media max-width?

Thanks

I believe that there is no way to get CSS from JavaScript code without parsing CSS file. JavaScript has a similar method is matchMedia . Usage:

if (window.matchMedia("(min-width: 400px)").matches) {
  /* the viewport is at least 400 pixels wide */
} else {
  /* the viewport is less than 400 pixels wide */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM