JS Screen Object Tutorial 📝🎯

beginner
7 min

JS Screen Object Tutorial 📝🎯

Welcome to the JavaScript (JS) Screen Object tutorial! In this lesson, we'll dive into the world of manipulating the browser window and screen using JavaScript. Let's get started!

Understanding the Screen Object 💡

The Screen object in JavaScript provides information about the user's screen size. It's a built-in object that allows us to access and modify properties related to the screen.

Properties of the Screen Object 📝

The Screen object has several properties that provide information about the screen:

  • availWidth: Returns the width of the screen available for displaying content, excluding toolbars and scrollbars.
  • availHeight: Returns the height of the screen available for displaying content, excluding toolbars and scrollbars.
  • height: Returns the total height of the screen, including toolbars and other areas.
  • width: Returns the total width of the screen, including toolbars and other areas.
  • colorDepth: Returns the color depth of the screen.

Accessing the Screen Object 💡

To access the Screen object, we simply use the screen keyword. Here's an example of accessing the screen width:

javascript
console.log(screen.width);

Manipulating the Screen 💡

While we can't actually change the screen size using the Screen object, we can use it to determine the optimal layout for our web applications based on the user's screen size.

Quiz 🎯

Quick Quiz
Question 1 of 1

Which property of the `Screen` object returns the width of the screen available for displaying content?

Let's move on to some practical examples!


Example 1 - Responsive Design 💡

In this example, we'll create a responsive design that adjusts the layout based on the screen size.

javascript
// Get the header and content elements const header = document.querySelector('header'); const content = document.querySelector('main'); // Function to handle screen resizing function handleScreenResize() { // If the screen is less than 600 pixels wide... if (screen.availWidth < 600) { // Hide the header and make the content full width header.style.display = 'none'; content.style.width = '100%'; } else { // Show the header and reset the content width header.style.display = 'block'; content.style.width = 'auto'; } } // Call the handleScreenResize function on page load and on screen resize window.onload = handleScreenResize; window.onresize = handleScreenResize;

In this example, we use the screen.availWidth property to check the screen size and adjust the layout accordingly. This is a simple but effective way to create a responsive design using JavaScript.


Example 2 - Creating a Full-Screen Button 💡

In this example, we'll create a button that makes the browser window full screen when clicked.

javascript
// Get the fullscreen button const fullscreenButton = document.querySelector('#fullscreen-button'); // Function to handle the fullscreen button click function handleFullscreenButtonClick() { // Request fullscreen mode document.documentElement.requestFullscreen(); } // Attach the click event listener to the fullscreen button fullscreenButton.addEventListener('click', handleFullscreenButtonClick);

In this example, we use the document.documentElement.requestFullscreen() method to request fullscreen mode when the fullscreen button is clicked.


That's it for this lesson! We've covered the basics of the Screen object in JavaScript, including its properties and how to manipulate the browser window. Happy coding! 🎯💡