当前位置:   article > 正文

VUE ———— Element Carousel 走马灯 源码分析与改写 (显示多张)_vue中走马灯显示多个图片

vue中走马灯显示多个图片

用过Element Carousel 组件的 应该都知道 ,他只能显示3个item, 而没有提供显示数量设置的属性,那如果想要显示多个,就要来改下他的源码,一起看下吧!

原本样式:

改动后:

首先在浏览器里先看下他的样式:

首先我们将隐藏的item 显示出来 发现他是这个样子的

 

我们会发现 他会在左面后者右面有两个item 是重叠的   其实不重叠 直接就可以用啦  哈哈  我们就在这个基础上面改就好了!

 

下面来看下代码: 首先carousel组件在element-ui/packages/carousel 就可以找到 主要是main.vue和item.vue两个文件

main.vue 这里不需要动,只需要改item.vue就可以,先看下item.vue的代码:

整个的处理主要有三个变量  

1. index : 每个item的原本索引值

2. activeIndex :  当前显示item的索引值  就是比例最大的那个

3. length: item的数量

下面看下 item.vue里面的方法:

 1. 这个方法是根据item的index和当前的activeIndex 以及 length 去重新排序item的位置的一个方法

2. 这个是在上面的方法之后,计算已经排序好的每个 item 的 偏移量

3. 再来看下这个方法 (代码中注释说明)

  1. translateItem(index, activeIndex, oldIndex) {
  2. const parentType = this.$parent.type;
  3. const parentDirection = this.parentDirection;
  4. const length = this.$parent.items.length;
  5. if (parentType !== 'card' && oldIndex !== undefined) {
  6. this.animating = index === activeIndex || index === oldIndex;
  7. }
  8. if (index !== activeIndex && length > 2 && this.$parent.loop) {
  9. // 这里调用上面第一个方法 计算每个item应该在的位置
  10. index = this.processIndex(index, activeIndex, length);
  11. }
  12. if (parentType === 'card') {
  13. if (parentDirection === 'vertical') {
  14. console.warn('[Element Warn][Carousel]vertical direction is not supported in card mode');
  15. }
  16. this.inStage = Math.round(Math.abs(index - activeIndex)) <= 1; // 这里变量用来判断class的 当前显示的和左右两个为true 提升了z-index 等级
  17. this.active = index === activeIndex; // 这个变量是用来判断当前主要展示的item 给了一个class z-index 等级最高
  18. this.translate = this.calcCardTranslate(index, activeIndex); // 这里调用上面第二个方法 得到每个位置item的偏移量
  19. this.scale = this.active ? 1 : CARD_SCALE;
  20. } else {
  21. this.active = index === activeIndex;
  22. const isVertical = parentDirection === 'vertical';
  23. this.translate = this.calcTranslate(index, activeIndex, isVertical);
  24. }
  25. this.ready = true;
  26. },

Ok,下面是我修改后的 item.vue 完整代码

  1. <template>
  2. <div
  3. v-show="ready"
  4. class="el-carousel__item"
  5. :class="{
  6. 'is-active': active,
  7. 'el-carousel__item--card': $parent.type === 'card',
  8. 'is-in-stage': inStage,
  9. 'specialIndex': specialIndex,
  10. 'is-hover': hover,
  11. 'is-animating': animating
  12. }"
  13. @click="handleItemClick"
  14. :style="itemStyle">
  15. <div
  16. v-if="$parent.type === 'card'"
  17. v-show="!active"
  18. class="el-carousel__mask">
  19. </div>
  20. <slot></slot>
  21. </div>
  22. </template>
  23. <script>
  24. import { autoprefixer } from 'element-ui/src/utils/util';
  25. const CARD_SCALE = 0.83;
  26. export default {
  27. name: 'ElCarouselItem',
  28. props: {
  29. name: String,
  30. label: {
  31. type: [String, Number],
  32. default: ''
  33. }
  34. },
  35. data() {
  36. return {
  37. hover: false,
  38. translate: 0,
  39. scale: 1,
  40. active: false,
  41. ready: false,
  42. inStage: false,
  43. specialIndex: false,
  44. animating: false
  45. };
  46. },
  47. methods: {
  48. processIndex(index, activeIndex, length) {
  49. if(activeIndex == 0) {
  50. return index==1?1:index==2?2:index==3?-3:index==4?-2:index==5?-1:0
  51. }
  52. if(activeIndex == 1) {
  53. return index==2?1:index==3?2:index==4?-3:index==5?-2:index==0?-1:0
  54. }
  55. if(activeIndex == 2) {
  56. return index==3?1:index==4?2:index==5?-3:index==0?-2:index==1?-1:0
  57. }
  58. if(activeIndex == 3) {
  59. return index==4?1:index==5?2:index==0?-3:index==1?-2:index==2?-1:0
  60. }
  61. if(activeIndex == 4) {
  62. return index==5?1:index==0?2:index==1?-3:index==2?-2:index==3?-1:0
  63. }
  64. if(activeIndex == 5) {
  65. return index==0?1:index==1?2:index==2?-3:index==3?-2:index==4?-1:0
  66. }
  67. },
  68. calcCardTranslate(index, activeIndex) {
  69. return index * 80 * 2
  70. },
  71. calcTranslate(index, activeIndex, isVertical) {
  72. const distance = this.$parent.$el[isVertical ? 'offsetHeight' : 'offsetWidth'];
  73. return distance * (index - activeIndex);
  74. },
  75. translateItem(index, activeIndex, oldIndex) {
  76. const parentType = this.$parent.type;
  77. const parentDirection = this.parentDirection;
  78. const length = this.$parent.items.length;
  79. if (parentType !== 'card' && oldIndex !== undefined) {
  80. this.animating = index === activeIndex || index === oldIndex;
  81. }
  82. index = this.processIndex(index, activeIndex, length);
  83. if (parentType === 'card') {
  84. if (parentDirection === 'vertical') {
  85. console.warn('[Element Warn][Carousel]vertical direction is not supported in card mode');
  86. }
  87. this.inStage = Math.round(Math.abs(index)) <= 1;
  88. this.specialIndex = Math.round(Math.abs(index)) >= 3;
  89. this.active = index === 0;
  90. this.translate = this.calcCardTranslate(index, activeIndex);
  91. this.scale = Math.abs(index)==0 ? 1 : Math.abs(index)==1? 0.83 : Math.abs(index)==2? 0.73 : Math.abs(index)==3? 0.65 : 0.58;
  92. } else {
  93. this.active = index === activeIndex;
  94. const isVertical = parentDirection === 'vertical';
  95. this.translate = this.calcTranslate(index, activeIndex, isVertical);
  96. }
  97. this.ready = true;
  98. },
  99. handleItemClick() {
  100. const parent = this.$parent;
  101. if (parent && parent.type === 'card') {
  102. const index = parent.items.indexOf(this);
  103. parent.setActiveItem(index);
  104. }
  105. }
  106. },
  107. computed: {
  108. parentDirection() {
  109. return this.$parent.direction;
  110. },
  111. itemStyle() {
  112. const translateType = this.parentDirection === 'vertical' ? 'translateY' : 'translateX';
  113. const value = `${translateType}(${ this.translate }px) scale(${ this.scale })`;
  114. const style = {
  115. transform: value
  116. };
  117. return autoprefixer(style);
  118. }
  119. },
  120. created() {
  121. this.$parent && this.$parent.updateItems();
  122. },
  123. destroyed() {
  124. this.$parent && this.$parent.updateItems();
  125. }
  126. };
  127. </script>
  128. <style>
  129. .el-carousel__arrow--left {
  130. left: -426px
  131. }
  132. .el-carousel__arrow--right {
  133. right: -25px;
  134. }
  135. .el-carousel__item {
  136. cursor: pointer;
  137. z-index: 1;
  138. }
  139. .el-carousel__item--card.is-in-stage {
  140. z-index: 2;
  141. }
  142. .el-carousel__item--card.is-active {
  143. z-index: 3;
  144. }
  145. .specialIndex{
  146. z-index: 0
  147. }
  148. </style>

主要就是在上面提到的三个方法上面做了改动,我只显示六张,所以就是按照显示6张 写死的值,懒得计算啦  哈哈哈  但是大致思路就是这样  如果显示数量不一样 数值稍作改动就可以啦!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/250925
推荐阅读
相关标签
  

闽ICP备14008679号