sticky.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 './utils';
  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': this.onScroll,
  25. };
  26. this.data = {
  27. containerStyle: '',
  28. contentStyle: '',
  29. classPrefix: `.${prefix}-sticky`,
  30. };
  31. }
  32. mounted() {
  33. this.onScroll();
  34. }
  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 (offsetTop + root.height > container.height + container.top) {
  49. this.setDataAfterDiff({
  50. isFixed: false,
  51. transform: container.height - root.height,
  52. });
  53. }
  54. else if (offsetTop >= root.top) {
  55. this.setDataAfterDiff({
  56. isFixed: true,
  57. height: root.height,
  58. transform: 0,
  59. });
  60. }
  61. else {
  62. this.setDataAfterDiff({ isFixed: false, transform: 0 });
  63. }
  64. });
  65. return;
  66. }
  67. getRect(this, ContainerClass).then((root) => {
  68. if (offsetTop >= root.top) {
  69. this.setDataAfterDiff({ isFixed: true, height: root.height });
  70. this.transform = 0;
  71. }
  72. else {
  73. this.setDataAfterDiff({ isFixed: false });
  74. }
  75. });
  76. }
  77. setDataAfterDiff(data) {
  78. const { offsetTop } = this.properties;
  79. const { containerStyle: prevContainerStyle, contentStyle: prevContentStyle } = this.data;
  80. const { isFixed, height, transform } = data;
  81. wx.nextTick(() => {
  82. let containerStyle = '';
  83. let contentStyle = '';
  84. if (isFixed) {
  85. containerStyle += `height:${height}px;`;
  86. contentStyle += `position:fixed;top:${offsetTop}px`;
  87. }
  88. if (transform) {
  89. const translate = `translate3d(0, ${transform}px, 0)`;
  90. contentStyle += `-webkit-transform:${translate};transform:${translate};`;
  91. }
  92. if (prevContainerStyle !== containerStyle || prevContentStyle !== contentStyle) {
  93. this.setData({ containerStyle, contentStyle });
  94. }
  95. this.triggerEvent('scroll', {
  96. scrollTop: this.scrollTop,
  97. isFixed,
  98. });
  99. });
  100. }
  101. getContainerRect() {
  102. const nodesRef = this.properties.container();
  103. return new Promise((resolve) => nodesRef.boundingClientRect(resolve).exec());
  104. }
  105. };
  106. Sticky = __decorate([
  107. wxComponent()
  108. ], Sticky);
  109. export default Sticky;
  110. //# sourceMappingURL=sticky.js.map