tencent cloud

Feedback

Explanation of the View Layer

Last updated: 2024-03-05 14:41:39

    Overview

    The view layer of the Mini Program framework is created via WXML and WXSS, and is displayed through components. WXML is used to describe the structure of the page, while WXSS is used to describe the style of the page. WXS is a scripting language for Mini Programs that, in conjunction with WXML, can be used to construct the structure of a page.
    The logic layer of the framework is responsible for transforming data into views, as well as transmitting events from the view layer to the logic layer. This allows developers to focus on data and logic processing, without having to overly concern themselves with the implementation details of the view layer.
    Components are the fundamental building blocks of the view, aiding developers in effortlessly implementing various functionalities and interactive effects. Developers have the option to create custom components or use the component library provided by the framework, thereby swiftly constructing services that offer a native App experience.

    WXML

    For operations related to WXML, see WXML Details.

    WXSS

    For operations related to WXSS, see WXSS Details.

    Basic components

    Description

    The framework provides developers with a series of fundamental components, enabling rapid development through the combination of these basic elements.
    What a component is:
    A component is the fundamental building block of the view layer.
    Components come equipped with functionalities and styles consistent with the TMF style.
    A component typically comprises a start tag and an end tag, with attributes serving to modify the component, and the content nestled between the two tags.
    <tagname property="value">
    Content goes here ...
    </tagname>
    Note:
    All components and attributes are in lowercase, connected by hyphens.

    Attribute type

    Type
    Description
    Annotation
    Boolean
    Boolean Value
    When an attribute is written on a component, regardless of its value, it is considered as True. The attribute value is only False when the attribute is absent from the component. If the attribute value is a variable, the variable's value will be converted to a Boolean type.
    Number
    Number
    1, 2.5
    String
    String
    "string"
    Array
    Array
    [ 1, "string" ]
    Object
    Object
    { key: value }
    EventHandler
    Event handling function name
    "handlerName" is the event handler function name defined within the Page.
    Any
    Arbitrary Attribute
    -

    Public Attribute

    All components possess the following attributes:
    Attribute name
    Type
    Description
    Annotation
    id
    string
    The unique identifier of the component.
    Maintain uniqueness across the entire page.
    class
    string
    The unique identifier of the component.
    The style class defined in the corresponding WXSS.
    style
    string
    The inline style of the component.
    Dynamically settable inline styles.
    hidden
    boolean
    The visibility of the component.
    All components are displayed by default.
    date-*
    any
    Custom attributes
    When an event is triggered on the component, it is dispatched to the event handling function.
    bind/catch
    eventhandler
    Component event
    See WXML for more details.

    Special attribute

    Almost all components have their own custom attributes, which can be used to modify the functionality or style of the component. See the definitions in each Component Overview.

    Get node information on the interface

    WXML Node Information

    The Node Information Query API can be used to get node attributes, styles, and position information on the interface.
    The most common usage of this interface is to query the current position of a particular node, as well as the scroll position of the interface.
    Sample Code
    const query = wx.createSelectorQuery()
    query.select('#the-id').boundingClientRect(function (res) {
    res.top // The upper boundary coordinate of the #the-id node (relative to the display area)
    })
    query.selectViewport().scrollOffset(function (res) {
    res.scrollTop // The vertical scroll position of the display area
    })
    query.exec()
    In the aforementioned example, #the-id is a node selector, similar to CSS selectors but with slight differences. See related instructions of SelectorQuery.
    In custom components or pages containing custom components, it is recommended to use this.createSelectorQuery instead of wx.createSelectorQuery to ensure the correct range of node selection.

    WXML Node Layout Intersection Status

    The Node Layout Intersection Status API can be used to monitor the intersection status of two or more component nodes in terms of layout position. This set of APIs is often used to infer whether certain nodes are visible to the user and what proportion can be seen by the user.
    The main concepts involved in this set of APIs are as follows.
    Reference Node: The reference node being monitored, whose layout area is taken as the reference area. If there are multiple reference nodes, the intersection of their layout areas will be taken as the reference area. The page display area can also serve as one of the reference areas.
    Target Node: The target of monitoring, by default, can only be a single node (when using the selectAll option, multiple nodes can be monitored simultaneously).
    Intersection Area: The overlapping area between the layout area of the target node and the reference area.
    Intersection Ratio: The proportion of the intersection area to the reference area.
    Threshold: If the intersection ratio reaches the threshold, the callback function of the listener will be triggered. Multiple thresholds can be set.
    The following example code triggers a callback function each time the target node (specified by the selector .target-class) enters or leaves the page display area.
    Sample Code
    Page({
    onLoad() {
    wx.createIntersectionObserver().relativeToViewport().observe('.target-class', (res) => {
    res.id // ID of the target node
    res.dataset // Dataset of the target node
    res.intersectionRatio // The proportion of the intersection area to the layout area of the target node
    res.intersectionRect // Intersection area
    res.intersectionRect.left // Coordinates of the left boundary of the intersection area
    res.intersectionRect.top // Coordinates of the upper boundary of the intersection area
    res.intersectionRect.width // Width of the intersection area
    res.intersectionRect.height // Height of the intersection area
    })
    }
    })
    The following example code triggers a callback function when the target node (specified by the .target-class selector) intersects or disjoins with the reference node (specified by the .relative-class selector) within the page display area, and the degree of intersection or disjunction reaches 20% and 50% of the target node's layout area.
    Sample Code
    Page({
    onLoad() {
    wx.createIntersectionObserver(this, {
    thresholds: [0.2, 0.5]
    }).relativeTo('.relative-class').relativeToViewport().observe('.target-class', (res) => {
    res.intersectionRatio // The proportion of the intersection area to the layout area of the target node
    res.intersectionRect // Intersection area
    res.intersectionRect.left // Coordinates of the left boundary of the intersection area
    res.intersectionRect.top // Coordinates of the upper boundary of the intersection area
    res.intersectionRect.width // Width of the intersection area
    res.intersectionRect.height // Height of the intersection area
    })
    }
    })
    Note:
    The intersection area with the page display area does not accurately represent the area visible to the user, as the area involved in the calculation is the "layout area". The layout area may be clipped and hidden by other nodes during rendering (such as encountering nodes with the overflow style set to hidden in ancestor nodes) or obscured (such as encountering nodes with fixed positioning).
    In custom components or pages containing custom components, it is recommended to use this.createIntersectionObserver instead of wx.createIntersectionObserver to ensure nodes are selected within the correct scope.

    Responding to display area changes

    Display area dimensions

    The display area refers to the area in the mini program interface where layout can be freely displayed. By default, the size of the mini program's display area does not change from the moment the page is initialized. However, the following two methods can alter this default behavior.

    Enabling screen rotation support on mobile phones

    Mini programs support screen rotation on mobile phones. The method to enable screen rotation in the pages of a mini program is to set app.json in the window section to "pageOrientation": "auto", or configure "pageOrientation": "auto" in the page json file.
    Below is an example of enabling screen rotation in a single page json file.
    Sample Code
    {
    "pageOrientation": "auto"
    }

    Enabling screen selection support on iPad

    Mini programs running on iPad can support screen rotation. The method to enable screen rotation support for iPad mini-programs is to add "resizable": true in app.json.
    Sample Code
    {
    "resizable": true
    }

    Media Query

    At times, the layout of a page may vary for different sized display areas. In such instances, media queries can be employed to resolve the majority of issues.
    Sample Code
    .my-class {
    width: 40px;
    }
    
    @media (min-width: 480px) {
    /* Style rules effective only on screens 480 px or wider */
    .my-class {
    width: 200px;
    }
    }

    Screen Rrotation event

    Occasionally, media queries alone cannot control some of the finer layout changes. In such instances, JavaScript can be used as an auxiliary tool.
    To read the display area size of a page in JavaScript, one can use selectorQuery.selectViewport.
    To monitor events where the page size changes, the page's onResize can be utilized. For custom components, the resize lifecycle can be used for monitoring. The callback function will return the size information of the display area.
    Sample Code
    Page({
    onResize(res) {
    res.size.windowWidth // New display area width
    res.size.windowHeight // New display area height
    }
    })
    Component({
    pageLifetimes: {
    resize(res) {
    res.size.windowWidth // New display area width
    res.size.windowHeight // New display area height
    }
    }
    })
    Additionally, wx.onWindowResize can be used for monitoring, although this is not the recommended method.

    Animation

    Common methods for interface animation

    In mini programs, CSS gradients and CSS animations are typically used to create simple interface animations.
    Simultaneously, the wx.canvasContext interface can be used to dynamically create simple animation effects.
    During the animation process, animation events can be monitored using bindtransitionend, bindanimationstart, bindanimationiteration, and bindanimationend.
    transitionend: Marks the end of a CSS gradient or the completion of a stage in wx.createAnimation.
    animationstart: Start of a CSS animation.
    animationiteration: Marks the completion of a stage in a CSS animation.
    animationend: Ending of a CSS animation.
    Note:
    These events are not bubbling events and will only take effect when bound to the nodes where the animation actually occurs.

    Advanced animation methods

    In certain complex scenarios, the aforementioned animation methods may not be applicable.
    WXML Event Response can dynamically adjust the style attributes of nodes by utilizing QS to respond to events. By continuously altering the value of the style attribute, animation effects can be achieved. Concurrently, this method can also dynamically generate animations based on user touch events.
    The method of continuously using setData to alter the interface can also achieve animation effects. This allows for arbitrary interface changes, but it typically results in significant delays or stuttering, and may even cause the mini program to freeze. In such cases, performance can be enhanced by changing the page's setData to the setData within the custom module component.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support