A method for implementing a virtual list with variable height

By calculating the real DOM height and scroll height, and rendering list items of variable height in real time, it solves the problem of fixed height limit when rendering large data lists in H5 pages, improves the display and performance of all content, and provides better user experience.

CN113656716BActive Publication Date: 2024-08-30INSPUR SOFTWARE CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202110888670.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-02
Publication Date
2024-08-30
Estimated Expiration
2041-08-02

AI Technical Summary

Technical Problem

When an H5 page renders a list with a large amount of data, existing virtual list solutions usually require the list items to have a fixed height, resulting in some content not being fully displayed and failing to effectively solve performance issues and user experience issues.

Method used

By calculating the real DOM height and the current scroll height, list items are rendered in real time, allowing list items to have variable heights, and the range of displayed data is calculated through scroll events, and absolute positioning and scroll bar mechanisms are used to achieve dynamic rendering of list items.

Benefits of technology

Under the premise of ensuring rendering performance, all list contents can be displayed, providing better user experience and authenticity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113656716B
    Figure CN113656716B
Patent Text Reader

Abstract

The present invention provides a method for realizing a virtual list with variable height, which belongs to the technical field of big data display. With the triggering of a scrolling event, the corresponding data is rendered in real time by calculating the real DOM height and the current scrolling height. The method is mainly used for displaying a list with large amounts of data on an H5 page, and does not impose height restrictions on list items, so that all contents can be displayed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of big data display technology, and in particular to a method for realizing a virtual list with variable height. The present invention is mainly used for displaying a list with large data volume on an H5 page, and does not impose a height limit on the list items, so that the entire content can be displayed. Background Art

[0002] H5 pages that render large lists without paging have always been a big performance issue, and the performance of H5 pages rendering DOM elements is far lower than the performance of JS calculations. Based on this premise, most solutions basically use virtual lists and do not render the DOM of the entire data volume.

[0003] For virtual lists, the most important thing is to calculate the part of the data that should be displayed based on the virtual scroll bar. Most virtual lists require each list item to have a uniform fixed height in order to correctly display the data and the virtual scroll bar. This means that some list items may not be able to display all the content. Summary of the invention

[0004] In order to solve the above technical problems, the present invention provides a method for realizing a virtual list with variable height, which is mainly used for displaying a list with large amount of data on an H5 page, and does not impose height restrictions on list items so that all contents can be displayed; the content of each list item is displayed to provide a better user experience.

[0005] The technical solution of the present invention is:

[0006] A method for implementing a virtual list with variable height. When a scroll event is triggered, the corresponding data is rendered in real time by calculating the real DOM height and the current scroll height.

[0007] The real DOM height is calculated by knowing the scroll height and content height of the current DOM, the scroll height and content height of the previous DOM, and the height of the DOM is the current DOM content height + the current DOM scroll height - the scroll height of the previous DOM - the content height of the previous DOM.

[0008] Furthermore,

[0009] If you want the list items to have variable heights and be able to calculate the range of displayed data, you need two key data: the height of the current scroll position from the scroll container and the height of each list item.

[0010] in,

[0011] a) The height of the scroll position from the scroll container

[0012] If you want to get the scroll height, the scroll bar must be real; the height of the visible area in the middle is fixed; the dotted line is the scroll area, and the height is the sum of the heights of all list items, that is, the real list height; the area below the visible area is the list item display area, and the area below the list item display area is the scroll area;

[0013] Set the visible area to the parent element, and the scrolling area and list item display area to the child element. Since the scrolling area is larger than the parent element and the parent element has a fixed height, the parent element will have a real scroll bar.

[0014] The display area can use absolute positioning relative to the parent element and scroll height to keep it in the visible area as it scrolls.

[0015] b) The height of each list item

[0016] offset1 is the scroll height of the previous list item, height1 is the element height of the previous list item, offset2 is the scroll height of the list item, height2 is the element height of the list item, then the actual height occupied by the list item is: height2+offset2–offset1–height1.

[0017] When calculating the height of list items and scrolling areas, for the corresponding data that has not been rendered to the DOM, a default height is required for calculation, and then replaced with the actual height after rendering during the scrolling process.

[0018] Furthermore,

[0019] Once the scroll height and list item height are known, the range of the displayed data can be calculated; traverse the list item heights and add them up one by one. When the sum is not less than the scroll height, it is the starting position of the displayed data. The end position can be obtained in the same way. The sum should be greater than or equal to the scroll height + the height of the visible area, or all list items have been traversed.

[0020] Going further,

[0021] The working steps are as follows:

[0022] 1) Create a parent element with a fixed height and relative positioning;

[0023] 2) Create two child elements in the parent element, both of which are absolutely positioned relative to the parent element. One is used for scrolling, and its height is the height of the list. The other is a display area with a variable height, which is supported by the child element.

[0024] 3) When initializing the data, use a default height for each data for the initial rendering;

[0025] 4) Listen to the scrolling event of the parent element, get the starting position of the displayed data according to the scrolling height of the element and the height of each data, and then get the end position of the displayed data according to the scrolling height, visible height, and height of each data, and update the position of the display area relative to the Y axis of the parent element so that it is always displayed in the visible area;

[0026] 5) After scrolling, if the displayed data has not been rendered before, get the real height corresponding to the data and update the height of the element used for scrolling. At this time, the length of the scroll bar will change. Until all the data has been rendered, the obtained list height is the real height.

[0027] The beneficial effects of the present invention are

[0028] For list rendering of large amounts of data, all content can be displayed at the same time while ensuring rendering performance, making the list more realistic and providing a better user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Figure 1 It is a schematic diagram of the workflow of the present invention;

[0030] Figure 2 This is a schematic diagram for obtaining the height of a list item. DETAILED DESCRIPTION

[0031] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0032] 1. If you want the list items to have variable heights and be able to calculate the range of displayed data, you need two key data: the height of the current scroll position from the scroll container and the height of each list item.

[0033] a) The height of the scroll position from the scroll container

[0034] If you want to get the scroll height, obviously, the scroll bar must be real. The DOM structure is as follows Figure 1As shown, the middle part is the visible area, which has a fixed height; the dotted line is the scrolling area, whose height is the sum of the heights of all list items, that is, the actual list height; the area below the visible area is the area for displaying corresponding data. As long as the visible area is set as the parent element, and the scrolling area and display area are set as child elements, since the scrolling area height is greater than the parent element and the parent element height is fixed, the parent element will have a real scroll bar. The display area can use absolute positioning relative to the parent element and the scrolling height to keep it in the visible area as it scrolls.

[0035] b) The height of each list item

[0036] The element height obtained in JS does not include the height of the outermost margin in the standard box model. The method for obtaining the height of the list item is as follows Figure 2 As shown, offset1 is the scroll height of the previous list item, height1 is the element height of the previous list item, offset2 is the scroll height of the list item, height2 is the element height of the list item, then the actual height occupied by the list item is: height2+offset2–offset1–height1.

[0037] When calculating the height of list items and scrolling areas, the height of the DOM is essential. For the corresponding data that has not been rendered in the DOM, a default height is required for calculation, and then replaced with the actual height after rendering during the scrolling process.

[0038] 2. Once the scroll height and list item height are known, the range of displayed data can be easily calculated. Traverse the list item heights and add them up one by one. When the sum is just greater than or equal to the scroll height, it is the start position of the displayed data. The end position can be obtained in the same way, except that the sum should be greater than or equal to the scroll height + the height of the visible area, or all list items have been traversed.

[0039] The specific operations are as follows:

[0040] 1) Create a parent element with a fixed height and relative positioning.

[0041] 2) Create two child elements in the parent element, both of which are absolutely positioned relative to the parent element. One is used for scrolling and its height is the list height. The other is the display area with a variable height, which is supported by the child element.

[0042] 3) When the data is initialized, a default height is used for each data for the initial rendering.

[0043] 4) Listen to the scrolling event of the parent element, get the starting position of the displayed data according to the scrolling height of the element and the height of each data, and then get the end position of the displayed data according to the scrolling height, visible height, and height of each data. At the same time, update the position of the display area relative to the Y-axis of the parent element so that it is always displayed in the visible area.

[0044] 5) After scrolling, if the displayed data has not been rendered before, get the real height corresponding to the data and update the height of the element used for scrolling. At this time, the length of the scroll bar will change. Until all the data has been rendered, the obtained list height is the real height.

[0045] The above description is only a preferred embodiment of the present invention, which is only used to illustrate the technical solution of the present invention, and is not used to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.

Claims

1. A method for implementing a virtual list of variable height, characterized in that: As the scroll event is triggered, the corresponding data is rendered in real time by calculating the real DOM height and the current scroll height; To make the list items variable in height and calculate the range of displayed data, two data are needed: the height of the current scroll position from the scroll container and the height of each list item a) The height of the scroll position from the scroll container If you want to get the scroll height, the scroll bar must be real; assume that the height of the visible area in the middle is fixed; the dotted line is the scroll area, and the height is the sum of the heights of all list items, that is, the real list height; the area below the visible area is the list item display area, and the area below the list item display area is the scroll area; Set the visible area to the parent element, and the scrolling area and list item display area to the child element. Since the scrolling area is larger than the parent element and the parent element has a fixed height, the parent element will have a real scroll bar. The display area uses absolute positioning relative to the parent element and scroll height to keep it in the visible area as it scrolls; b) The height of each list item offset1 is the scroll height of the previous list item, height1 is the element height of the previous list item, offset2 is the scroll height of the list item, height2 is the element height of the list item, then the actual height occupied by the list item is: height2 + offset2 – offset1 – height1.

2. The method according to claim 1, characterized in that The real DOM height is calculated, that is, the scroll height and content height of the current DOM, the scroll height and content height of the previous DOM are known, and the height of the DOM is the current DOM content height + the current DOM scroll height - the scroll height of the previous DOM - the content height of the previous DOM.

3. The method according to claim 1, characterized in that When calculating the height of list items and scrolling areas, for the corresponding data that has not been rendered to the DOM, a default height is required for calculation, and then replaced with the actual height after rendering during the scrolling process.

4. The method according to claim 3, characterized in that Once the scroll height and list item height are known, the range of the displayed data can be calculated; traverse the list item heights and add them up one by one. When the sum is not less than the scroll height, it is the starting position of the displayed data. The end position can be obtained in the same way. The sum should be greater than or equal to the scroll height + the height of the visible area, or all list items have been traversed.

5. The method according to claim 4, characterized in that The working steps are as follows: 1) Create a parent element with a fixed height and relative positioning; 2) Create two child elements in the parent element, both of which are absolutely positioned relative to the parent element. One is used for scrolling, and its height is the height of the list. The other is a display area with a variable height, which is supported by the child element. 3) When initializing the data, use a default height for each data for the initial rendering; 4) Listen to the scrolling event of the parent element, get the starting position of the displayed data according to the scrolling height of the element and the height of each data, and then get the end position of the displayed data according to the scrolling height, visible height, and height of each data. At the same time, update the position of the display area relative to the Y axis of the parent element so that it is always displayed in the visible area; 5) After scrolling, if the displayed data has not been rendered before, get the real height corresponding to the data and update the height of the element used for scrolling. At this time, the length of the scroll bar will change; until all the data has been rendered, the obtained list height is the real height.

Citation Information

Patent Citations

  • A method and a system for optimizing the rendering performance of a Vue front-end table component

    CN108959206A

  • A mass flight real-time monitoring method and a processing terminal based on a web terminal

    CN109389544A

  • Webpage list display method and device

    CN110717120A

  • List data display method and device and electronic equipment

    CN111339463A

  • Table implementation method and device, computer terminal equipment and storage medium

    CN111898049A