sticky.js 5.0 KB

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