Method for realizing relative layout based on JavaScript
By using the relative layout methods of RelativeLayout.js and Vue in the page layout, and using attributes such as left, right, above, etc. for layout design, the problem of increasing page display time and large amount of code in the existing technology is solved, and the control is implemented The linkage and hierarchy of the simplified implementation of complex page logic.
Patent Information
- Application Number
- CN202411398261.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-09
- Publication Date
- 2024-11-05
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
When implementing relative layouts, the existing technology needs to recalculate and move the control position through JavaScript, which increases the page display time and may lead to short-term control position changes, and the amount of code is large when layouting complex pages.
By defining the RelativeLayout.js core parsing library and creating relative layout parent container components in Vue, using left, right, above, below and other properties for relative layout, the amount of code is reduced and the linkage of controls is achieved. You only need to set properties Complete complex layouts.
The linkage and hierarchy of the page are achieved. If a control changes the size or position, the associated controls can be changed accordingly. Only one layer of layout can be used to achieve complex page logic, reducing the layout hierarchy.
Smart Images

Figure CN118897670A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of page layout, and in particular to a method for realizing relative layout based on JavaScript. Background Art
[0002] Relative layout is a layout method that determines the position of controls based on the parent container and sibling controls as references, which can greatly reduce the level of complex layout pages. However, this layout is not implemented in the W3C standard. The benefits of using relative layout are: (1) Page linkage: Set up a group of related sibling controls. When the position or size of one of the controls changes, its related sibling controls will also change accordingly, thus realizing the linkage of the page.
[0003] (2) Reduce page hierarchy: Because relative layout is relative to the position of the parent container or sibling child controls, it can reduce the layout hierarchy and reduce page loading time.
[0004] (3) Reduce the amount of code: In some complex situations, using only CSS styles may not meet the requirements of page rendering. In this case, JavaScript code is needed to help generate the page. In many complex situations, using relative layouts only requires setting properties without having to write JavaScript code. Because the linkage between sibling controls is achieved, changing one control can reduce the amount of code required to change the relative positions of other controls.
[0005] However, in actual use, after HTML loads the page, JavaScript needs to be used to recalculate and move the positions of each control, which will slightly increase the time it takes to display the final result of the page. At the same time, there may be a brief process of control position changes during the page loading process. Summary of the invention
[0006] In view of the above problems, the present invention provides a method for realizing relative layout based on JavaScript.
[0007] The technical solution adopted is a method for implementing relative layout based on JavaScript, comprising the following steps: Define the RelativeLayout.js core parsing library for parsing RelativeLayout layout; Create a RelativeLayout.vue file in Vue to create a relative layout parent container component. When the component is used, call RelativeLayout.js to analyze the layout; Create a RelativeView.vue file in Vue as a child element of RelativeLayout.vue; Use the left, right, above, and below controls to surround the center for relative layout.
[0008] Optionally, in the RelativeLayout.js core parsing library definition, include the following steps: Set the required properties of the parent control; Set the required properties of the child controls; Get the Margin property of the child control and use it for position calculation; Resolves the relative properties of child controls.
[0009] Optionally, in RelativeLayout.js, use the center-in-parent attribute to indicate that the element is located in the middle of the parent container and is displayed in the center. The center-horizontal property indicates that the horizontal position is in the middle of the parent container; The center-vertical property indicates that the vertical position is in the middle of the parent container.
[0010] Optionally, in RelativeLayout.js, use the left attribute to indicate that it is located on the left side of the specified control; Through the right attribute, it indicates that it is located on the right side of the specified control; Through the above attribute, it indicates that it is located on the upper side of the specified control; Through the below attribute, it indicates that it is located below the specified control; The align-left attribute indicates that the left side is aligned with the left side of the specified control; The align-right property indicates that the right side is aligned with the right side of the specified control; The align-top attribute indicates that the top is aligned with the top of the specified control; The align-bottom property is used to align the bottom with the bottom of the specified control. The align-parent-right property indicates that the horizontal position is on the far right of the parent container; Through the align-parent-bottom property, it indicates the bottom of the parent container; The match-parent-width property indicates that the width is the width of the parent container minus the X-axis position of the control; The match-parent-height property indicates that the height is the height of the parent container minus the Y-axis position of the control.
[0011] Optionally, create a RelativeLayout.vue file in Vue, including the following steps: Create a container to hold the relative layout; The analyzeChildAttrs property in the created container is called cyclically to place the positions of all child controls.
[0012] Optionally, create a RelativeView.vue file in Vue, including the following steps: Define the attribute parameters of relative layout in Vue; Mount the defined attribute parameters to the properties of the Vue component.
[0013] Optionally, in the relative layout using the four controls of left, right, above, and below to surround the center, the following steps are included: Place the control with the id center in the middle of the relative-layout parent container and set the center-in-parent="true" attribute; Place the control with id left on the left side of center and set the left="center" attribute; Place the control with id right to the right of center and align it with the bottom of center, and set the two properties right="center" and align-bottom="center"; Place the control with id "above" above center and align it to the right of center. Set the above="center" and align-right="center" properties. Place the control with id below below the center. The size of below is the remaining space from the below position to the parent container. Set the three properties below="center", match-parent-width="true", and match-parent-height="true".
[0014] The beneficial effects of the present invention are: By adding controls, you only need to set properties to complete complex page layouts, and realize the linkage of pages. When a control changes its size or position, other related controls can also change their positions in conjunction. The layout level is reduced, and complex page logic can be implemented using only one layer of layout. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 This is a schematic diagram of the operation results. DETAILED DESCRIPTION
[0016] The following describes the implementation of the present invention through specific examples. Those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific implementations, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.
[0017] It should be noted that the illustrations provided in the following embodiments are only used to schematically illustrate the basic concept of the present invention, and thus the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0018] A method for implementing relative layout based on JavaScript, comprising the following steps: Define the RelativeLayout.js core parsing library for parsing RelativeLayout layout; Create a RelativeLayout.vue file in Vue to create a relative layout parent container component. When the component is used, call RelativeLayout.js to analyze the layout; Create a RelativeView.vue file in Vue as a child element of RelativeLayout.vue; Use the left, right, above, and below controls to surround the center for relative layout.
[0019] The purpose of this design is to complete complex page layouts by simply setting properties of the added controls. This realizes the linkage of the page. When a control changes its size or position, other related controls can also change their positions in conjunction with it. This reduces the layout hierarchy, and only one layer of layout is needed to implement complex page logic.
[0020] In the specific implementation, the properties of each subcomponent in the layout are first parsed (such as custom properties of relative layout and standard properties such as border and margin), and then the placement position of the control relative to the parent control or sibling control is calculated based on the properties.
[0021] For example, if a control has a "center-in-parent" attribute, it means that the control is horizontally and vertically centered in the parent control.
[0022] At the same time, as an example, the following code can be used: function analyzeChildAttrs(root, elm, attrs) { root.style.position = "relative"; root.style["box-sizing"] = "border-box"; Used to set the required properties of the parent control.
[0023] Then, as an example, the following code: elm.style.position = "absolute"; elm.style["box-sizing"] = "border-box"; Used to set required properties of child controls.
[0024] Next, as an example, you can use the following code: const elmMargin = getMargin(elm); Used to get the Margin property of a child control and use the Margin property for position calculation.
[0025] Finally, as an example, the following code can be used: if (attrs["center-in-parent"]) { elm.style.left = parseFloat(root.offsetWidth) / 2 - parseFloat(elm.offsetWidth) / 2 + "px"; elm.style.top = parseFloat(root.offsetHeight) / 2 - parseFloat(elm.offsetHeight) / 2 + "px"; return; } if (attrs["center-horizontal"]) { elm.style.left = parseFloat(root.offsetWidth) / 2 - parseFloat(elm.offsetWidth) / 2 + "px"; } if (attrs["center-vertical"]) { elm.style.top = parseFloat(root.offsetHeight) / 2 - parseFloat(elm.offsetHeight) / 2 + "px"; } if (attrs["align-parent-right"]) { elm.style.left = parseFloat(root.offsetWidth) - parseFloat(elm.offsetWidth) + "px"; } .... } Used to parse the relative properties of child controls. In the above code, the center-in-parent property is used to indicate that the child control is located in the middle of the parent container and is displayed in the center. The center-horizontal property indicates that the horizontal position is in the middle of the parent container; The center-vertical property indicates that the vertical position is in the middle of the parent container. Through the left attribute, it indicates that it is located on the left side of the specified control; Through the right attribute, it indicates that it is located on the right side of the specified control; Through the above attribute, it indicates that it is located on the upper side of the specified control; Through the below attribute, it indicates that it is located below the specified control; The align-left attribute indicates that the left side is aligned with the left side of the specified control; The align-right property indicates that the right side is aligned with the right side of the specified control; The align-top attribute indicates that the top is aligned with the top of the specified control; The align-bottom property is used to align the bottom with the bottom of the specified control. The align-parent-right property indicates that the horizontal position is on the far right of the parent container; Through the align-parent-bottom property, it indicates the bottom of the parent container; The match-parent-width property indicates that the width is the width of the parent container minus the X-axis position of the control; The match-parent-height property indicates that the height is the height of the parent container minus the Y-axis position of the control.
[0026] At the same time, create a RelativeLayout.vue file in Vue to create a relative layout parent container component. When the component is used, RelativeLayout.js is called to analyze the layout.
[0027] At the same time, as an example, the following code can be used: return createElement("div", { ref: "content"}, this.$slots.default); }, mounted() { const root = this.$refs.content; const children = this.$slots.default; Used to create a container for holding relative layout.
[0028] Again, as an example, the following code can be used: children.forEach((vNode) => { analyzeChildAttrs(root, vNode.elm, vNode.data.attrs); }); }; }; Used to loop through the analyzeChildAttrs property in the created container to position all child controls.
[0029] Create a RelativeView.vue file in Vue as a child element of RelativeLayout.vue, which is mainly used to define the properties in RelativeLayout.js in Vue.
[0030] At the same time, as an example, the following code can be used: props: [ "centerInParent", "centerVertical", "centerHorizontal", "alignParentRight", "alignParentBottom", "alignLeft", "alignRight", "alignTop", "alignBottom", "left", "right", "above", "below", "matchParentWidth", "matchParentHeight", ], created() {; Attribute parameters used to define relative layout in Vue.
[0031] Again, as an example, the following code can be used: this.$attrs["center-in-parent"] = this.centerInParent; this.$attrs["center-vertical"] =this.centerVertical; this.$attrs["center-horizontal"] =this.centerHorizontal; this.$attrs["align-parent-right"] =this.alignParentRight; this.$attrs["align-parent-bottom"] =this.alignParentBottom; this.$attrs["center-parent"] =this.centerInParent; this.$attrs["align-left"] = this.alignLeft; this.$attrs["align-right"] = this.alignRight; this.$attrs["align-top"] = this.alignTop; this.$attrs["align-bottom"] =this.alignBottom; this.$attrs["left"] = this.left; this.$attrs["right"] = this.right; this.$attrs["above"] = this.above; this.$attrs["below"] = this.below; this.$attrs["match-parent-width"] =this.matchParentWidth; this.$attrs["match-parent-height"] =this.matchParentHeight; }, }; ; Used to mount the defined attribute parameters to the properties of the Vue component.
[0032] In the relative layout using the four controls left, right, above, and below to surround the center, such as Figure 1 As shown: At the same time, as an example, the following code can be used: <relative-viewid="center"center-in-parent="true"> center; Used to place the control with id center in the middle of the relative-layout parent container and set the center-in-parent="true" attribute; Again, as an example, the following code can be used: <relative-view id="left" left="center"> left< / relative-view> ; Used to place the control with id left on the left side of center and set the left="center" attribute; Again, as an example, the following code can be used: <relative-viewid="right"right="center"align-bottom="center"> right; Used to place the control with id right to the right of center and align it with the bottom of center, set the two attributes right="center" and align-bottom="center"; Again, as an example, the following code can be used: <relative-viewid="above"above="center"align-right="center"> above; It is used to place the control with id "above" above the center and align it to the right of the center. Set the above="center" and align-right="center" attributes. Again, as an example, the following code can be used: <relative-view id="below" below="center" match-parent-width="true" match-parent-height="true"> Below; ; ; It is used to place the control with id below below the center. The size of below is the remaining space from the below position to the parent container. Set the three attributes below="center", match-parent-width="true", and match-parent-height="true".
[0033] To implement the above page, you usually need to adjust the position with JavaScript code, but with relative-layout, you only need to set the attributes to achieve it. If the size or position of center is changed, the position of left, right and other controls will also change accordingly, realizing the linkage of the page. Using relative-layout reduces the layout level, and only one layer of layout can be used to implement complex page logic.
[0034] Finally, it should be noted that the above are only preferred embodiments of the present invention and are not intended to limit the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, it is still possible for those skilled in the art to modify the technical solutions described in the aforementioned embodiments, or to make equivalent substitutions for some of the technical features therein. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for implementing relative layout based on JavaScript, characterized in that: The following steps are involved: Define the RelativeLayout.js core parsing library for parsing RelativeLayout layout; Create a RelativeLayout.vue file in Vue to create a relative layout parent container component. When the component is used, call RelativeLayout.js to analyze the layout; Create a RelativeView.vue file in Vue as a child element of RelativeLayout.vue; Use the left, right, above, and below controls to surround the center for relative layout.
2. The method for implementing relative layout based on JavaScript according to claim 1, characterized in that: In the RelativeLayout.js core parsing library definition, the following steps are included: Set the required properties of the parent control; Set the required properties of the child controls; Get the Margin property of the child control and use it for position calculation; Resolves the relative properties of child controls.
3. The method for implementing relative layout based on JavaScript according to claim 2, characterized in that: In RelativeLayout.js, the center-in-parent attribute is used to indicate that the element is located in the middle of the parent container and is displayed in the center. The center-horizontal property indicates that the horizontal position is in the middle of the parent container; The center-vertical property indicates that the vertical position is in the middle of the parent container.
4. The method for implementing relative layout based on JavaScript according to claim 3, characterized in that: In RelativeLayout.js, the left attribute is used to indicate that the control is located on the left side of the specified control. Through the right attribute, it indicates that it is located on the right side of the specified control; Through the above attribute, it indicates that it is located on the upper side of the specified control; Through the below attribute, it indicates that it is located below the specified control; The align-left attribute indicates that the left side is aligned with the left side of the specified control; The align-right property indicates that the right side is aligned with the right side of the specified control; The align-top attribute is used to align the top of the specified control. The align-bottom property is used to align the bottom with the bottom of the specified control. The align-parent-right property indicates that the horizontal position is on the far right of the parent container; Through the align-parent-bottom property, it indicates the bottom of the parent container; The match-parent-width property indicates that the width is the width of the parent container minus the X-axis position of the control; The match-parent-height property indicates that the height is the height of the parent container minus the Y-axis position of the control.
5. The method for implementing relative layout based on JavaScript according to claim 4, characterized in that: Create a RelativeLayout.vue file in Vue, including the following steps: Create a container to hold the relative layout; The analyzeChildAttrs property in the created container is called cyclically to place the positions of all child controls.
6. The method for implementing relative layout based on JavaScript according to claim 5, characterized in that: Create a RelativeView.vue file in Vue, including the following steps: Define the attribute parameters of relative layout in Vue; Mount the defined attribute parameters to the properties of the Vue component.
7. The method for implementing relative layout based on JavaScript according to claim 6, characterized in that: In the relative layout using the four controls of left, right, above, and below to surround the center, the following steps are included: Place the control with the id center in the middle of the relative-layout parent container and set the center-in-parent="true" attribute; Place the control with id left on the left side of center and set the left="center" attribute; Place the control with id right to the right of center and align it with the bottom of center, and set the two properties right="center" and align-bottom="center"; Place the control with id "above" above center and align it to the right of center. Set the above="center" and align-right="center" properties. Place the control with id below below the center. The size of below is the remaining space from the below position to the parent container. Set the three properties below="center", match-parent-width="true", and match-parent-height="true".
Citation Information
Patent Citations
Page generation method and device
CN111752667A
Display Interface Layout Method and Electronic Device
US20230359447A1
