This article brings you relevant knowledge about vue. It mainly introduces the detailed explanation of Vue instructions to implement resolution adaptation of large screen elements, including common adaptation schemes and css scaling schemes, etc. Wait, let’s take a look at it together, I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, vue.js tutorial】
With front-end technology With the continuous development of data center (middle platform) and other concepts, and the update and popularization of Internet of Things devices, more and more owners (projects) like to add one or more large visual screens to the system for Centrally display data changes, location changes, etc. Bosses also prefer to call it "situation".
Of course, programmers generally don’t care what the “bosses” think, as long as they complete the project. But there is often a problem like this: I have a large-screen template, but the user's browser resolution is not enough, or some have a bookmark bar and some don't, or some are full screen and some are just small windows. In this way, there are requirements for the code to adapt to different resolution scenarios.
The web-side adaptation solutions we usually use mainly include the following:
The principles of most current screen adaptation solutions are based on the above methods, but these methods also have great disadvantages:Browser text has a minimum size!
On ordinary screens with a resolution of 1080p and above, the proportions and display effects of most design drawings can be perfectly restored. However, if there is too much page content in a certain system, or the resolution used by the browser (not the physical resolution) does not meet the full display requirements, using the above methods may cause the calculated size of text If it is smaller than the browser's minimum font size, the page style may collapse because the text width exceeds the element.
The center layout and the minimum width can ensure the display effect, but it is not suitable for large-screen projects.
When the above solutions are not satisfied, everyone will generally use another solution: CSS3 scale scaling.
Dynamically adjust the scaling ratio of elements by calculating the size ratio of the design drawing and the actual page display area size.
Personally, I think this is the best way to preserve the display content and style at small resolutions.
Of course, this method still has some disadvantages:
Here is a brief review of Vue's custom instructions: by configuring custom instructions and binding parameters, the corresponding processing logic is executed at different times such as component/element loading, updating, and destruction.
Vue’s custom instructions include the following hook functions:
Here because we only need to bind the browser's resize event to adjust the element scaling during initialization, we only need to configure inserted; of course, in order to optimize the code logic and reduce resource consumption, etc. , it is also necessary to cancel a callback function of the resize event in the unbind stage.
The code is as follows:
// 缩放指令 import Vue from "vue"; function transformScale(el, options) { const { target = "width", origin = "top left" } = options; Vue.nextTick(() => { // 获取显示区域高宽 const width = window.innerWidth; const height = window.innerHeight; el.style.transformOrigin = origin; if (target === "ratio") { const scaleX = width / CONF.width; const scaleY = height / CONF.height; el.style.transform = `scaleX(${scaleX}) scaleY(${scaleY})`; } else { let scaleProportion = 1; if (target === "width") { scaleProportion = width / CONF.width; } if (target === "height") { scaleProportion = height / CONF.height; } el.style.transform = `scale(${scaleProportion})`; } }); } function inserted(el, binding) { const options = binding.options || { passive: true }; const callback = () => transformScale(el, binding.value); window.addEventListener("resize", callback); callback(); el._onResize = { callback, options }; } function unbind(el) { if (!el._onResize) { return; } const { callback } = el._onResize; window.removeEventListener("resize", callback); delete el._onResize; } export const Scale = { inserted, unbind }; export default Scale;
Description: The
The whole code is actually very simple, which is to adjust the zoom ratio of the element by listening to the resize event.
But here I also made a little configuration to adapt to more situations:
Of course, this command cannot be said to be perfect. There are still many loopholes, such as no anti-shake, scaling will not change the size specified by css, scroll bars are prone to appear, etc.;
And because of the previous The project also involves a lot of charts and maps, which often leads to some display problems, so some new instructions are added later, but the issue of resolution adaptation still needs to be determined based on the actual situation.
【Related recommendations: javascript video tutorial, vue.js tutorial】
The above is the detailed content of Detailed explanation of Vue instruction to implement resolution adaptation of large screen elements. For more information, please follow other related articles on the PHP Chinese website!