sticky.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import props from './props';
  9. import config from '../common/config';
  10. import { pageScrollMixin, getRect } from '../mixins/page-scroll';
  11. const { prefix } = config;
  12. const ContainerClass = `.${prefix}-sticky`;
  13. let Sticky = class Sticky extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`];
  17. this.properties = props;
  18. this.behaviors = [
  19. pageScrollMixin(function (event) {
  20. this.onScroll(event);
  21. }),
  22. ];
  23. this.observers = {
  24. 'offsetTop, disabled, container'() {
  25. this.onScroll();
  26. },
  27. };
  28. this.data = {
  29. prefix,
  30. containerStyle: '',
  31. contentStyle: '',
  32. classPrefix: `.${prefix}-sticky`,
  33. };
  34. this.methods = {
  35. onScroll(event) {
  36. const { scrollTop } = event || {};
  37. const { container, offsetTop, disabled } = this.properties;
  38. if (disabled) {
  39. this.setDataAfterDiff({
  40. isFixed: false,
  41. transform: 0,
  42. });
  43. return;
  44. }
  45. this.scrollTop = scrollTop || this.scrollTop;
  46. if (typeof container === 'function') {
  47. Promise.all([getRect(this, ContainerClass), this.getContainerRect()]).then(([root, container]) => {
  48. if (!root || !container)
  49. return;
  50. if (offsetTop + root.height > container.height + container.top) {
  51. this.setDataAfterDiff({
  52. isFixed: false,
  53. transform: container.height - root.height,
  54. });
  55. }
  56. else if (offsetTop >= root.top) {
  57. this.setDataAfterDiff({
  58. isFixed: true,
  59. height: root.height,
  60. transform: 0,
  61. });
  62. }
  63. else {
  64. this.setDataAfterDiff({ isFixed: false, transform: 0 });
  65. }
  66. });
  67. return;
  68. }
  69. getRect(this, ContainerClass).then((root) => {
  70. if (!root)
  71. return;
  72. if (offsetTop >= root.top) {
  73. this.setDataAfterDiff({ isFixed: true, height: root.height });
  74. this.transform = 0;
  75. }
  76. else {
  77. this.setDataAfterDiff({ isFixed: false });
  78. }
  79. });
  80. },
  81. setDataAfterDiff(data) {
  82. const { offsetTop } = this.properties;
  83. const { containerStyle: prevContainerStyle, contentStyle: prevContentStyle } = this.data;
  84. const { isFixed, height, transform } = data;
  85. wx.nextTick(() => {
  86. let containerStyle = '';
  87. let contentStyle = '';
  88. if (isFixed) {
  89. containerStyle += `height:${height}px;`;
  90. contentStyle += `position:fixed;top:${offsetTop}px`;
  91. }
  92. if (transform) {
  93. const translate = `translate3d(0, ${transform}px, 0)`;
  94. contentStyle += `-webkit-transform:${translate};transform:${translate};`;
  95. }
  96. if (prevContainerStyle !== containerStyle || prevContentStyle !== contentStyle) {
  97. this.setData({ containerStyle, contentStyle });
  98. }
  99. this.triggerEvent('scroll', {
  100. scrollTop: this.scrollTop,
  101. isFixed,
  102. });
  103. });
  104. },
  105. getContainerRect() {
  106. const nodesRef = this.properties.container();
  107. return new Promise((resolve) => nodesRef.boundingClientRect(resolve).exec());
  108. },
  109. };
  110. }
  111. ready() {
  112. this.onScroll();
  113. }
  114. };
  115. Sticky = __decorate([
  116. wxComponent()
  117. ], Sticky);
  118. export default Sticky;