Media queries are a powerful CSS feature used to apply styles based on the characteristics of the user's device, such as screen width, height, or resolution. JavaScript enhances the utility of media queries by enabling dynamic responses to changes in these conditions, providing greater control over how applications behave across different devices.
Media queries allow developers to define CSS rules that are applied conditionally, based on the properties of the user's device or viewport.
Basic Syntax in CSS:
@media (max-width: 768px) { body { background-color: lightblue; } }
With JavaScript, you can dynamically interact with these media queries using the Window.matchMedia() method.
The Window.matchMedia() method enables you to test a media query programmatically and respond to changes in real-time.
Syntax:
const mediaQuery = window.matchMedia("(max-width: 768px)");
if (mediaQuery.matches) { console.log("Viewport is 768px or smaller."); } else { console.log("Viewport is larger than 768px."); }
You can use the addEventListener method to listen for changes in the media query state.
Example:
const mediaQuery = window.matchMedia("(max-width: 768px)"); function handleMediaQueryChange(e) { if (e.matches) { console.log("Now in mobile view."); } else { console.log("Now in desktop view."); } } // Attach listener mediaQuery.addEventListener("change", handleMediaQueryChange);
This approach dynamically responds to viewport changes without requiring a page reload.
Change JavaScript-driven layouts based on screen size.
const mediaQuery = window.matchMedia("(max-width: 768px)"); function adjustLayout() { if (mediaQuery.matches) { document.body.style.backgroundColor = "lightblue"; } else { document.body.style.backgroundColor = "white"; } } // Initial check and listener adjustLayout(); mediaQuery.addEventListener("change", adjustLayout);
Load or execute scripts only when specific conditions are met.
const mediaQuery = window.matchMedia("(max-width: 768px)"); if (mediaQuery.matches) { // Load mobile-specific script import("./mobile.js").then(module => { module.init(); }); }
Disable animations on smaller devices to improve performance.
@media (max-width: 768px) { body { background-color: lightblue; } }
Most modern browsers fully support the Window.matchMedia() API. However, older browsers may require polyfills for the addEventListener method on MediaQueryList.
Here’s a practical example of combining media queries and JavaScript to manage a responsive menu.
const mediaQuery = window.matchMedia("(max-width: 768px)");
By combining JavaScript and media queries, you can achieve highly dynamic and responsive designs. This approach enables real-time adaptations to user preferences and device characteristics, enhancing both performance and user experience. Mastering these techniques is essential for building modern, device-friendly web applications.
Hi, I'm Abhay Singh Kathayat!
**I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: **kaashshorts28@gmail.com.
The above is the detailed content of JavaScript and Media Queries: Building Responsive and Dynamic Web Applications. For more information, please follow other related articles on the PHP Chinese website!