开始标签
和结束标签
,属性
用来修饰这个组件,内容
在两个标签之内。<tagname property="value">Content goes here ...</tagname>
类型 | 描述 | 注解 |
Boolean | 布尔值 | 组件写上该属性,不管是什么值都被当作 True;只有组件上没有该属性时,属性值才为 False。如果属性值为变量,变量的值会被转换为 Boolean 类型 |
Number | 数字 | 1, 2.5 |
String | 字符串 | "string" |
Array | 数组 | [ 1, "string" ] |
Object | 对象 | { key: value } |
EventHandler | 事件处理函数名 | |
Any | 任意属性 | - |
属性名 | 类型 | 描述 | 注解 |
id | string | 组件的唯一标识 | 保持整个页面唯一 |
class | string | 组件的唯一标识 | 在对应的 WXSS 中定义的样式类 |
style | string | 组件的内联样式 | 可以动态设置的内联样式 |
hidden | boolean | 组件是否显示 | 所有组件默认显示 |
date-* | any | 自定义属性 | 组件上触发的事件时,会发送给事件处理函数 |
bind/catch | eventhandler | 组件的事件 |
const query = wx.createSelectorQuery()query.select('#the-id').boundingClientRect(function (res) {res.top // #the-id 节点的上边界坐标(相对于显示区域)})query.selectViewport().scrollOffset(function (res) {res.scrollTop // 显示区域的竖直滚动位置})query.exec()
selectAll
选项时,可以同时监听多个节点)。.target-class
指定)每次进入或离开页面显示区域时,触发回调函数。Page({onLoad() {wx.createIntersectionObserver().relativeToViewport().observe('.target-class', (res) => {res.id // 目标节点 idres.dataset // 目标节点 datasetres.intersectionRatio // 相交区域占目标节点的布局区域的比例res.intersectionRect // 相交区域res.intersectionRect.left // 相交区域的左边界坐标res.intersectionRect.top // 相交区域的上边界坐标res.intersectionRect.width // 相交区域的宽度res.intersectionRect.height // 相交区域的高度})}})
Page({onLoad() {wx.createIntersectionObserver(this, {thresholds: [0.2, 0.5]}).relativeTo('.relative-class').relativeToViewport().observe('.target-class', (res) => {res.intersectionRatio // 相交区域占目标节点的布局区域的比例res.intersectionRect // 相交区域res.intersectionRect.left // 相交区域的左边界坐标res.intersectionRect.top // 相交区域的上边界坐标res.intersectionRect.width // 相交区域的宽度res.intersectionRect.height // 相交区域的高度})}})
this.createIntersectionObserver
来代替 wx.createIntersectionObserver,这样可以确保在正确的范围内选择节点。app.json
的 window
段中设置 "pageOrientation": "auto"
,或在页面 json 文件中配置 "pageOrientation": "auto"
。{"pageOrientation": "auto"}
app.json
中添加 "resizable": true
。{"resizable": true}
.my-class {width: 40px;}@media (min-width: 480px) {/* 仅在 480px 或更宽的屏幕上生效的样式规则 */.my-class {width: 200px;}}
onResize
来监听。对于自定义组件,可以使用 resize 生命周期来监听。回调函数中将返回显示区域的尺寸信息。Page({onResize(res) {res.size.windowWidth // 新的显示区域宽度res.size.windowHeight // 新的显示区域高度}})
Component({pageLifetimes: {resize(res) {res.size.windowWidth // 新的显示区域宽度res.size.windowHeight // 新的显示区域高度}}})
wx.onWindowResize
来监听(但这不是推荐的方式)。bindtransitionend
bindanimationstart
bindanimationiteration
bindanimationend
来监听动画事件。
本页内容是否解决了您的问题?