18664ee9fc9d5e826849cbe2f1e3d370ca114c84722e3d9085c82492833db2a3416a1952e29d489c965d6448035904e59162f5b4e4a878d54a7cb28603bf08 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <div
  3. :class="carouselClasses"
  4. @mouseenter.stop="handleMouseEnter"
  5. @mouseleave.stop="handleMouseLeave">
  6. <div
  7. class="el-carousel__container"
  8. :style="{ height: height }">
  9. <transition
  10. v-if="arrowDisplay"
  11. name="carousel-arrow-left">
  12. <button
  13. type="button"
  14. v-show="(arrow === 'always' || hover) && (loop || activeIndex > 0)"
  15. @mouseenter="handleButtonEnter('left')"
  16. @mouseleave="handleButtonLeave"
  17. @click.stop="throttledArrowClick(activeIndex - 1)"
  18. class="el-carousel__arrow el-carousel__arrow--left">
  19. <i class="el-icon-arrow-left"></i>
  20. </button>
  21. </transition>
  22. <transition
  23. v-if="arrowDisplay"
  24. name="carousel-arrow-right">
  25. <button
  26. type="button"
  27. v-show="(arrow === 'always' || hover) && (loop || activeIndex < items.length - 1)"
  28. @mouseenter="handleButtonEnter('right')"
  29. @mouseleave="handleButtonLeave"
  30. @click.stop="throttledArrowClick(activeIndex + 1)"
  31. class="el-carousel__arrow el-carousel__arrow--right">
  32. <i class="el-icon-arrow-right"></i>
  33. </button>
  34. </transition>
  35. <slot></slot>
  36. </div>
  37. <ul
  38. v-if="indicatorPosition !== 'none'"
  39. :class="indicatorsClasses">
  40. <li
  41. v-for="(item, index) in items"
  42. :key="index"
  43. :class="[
  44. 'el-carousel__indicator',
  45. 'el-carousel__indicator--' + direction,
  46. { 'is-active': index === activeIndex }]"
  47. @mouseenter="throttledIndicatorHover(index)"
  48. @click.stop="handleIndicatorClick(index)">
  49. <button class="el-carousel__button">
  50. <span v-if="hasLabel">{{ item.label }}</span>
  51. </button>
  52. </li>
  53. </ul>
  54. </div>
  55. </template>
  56. <script>
  57. import throttle from 'throttle-debounce/throttle';
  58. import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event';
  59. export default {
  60. name: 'ElCarousel',
  61. props: {
  62. initialIndex: {
  63. type: Number,
  64. default: 0
  65. },
  66. height: String,
  67. trigger: {
  68. type: String,
  69. default: 'hover'
  70. },
  71. autoplay: {
  72. type: Boolean,
  73. default: true
  74. },
  75. interval: {
  76. type: Number,
  77. default: 3000
  78. },
  79. indicatorPosition: String,
  80. indicator: {
  81. type: Boolean,
  82. default: true
  83. },
  84. arrow: {
  85. type: String,
  86. default: 'hover'
  87. },
  88. type: String,
  89. loop: {
  90. type: Boolean,
  91. default: true
  92. },
  93. direction: {
  94. type: String,
  95. default: 'horizontal',
  96. validator(val) {
  97. return ['horizontal', 'vertical'].indexOf(val) !== -1;
  98. }
  99. }
  100. },
  101. data() {
  102. return {
  103. items: [],
  104. activeIndex: -1,
  105. containerWidth: 0,
  106. timer: null,
  107. hover: false
  108. };
  109. },
  110. computed: {
  111. arrowDisplay() {
  112. return this.arrow !== 'never' && this.direction !== 'vertical';
  113. },
  114. hasLabel() {
  115. return this.items.some(item => item.label.toString().length > 0);
  116. },
  117. carouselClasses() {
  118. const classes = ['el-carousel', 'el-carousel--' + this.direction];
  119. if (this.type === 'card') {
  120. classes.push('el-carousel--card');
  121. }
  122. return classes;
  123. },
  124. indicatorsClasses() {
  125. const classes = ['el-carousel__indicators', 'el-carousel__indicators--' + this.direction];
  126. if (this.hasLabel) {
  127. classes.push('el-carousel__indicators--labels');
  128. }
  129. if (this.indicatorPosition === 'outside' || this.type === 'card') {
  130. classes.push('el-carousel__indicators--outside');
  131. }
  132. return classes;
  133. }
  134. },
  135. watch: {
  136. items(val) {
  137. if (val.length > 0) this.setActiveItem(this.initialIndex);
  138. },
  139. activeIndex(val, oldVal) {
  140. this.resetItemPosition(oldVal);
  141. if (oldVal > -1) {
  142. this.$emit('change', val, oldVal);
  143. }
  144. },
  145. autoplay(val) {
  146. val ? this.startTimer() : this.pauseTimer();
  147. },
  148. loop() {
  149. this.setActiveItem(this.activeIndex);
  150. },
  151. interval() {
  152. this.pauseTimer();
  153. this.startTimer();
  154. }
  155. },
  156. methods: {
  157. handleMouseEnter() {
  158. this.hover = true;
  159. this.pauseTimer();
  160. },
  161. handleMouseLeave() {
  162. this.hover = false;
  163. this.startTimer();
  164. },
  165. itemInStage(item, index) {
  166. const length = this.items.length;
  167. if (index === length - 1 && item.inStage && this.items[0].active ||
  168. (item.inStage && this.items[index + 1] && this.items[index + 1].active)) {
  169. return 'left';
  170. } else if (index === 0 && item.inStage && this.items[length - 1].active ||
  171. (item.inStage && this.items[index - 1] && this.items[index - 1].active)) {
  172. return 'right';
  173. }
  174. return false;
  175. },
  176. handleButtonEnter(arrow) {
  177. if (this.direction === 'vertical') return;
  178. this.items.forEach((item, index) => {
  179. if (arrow === this.itemInStage(item, index)) {
  180. item.hover = true;
  181. }
  182. });
  183. },
  184. handleButtonLeave() {
  185. if (this.direction === 'vertical') return;
  186. this.items.forEach(item => {
  187. item.hover = false;
  188. });
  189. },
  190. updateItems() {
  191. this.items = this.$children.filter(child => child.$options.name === 'ElCarouselItem');
  192. },
  193. resetItemPosition(oldIndex) {
  194. this.items.forEach((item, index) => {
  195. item.translateItem(index, this.activeIndex, oldIndex);
  196. });
  197. },
  198. playSlides() {
  199. if (this.activeIndex < this.items.length - 1) {
  200. this.activeIndex++;
  201. } else if (this.loop) {
  202. this.activeIndex = 0;
  203. }
  204. },
  205. pauseTimer() {
  206. if (this.timer) {
  207. clearInterval(this.timer);
  208. this.timer = null;
  209. }
  210. },
  211. startTimer() {
  212. if (this.interval <= 0 || !this.autoplay || this.timer) return;
  213. this.timer = setInterval(this.playSlides, this.interval);
  214. },
  215. resetTimer() {
  216. this.pauseTimer();
  217. this.startTimer();
  218. },
  219. setActiveItem(index) {
  220. if (typeof index === 'string') {
  221. const filteredItems = this.items.filter(item => item.name === index);
  222. if (filteredItems.length > 0) {
  223. index = this.items.indexOf(filteredItems[0]);
  224. }
  225. }
  226. index = Number(index);
  227. if (isNaN(index) || index !== Math.floor(index)) {
  228. console.warn('[Element Warn][Carousel]index must be an integer.');
  229. return;
  230. }
  231. let length = this.items.length;
  232. const oldIndex = this.activeIndex;
  233. if (index < 0) {
  234. this.activeIndex = this.loop ? length - 1 : 0;
  235. } else if (index >= length) {
  236. this.activeIndex = this.loop ? 0 : length - 1;
  237. } else {
  238. this.activeIndex = index;
  239. }
  240. if (oldIndex === this.activeIndex) {
  241. this.resetItemPosition(oldIndex);
  242. }
  243. this.resetTimer();
  244. },
  245. prev() {
  246. this.setActiveItem(this.activeIndex - 1);
  247. },
  248. next() {
  249. this.setActiveItem(this.activeIndex + 1);
  250. },
  251. handleIndicatorClick(index) {
  252. this.activeIndex = index;
  253. },
  254. handleIndicatorHover(index) {
  255. if (this.trigger === 'hover' && index !== this.activeIndex) {
  256. this.activeIndex = index;
  257. }
  258. }
  259. },
  260. created() {
  261. this.throttledArrowClick = throttle(300, true, index => {
  262. this.setActiveItem(index);
  263. });
  264. this.throttledIndicatorHover = throttle(300, index => {
  265. this.handleIndicatorHover(index);
  266. });
  267. },
  268. mounted() {
  269. this.updateItems();
  270. this.$nextTick(() => {
  271. addResizeListener(this.$el, this.resetItemPosition);
  272. if (this.initialIndex < this.items.length && this.initialIndex >= 0) {
  273. this.activeIndex = this.initialIndex;
  274. }
  275. this.startTimer();
  276. });
  277. },
  278. beforeDestroy() {
  279. if (this.$el) removeResizeListener(this.$el, this.resetItemPosition);
  280. this.pauseTimer();
  281. }
  282. };
  283. </script>