Several methods for js to determine the horizontal and vertical screen viewport detection of mobile terminals

Several methods for js to determine the horizontal and vertical screen viewport detection of mobile terminals

1. How to obtain different viewports

// Get the visual viewport size (including vertical scroll bar)
let iw = window.innerWidth,
 ih = window.innerHeight;
console.log(iw, ih);

// Get the visual viewport size (the size of the content area, including the sidebar, window chrome, and borders for resizing the window)
let ow = window.outerWidth,
 oh = window.outerHeight;
console.log(ow, oh);

// Get the ideal viewport size of the screen, fixed value (screen resolution size)
let sw = window.screen.width,
 sh = window.screen.height;
console.log(sw, sh);

// Get the size of the browser's available window (including padding, but excluding vertical scroll bars, borders, and margins)
let aw = window.screen.availWidth,
 ah = window.screen.availHeight;
console.log(aw, ah);

// Including padding, scrollbars, borders and margins let dow = document.documentElement.offsetWidth,
 doh = document.documentElement.offsetHeight;
console.log(dow, doh);

// The minimum width and height required to fit all content in the viewport without using scrollbars let dsW = document.documentElement.scrollWidth,
 dsH = document.documentElement.scrollHeight;
console.log(dsW, dsH);

// Includes the element's padding, but not borders, margins, or vertical scrollbars let cw = document.documentElement.clientWidth,
 ch = document.documentElement.clientHeight;
console.log(cw, ch);

2. JavaScript detection of horizontal and vertical screen

// window.orientation: Get the screen rotation direction window.addEventListener('resize', () => {
 // Normal orientation or screen rotated 180 degrees if (window.orientation === 180 || window.orientation === 0) {
  console.log('vertical screen')
 }

 // The screen rotates 90 degrees clockwise or 90 degrees counterclockwise if (window.orientation === 90 || window.orientation === -90) {
  console.log('horizontal screen')
 }
});

3. CSS detection of horizontal and vertical screen

/*css detection of horizontal and vertical screen*/
@media screen and (orientation:portrait) {

 /* Vertical screen */
 #app {
  width: 100vw;
  height: 100vh;
  background: red;
 }
}

@media screen and (orientation:landscape) {

 /* Horizontal screen */
 #app {
  width: 50vw;
  height: 100vh;
  background: green;
 }
}

4. Meta tag attribute settings

<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

5. Meta tag attribute settings Set up the notch screen & bottom black bar

<meta name="viewport" content="viewport-fit=cover" />

Set the distance between the safe area and the border

/* When using the bottom fixed navigation bar, we need to set the padding value for them: */
body {
 padding-bottom: constant(safe-area-inset-bottom);
 padding-bottom: env(safe-area-inset-bottom);
}

Note: constant function is effective when iOS < 11.2, env is effective when iOS >= 11.2

This concludes this article about several methods of using js to determine the horizontal and vertical viewport detection on mobile terminals. For more relevant content on js horizontal and vertical viewport detection on mobile terminals, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use JS to determine the horizontal and vertical screen status of the mobile phone
  • JS code to detect horizontal and vertical screen on mobile terminal

<<:  How to use vs2019 for Linux remote development

>>:  MySQL index leftmost principle example code

Recommend

Detailed explanation of how to mount remote file systems via SSH on Linux

Features of SSHFS: Based on FUSE (the best usersp...

How to use a field in one table to update a field in another table in MySQL

1. Modify 1 column update student s, city c set s...

Detailed analysis of the MySQL slow log opening method and storage format

In development projects, we can monitor SQL with ...

Ubuntu 16.04 installation tutorial under VMware 12

This article shares with you the installation tut...

JS Easy to understand Function and Constructor

Table of contents 1. Overview 1.1 Creating a func...

Several methods of implementing carousel images in JS

Carousel The main idea is: In the large container...

td width problem when td cells are merged

In the following example, when the width of the td...

MySQL sorting Chinese details and examples

Detailed explanation of MySQL sorting Chinese cha...

The implementation of event binding this in React points to three methods

1. Arrow Function 1. Take advantage of the fact t...

Detailed tutorial on installing Spring boot applications on Linux systems

Unix/Linux Services systemd services Operation pr...

Some parameter descriptions of text input boxes in web design

<br />In general guestbooks, forums and othe...

JavaScript Canvas draws dynamic wireframe effect

This article shares the specific code of JavaScri...

Install MySQL in Ubuntu 18.04 (Graphical Tutorial)

Tip: The following operations are all performed u...

Scoring rules of YSlow, a webpage scoring plugin developed by Yahoo

YSlow is a page scoring plug-in developed by Yaho...

Implementation of Nginx+ModSecurity security module deployment

Table of contents 1. Download 2. Deployment 1.Ngi...