|
| 1 | +Component({ |
| 2 | + externalClasses: ["l-class"], |
| 3 | + |
| 4 | + relations: { |
| 5 | + "../sticky-item/index": { |
| 6 | + type: "child", |
| 7 | + linked() { |
| 8 | + this.checkSupportCssSticky().then((isSupportCssSticky) => { |
| 9 | + if (!isSupportCssSticky) { |
| 10 | + this.updateStickyItemsSizeData() |
| 11 | + } |
| 12 | + }) |
| 13 | + }, |
| 14 | + linkChanged() { |
| 15 | + this.checkSupportCssSticky().then((isSupportCssSticky) => { |
| 16 | + if (!isSupportCssSticky) { |
| 17 | + this.updateStickyItemsSizeData() |
| 18 | + } |
| 19 | + }) |
| 20 | + }, |
| 21 | + unlinked() { |
| 22 | + this.checkSupportCssSticky().then((isSupportCssSticky) => { |
| 23 | + if (!isSupportCssSticky) { |
| 24 | + this.updateStickyItemsSizeData() |
| 25 | + } |
| 26 | + }) |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + |
| 31 | + properties: { |
| 32 | + /** |
| 33 | + * 吸顶容器实现模式 |
| 34 | + * js - 使用js实现 |
| 35 | + * css - 使用css实现,若不支持css,则回滚到js模式 |
| 36 | + */ |
| 37 | + mode: { |
| 38 | + type: String, |
| 39 | + value: "js" |
| 40 | + }, |
| 41 | + |
| 42 | + /** |
| 43 | + * 页面垂直滚动的距离 |
| 44 | + */ |
| 45 | + scrollTop: Number, |
| 46 | + }, |
| 47 | + |
| 48 | + observers: { |
| 49 | + /** |
| 50 | + * 监听页面滚动,实时更新吸顶容器位置 |
| 51 | + */ |
| 52 | + "scrollTop": function () { |
| 53 | + this.checkSupportCssSticky().then((isSupportCssSticky) => { |
| 54 | + if (!isSupportCssSticky) { |
| 55 | + this.updateStickyItemsPosition() |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | + }, |
| 60 | + |
| 61 | + lifetimes: { |
| 62 | + attached() { |
| 63 | + this.checkSupportCssSticky().then((isSupportCssSticky) => { |
| 64 | + if (!isSupportCssSticky) { |
| 65 | + this.initSticky() |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + methods: { |
| 72 | + |
| 73 | + /** |
| 74 | + * 创建wx.lin函数 |
| 75 | + */ |
| 76 | + initSticky() { |
| 77 | + wx.lin = wx.lin || {} |
| 78 | + wx.lin.flushSticky = () => { |
| 79 | + this.updateStickyItemsSizeData() |
| 80 | + } |
| 81 | + |
| 82 | + // 传入scrollTop的值的函数 |
| 83 | + wx.lin.setScrollTop = (scrollTop) => { |
| 84 | + this.data.scrollTop = scrollTop |
| 85 | + this.checkSupportCssSticky().then((isSupportCssSticky) => { |
| 86 | + if (!isSupportCssSticky) { |
| 87 | + this.updateStickyItemsPosition() |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | + }, |
| 92 | + |
| 93 | + /** |
| 94 | + * 更新所有sticky-item组件的position属性 |
| 95 | + */ |
| 96 | + updateStickyItemsPosition() { |
| 97 | + const stickyItemNodes = this.getStickyItemNodes() |
| 98 | + for (let stickyItemNode of stickyItemNodes) { |
| 99 | + stickyItemNode.updateStickyItemPosition(this.data.scrollTop) |
| 100 | + } |
| 101 | + }, |
| 102 | + |
| 103 | + /** |
| 104 | + * 更新所有sticky-item组件的基础数据 |
| 105 | + */ |
| 106 | + updateStickyItemsSizeData() { |
| 107 | + const stickyItemNodes = this.getStickyItemNodes() |
| 108 | + stickyItemNodes.forEach((item, index) => { |
| 109 | + item.updateStickyItemBaseData(index) |
| 110 | + }) |
| 111 | + }, |
| 112 | + |
| 113 | + /** |
| 114 | + * 获取所有的sticky-item组件 |
| 115 | + * @return {Object} sticky-item组件集合 |
| 116 | + */ |
| 117 | + getStickyItemNodes() { |
| 118 | + return this.getRelationNodes("../sticky-item/index") |
| 119 | + }, |
| 120 | + |
| 121 | + /** |
| 122 | + * 检测当前webview内核是否支持css设置sticky |
| 123 | + * @return {Boolean} css是否支持设置sticky |
| 124 | + */ |
| 125 | + checkSupportCssSticky() { |
| 126 | + return new Promise((resolve) => { |
| 127 | + const stickyItemNodes = this.getStickyItemNodes() |
| 128 | + if (stickyItemNodes.length == 0) { |
| 129 | + resolve(false) |
| 130 | + } |
| 131 | + |
| 132 | + // 根据position判断是否支持position:sticky |
| 133 | + wx |
| 134 | + .createSelectorQuery() |
| 135 | + .in(stickyItemNodes[0]) |
| 136 | + .select(".l-sticky-item-header") |
| 137 | + .fields({ computedStyle: ["position"] }) |
| 138 | + .exec((res) => { |
| 139 | + if (res[0] === null) { |
| 140 | + resolve(false) |
| 141 | + } else { |
| 142 | + resolve(res[0].position === "sticky") |
| 143 | + } |
| 144 | + }) |
| 145 | + }) |
| 146 | + }, |
| 147 | + } |
| 148 | +}) |
0 commit comments