scrollerX.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="scroller-main" :class="['scroller-' + status, (effect === false ? 'scroller-uneffect' : '')]">
  3. <div class="scroller-x" ref="scrollerx">
  4. <slot></slot>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'ScrollerX',
  11. props: ['effect'],
  12. data() {
  13. return {
  14. scrollX: 0,
  15. status: 'start',
  16. box: null,
  17. };
  18. },
  19. activated() {
  20. if (this.scrollX > 0) {
  21. this.$nextTick(() => {
  22. $A(this.$refs.scrollerx).scrollLeft(this.scrollX);
  23. });
  24. }
  25. },
  26. mounted() {
  27. this.$nextTick(() => {
  28. this.box = this.$refs.scrollerx;
  29. this.box.addEventListener('scroll', () => {
  30. this.scrollX = this.box.scrollLeft;
  31. if (this.box.scrollLeft > 0) {
  32. if (this.box.scrollWidth == this.box.clientWidth + this.box.scrollLeft) {
  33. this.status = 'end';
  34. } else {
  35. this.status = 'center';
  36. }
  37. } else {
  38. if (this.box.scrollWidth > this.box.clientWidth) {
  39. this.status = 'start';
  40. } else {
  41. this.status = '';
  42. }
  43. }
  44. }, false);
  45. });
  46. }
  47. }
  48. </script>
  49. <style lang="scss" scoped>
  50. .scroller-main {
  51. position: relative;
  52. .scroller-x {
  53. display: flex;
  54. width: 100%;
  55. flex-wrap: nowrap;
  56. justify-content: flex-start;
  57. align-items: center;
  58. overflow-x: auto;
  59. overflow-y: hidden;
  60. position: relative;
  61. -webkit-overflow-scrolling: touch;
  62. > div,
  63. > span {
  64. display: block;
  65. flex-grow: 1;
  66. flex-shrink: 0;
  67. flex-basis: auto;
  68. }
  69. }
  70. }
  71. .scroller-center:before,
  72. .scroller-end:before {
  73. position: absolute;
  74. left: 0;
  75. top: 0;
  76. bottom: 0;
  77. width: 64px;
  78. content: "";
  79. z-index: 1;
  80. background: linear-gradient(to right, #ffffff, rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0));
  81. pointer-events: none
  82. }
  83. .scroller-start:after,
  84. .scroller-center:after {
  85. position: absolute;
  86. right: 0;
  87. top: 0;
  88. bottom: 0;
  89. width: 64px;
  90. content: "";
  91. z-index: 1;
  92. background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.8), #ffffff);
  93. pointer-events: none
  94. }
  95. .scroller-uneffect {
  96. &:before,
  97. &:after {
  98. display: none;
  99. }
  100. }
  101. </style>