sticky.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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`, `${prefix}-class-content`];
  19. this.properties = props;
  20. this.behaviors = [pageScrollMixin()];
  21. this.observers = {
  22. 'offsetTop, disabled, container'() {
  23. this.onScroll();
  24. },
  25. };
  26. this.data = {
  27. prefix,
  28. classPrefix: name,
  29. containerStyle: '',
  30. contentStyle: '',
  31. };
  32. this.methods = {
  33. onScroll(event) {
  34. const { scrollTop } = event || {};
  35. const { container, offsetTop, disabled } = this.properties;
  36. if (disabled) {
  37. this.setDataAfterDiff({
  38. isFixed: false,
  39. transform: 0,
  40. });
  41. return;
  42. }
  43. this.scrollTop = scrollTop || this.scrollTop;
  44. if (typeof container === 'function') {
  45. Promise.all([getRect(this, ContainerClass), this.getContainerRect()]).then(([root, container]) => {
  46. if (!root || !container)
  47. return;
  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 (!root)
  69. return;
  70. if (offsetTop >= root.top) {
  71. this.setDataAfterDiff({ isFixed: true, height: root.height });
  72. this.transform = 0;
  73. }
  74. else {
  75. this.setDataAfterDiff({ isFixed: false });
  76. }
  77. });
  78. },
  79. setDataAfterDiff(data) {
  80. const { offsetTop } = this.properties;
  81. const { containerStyle: prevContainerStyle, contentStyle: prevContentStyle } = this.data;
  82. const { isFixed, height, transform } = data;
  83. wx.nextTick(() => {
  84. let containerStyle = '';
  85. let contentStyle = '';
  86. if (isFixed) {
  87. containerStyle += `height:${height}px;`;
  88. contentStyle += `position:fixed;top:${offsetTop}px;left:0;right:0;`;
  89. }
  90. if (transform) {
  91. const translate = `translate3d(0, ${transform}px, 0)`;
  92. contentStyle += `-webkit-transform:${translate};transform:${translate};`;
  93. }
  94. if (prevContainerStyle !== containerStyle || prevContentStyle !== contentStyle) {
  95. this.setData({ containerStyle, contentStyle });
  96. }
  97. this.triggerEvent('scroll', {
  98. scrollTop: this.scrollTop,
  99. isFixed,
  100. });
  101. });
  102. },
  103. getContainerRect() {
  104. const nodesRef = this.properties.container();
  105. return new Promise((resolve) => nodesRef.boundingClientRect(resolve).exec());
  106. },
  107. };
  108. }
  109. ready() {
  110. this.onScroll();
  111. }
  112. };
  113. Sticky = __decorate([
  114. wxComponent()
  115. ], Sticky);
  116. export default Sticky;