v-autoplay.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { mergeDeep } from "@vci/helper/src/object";
  2. import { isElement } from "@vci/helper/src/element";
  3. import EventDispatcher from "@vci/helper/src/EventDispatcher";
  4. export default class VciAutoplay extends EventDispatcher {
  5. constructor(option) {
  6. super();
  7. this.option = mergeDeep({
  8. el: null,
  9. page: 1,
  10. pages: 0,
  11. durationInterval: 1e3,
  12. enableAlternateReverse: false
  13. }, option);
  14. this.init();
  15. }
  16. init() {
  17. this.alias();
  18. this.isPaused = true;
  19. this.increment = 1;
  20. this.inter = -1;
  21. this.el && this.mount(this.el);
  22. }
  23. alias() {
  24. Object.defineProperty(this, "el", {
  25. get: () => this.option.el,
  26. set: el => { this.option.el = el; }
  27. });
  28. Object.defineProperty(this, "durationInterval", {
  29. get: () => this.option.durationInterval,
  30. set: durationInterval => {
  31. this.adaptModifyProps(() => this.option.durationInterval = durationInterval);
  32. }
  33. });
  34. Object.defineProperty(this, "enableAlternateReverse", {
  35. get: () => this.option.enableAlternateReverse,
  36. set: enableAlternateReverse => {
  37. this.adaptModifyProps(() => this.option.enableAlternateReverse = enableAlternateReverse);
  38. }
  39. });
  40. Object.defineProperty(this, "page", {
  41. get: () => this.option.page,
  42. set: page => {
  43. if(isNaN(+page)) console.warn(`page设置失败, page[${page}]不是一个数字`);
  44. else this.adaptModifyProps(() => {
  45. this.option.page = Math.max(Math.min(+page, this.pages), 1);
  46. this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
  47. });
  48. }
  49. });
  50. Object.defineProperty(this, "pages", {
  51. get: () => this.option.pages,
  52. set: pages => {
  53. if(isNaN(+pages)) console.warn(`pages设置失败, pages[${pages}]不是一个数字`);
  54. else this.adaptModifyProps(() => {
  55. this.option.pages = +pages;
  56. this.option.page = Math.max(Math.min(this.option.page, this.pages), 1);
  57. this.dispatchEvent("pages-change", { page: this.page, pages: this.pages });
  58. });
  59. }
  60. });
  61. Object.defineProperty(this, "enabled", {
  62. get: () => this.option.pages > 1
  63. });
  64. }
  65. mount(el) {
  66. if(!isElement(el)) throw new Error(`挂载元素时${el}不是一个HTML元素`);
  67. this.el = el;
  68. !this._pause && (this._pause = this.pause.bind(this));
  69. !this._resume && (this._resume = this.resume.bind(this));
  70. !this._wheel && (this._wheel = this.wheel.bind(this));
  71. this.el.addEventListener("pointermove", this._pause);
  72. this.el.addEventListener("pointerleave", this._resume);
  73. this.el.addEventListener("wheel", this._wheel);
  74. return this;
  75. }
  76. detach() {
  77. if(this.el) {
  78. this.el.removeEventListener("pointermove", this._pause);
  79. this.el.removeEventListener("pointerleave", this._resume);
  80. this.el.removeEventListener("wheel", this._wheel);
  81. this.el = null;
  82. }
  83. return this;
  84. }
  85. wheel(e) {
  86. clearTimeout(this.interWheel);
  87. this.interWheel = setTimeout(() => e["wheelDelta"] > 0 ? this.sub() : this.add(), 50);
  88. }
  89. pause() {
  90. this.isPaused = true;
  91. clearTimeout(this.inter);
  92. return this;
  93. }
  94. resume() {
  95. this.play();
  96. return this;
  97. }
  98. play() {
  99. if(!this.enabled) return this;
  100. this.isPaused = false;
  101. this.inter = setTimeout(() => {
  102. if(this.enableAlternateReverse) {
  103. this.option.page += this.increment;
  104. if(this.option.page > this.pages) {
  105. this.increment = -1;
  106. this.option.page = this.pages - 1;
  107. }
  108. if(this.option.page < 1) {
  109. this.increment = 1;
  110. this.option.page = 2;
  111. }
  112. } else {
  113. this.option.page++;
  114. this.option.page > this.pages && (this.option.page = 1);
  115. }
  116. this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
  117. this.play();
  118. }, this.durationInterval);
  119. return this;
  120. }
  121. start() {
  122. this.play();
  123. return this;
  124. }
  125. stop() {
  126. this.pause();
  127. this.option.page = 1;
  128. this.increment = 1;
  129. return this;
  130. }
  131. adaptModifyProps(handle) {
  132. const _isPaused = this.isPaused;
  133. !_isPaused && this.pause();
  134. handle && handle();
  135. !_isPaused && this.play();
  136. }
  137. adaptVue(vm, keyPage = "page") {
  138. vm.$watch(keyPage, page => this.option.page = Math.max(Math.min(+page, this.pages), 1));
  139. vm.$once("hook:beforeDestroy", () => this.destroy());
  140. return this;
  141. }
  142. add() {
  143. this.option.page++;
  144. if(this.option.page > this.pages) this.option.page = this.pages;
  145. this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
  146. }
  147. sub() {
  148. this.option.page--;
  149. if(this.option.page < 1) this.option.page = 1;
  150. this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
  151. }
  152. jump(page) {
  153. this.option.page = Math.min(Math.max(page, 1), this.pages);
  154. this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
  155. }
  156. destroy() {
  157. super.destroy();
  158. this
  159. .stop()
  160. .detach();
  161. }
  162. }