Use JS to determine the current screen size. We can use the Window matchMedia() method to match the specified size element, and then judge it with the current screen size.
Now we will introduce the JS method to determine the current screen size with a simple code example.
The code example is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JS判断屏幕大小示例</title> </head> <body> <script> if(window.matchMedia("(max-width: 767px)").matches){ document.write("这是一个移动设备。"); }else { document.write("这是平板电脑或台式电脑。"); } </script> </body> </html>
Here we write a screen size judgment to match the given maximum width size (767px) through the matchMedia method. When the obtained current screen, that is, the window size, is less than 767 pixels, it is determined that "this is a mobile device." On the contrary, when it is larger than 767 pixels, it is judged as "this is a tablet or desktop computer."
The judgment results are as follows:
When the current screen width is greater than 767px:
When the current screen width is less than 767px:
matchMedia() Returns a new MediaQueryList object, representing the parsed result of the specified media query string. The value of the matchMedia() method can be any characteristic of the CSS @media rule, such as min-height, min-width, orientation, etc.
The MediaQueryList object has the following two properties:
media: The content of the query statement.
matches: Used to detect query results. If the document matches the media query list, the value is true, otherwise it is false.
This article is an introduction to the JS method of judging screen size. It is simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to determine screen size in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software