Vue mobile terminal cannot adapt
With the development of mobile Internet, more and more websites and applications are beginning to use mobile terminals as the main access method. In mobile development, how to adapt to different device resolutions has become an important issue. For Vue developers, mobile adaptation requires consideration of adaptation issues for different screen sizes, densities, and orientations.
The traditional adaptation method is achieved through media queries and rem units. The specific method is to first set up different style files for different screens, and then use the rem unit to scale the font and element size relative to the width of the root element. Mobile devices usually use high-definition screens with a Device Pixel Ratio (DPR) greater than 1. In order to ensure the display effect, you need to use the viewport to set the correct scaling ratio. Below is an example of an adaptation scheme based on rem units.
/* 设置用于计算 rem 值的根元素字体大小 */ html { font-size: 16px; } @media only screen and (min-device-width: 320px) and (max-device-width: 568px) { /* 针对 4 英寸屏幕设置样式 */ html { font-size: 14px; } } @media only screen and (min-device-width: 375px) and (max-device-width: 667px) { /* 针对 4.7 英寸屏幕设置样式 */ html { font-size: 16px; } } @media only screen and (min-device-width: 414px) and (max-device-width: 736px) { /* 针对 5.5 英寸屏幕设置样式 */ html { font-size: 18px; } } @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { /* 针对 9.7 英寸 iPad 屏幕设置样式 */ html { font-size: 24px; } }
As shown in the above code, media queries are used to set different font sizes of the root element according to the screen width of different devices, and then the rem unit is used to scale the element size relative to the width of the root element.
However, there are some problems with this traditional adaptation method. First, since rem is calculated relative to the root element font size, there may be scaling errors. Secondly, there may be some problems with the viewport scaling setting, which may cause some elements to display abnormally.
Therefore, in Vue mobile development, it is recommended to use flex layout as an adaptation solution. The advantage of using flex layout is that you can adapt to devices of different sizes by setting different flex properties. Typically, mobile adaptation is achieved through the following steps:
- Use the viewport to set the correct zoom ratio.
Add the following code in the head tag of the HTML file:
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
- Enable the flex layout feature.
You can use the sass-resources-loader plug-in in the Vue.js project to automatically enable the flex layout feature:
const path = require('path') module.exports = { css: { loaderOptions: { sass: { prependData: `@import "${path.resolve(__dirname, 'src/assets/scss/flex.scss')}";` }, }, }, }
Among them, flex.scss
file code As follows:
/* 开启 flex 布局特性 */ $flex: 1; $flex-use-strict: false; // 不使用严格模式,防止出现 flex-basis 百分比计算错误 @mixin flex($direction: row, $justify: center, $align: center) { display: flex; flex-direction: $direction; justify-content: $justify; align-items: $align; } @mixin align-self($align: center) { align-self: $align; } @mixin flex-wrap($wrap: wrap) { flex-wrap: $wrap; } .flex { flex: #{$flex}; } .flex-row { @include flex(row); } .flex-column { @include flex(column); } .flex-start { justify-content: flex-start; } .flex-end { justify-content: flex-end; } .flex-between { justify-content: space-between; } .flex-around { justify-content: space-around; } .flex-center { justify-content: center; align-items: center; } .flex-align-start { align-items: flex-start; } .flex-align-end { align-items: flex-end; } .flex-align-center { align-items: center; } .flex-wrap { @include flex-wrap; } .flex-self-start { @include align-self(flex-start); } .flex-self-end { @include align-self(flex-end); } .flex-self-center { @include align-self(center); } .flex-self-auto { @include align-self(auto); }
- Set the rem value according to the resolution of the design draft.
For example: Based on the iPhone 6/7/8 series (375x667), the design draft size is 750x1334. You can first set the font size of the root element to 100px, and then set the size of other elements in rem. Make settings.
html { font-size: 100px; } @media only screen and (max-width: 480px) { /* 750 x 1334 设计稿在 480 这个断点上相当于 375 x 667 */ html { font-size: 66.7px; } } @media only screen and (min-width: 481px) and (max-width: 767px) { /* 750 x 1334 设计稿在 768 这个断点上相当于 414 x 736 */ html { font-size: 110.94px; } } @media only screen and (min-width: 768px) { /* 750 x 1334 设计稿在 768 这个断点上相当于 768 x 1366 */ html { font-size: 153.6px; } }
After using the above steps to implement mobile adaptation, you can avoid using traditional media queries and rem to significantly scale the element size. In addition, the responsive flex layout is suitable for mobile devices with different resolutions and orientations, and can better adapt to the user's device.
The above is the detailed content of Vue mobile terminal cannot adapt. 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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

The advantages of React are its flexibility and efficiency, which are reflected in: 1) Component-based design improves code reusability; 2) Virtual DOM technology optimizes performance, especially when handling large amounts of data updates; 3) The rich ecosystem provides a large number of third-party libraries and tools. By understanding how React works and uses examples, you can master its core concepts and best practices to build an efficient, maintainable user interface.

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React's main functions include componentized thinking, state management and virtual DOM. 1) The idea of componentization allows splitting the UI into reusable parts to improve code readability and maintainability. 2) State management manages dynamic data through state and props, and changes trigger UI updates. 3) Virtual DOM optimization performance, update the UI through the calculation of the minimum operation of DOM replica in memory.
