Slider.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. let idPrefix = 'dcat-slider-',
  2. template = `<div id="{id}" class="slider-panel {class}">
  3. <div class="slider-content position-fixed p-1 ps ps--active-y"></div>
  4. </div>`;
  5. export default class Slider {
  6. constructor(Dcat, options) {
  7. let _this = this;
  8. _this.options = $.extend({
  9. target: null,
  10. class: null,
  11. autoDestory: true,
  12. }, options);
  13. _this.id = idPrefix + Dcat.helpers.random();
  14. _this.$target = $(_this.options.target);
  15. _this.$container = $(
  16. template
  17. .replace('{id}', _this.id)
  18. .replace('{class}', _this.options.class || '')
  19. );
  20. _this.$container.appendTo('body');
  21. _this.$container.find('.slider-content').append(_this.$target);
  22. // 滚动条
  23. new PerfectScrollbar(`#${_this.id} .slider-content`);
  24. if (_this.options.autoDestory) {
  25. // 刷新或跳转页面时移除面板
  26. Dcat.onPjaxComplete(() => {
  27. _this.destroy();
  28. });
  29. }
  30. }
  31. open() {
  32. this.$container.addClass('open');
  33. }
  34. close() {
  35. this.$container.removeClass('open');
  36. }
  37. toggle() {
  38. this.$container.toggleClass('open');
  39. }
  40. destroy() {
  41. this.$container.remove()
  42. }
  43. }