当前位置:   article > 正文

vue+element-ui carousel走马灯一次轮播(显示)5张图片_element-plus 走马灯实现单页多张图片轮播

element-plus 走马灯实现单页多张图片轮播

效果:

子组件定义

HTML

  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. >
  16. <div
  17. v-if="$parent.type === 'card'"
  18. v-show="!active"
  19. class="el-carousel__mask"
  20. ></div>
  21. <slot></slot>
  22. </div>
  23. </template>

JS

  1. <script>
  2. import { autoprefixer } from 'element-ui/src/utils/util'
  3. const CARD_SCALE = 0.83
  4. export default {
  5. name: 'ElCarouselItem',
  6. props: {
  7. name: String,
  8. label: {
  9. type: [String, Number],
  10. default: ''
  11. }
  12. },
  13. data () {
  14. return {
  15. hover: false,
  16. translate: 0,
  17. scale: 1,
  18. active: false,
  19. ready: false,
  20. inStage: false,
  21. specialIndex: false,
  22. animating: false
  23. }
  24. },
  25. created () {
  26. this.$parent && this.$parent.updateItems()
  27. },
  28. destroyed () {
  29. this.$parent && this.$parent.updateItems()
  30. },
  31. methods: {
  32. processIndex (index, activeIndex) {
  33. // console.log('activeIndex', activeIndex, index)
  34. // console.log('index', index)
  35. if (activeIndex == 0) {
  36. return index == 1 ? 1 : index == 2 ? 2 : index == 3 ? -2 : index == 4 ? -1 : 0
  37. }
  38. if (activeIndex == 1) {
  39. return index == 2 ? 1 : index == 3 ? 2 : index == 4 ? -2 : index == 0 ? -1 : 0
  40. }
  41. if (activeIndex == 2) {
  42. return index == 3 ? 1 : index == 4 ? 2 : index == 0 ? -2 : index == 1 ? -1 : 0
  43. }
  44. if (activeIndex == 3) {
  45. return index == 4 ? 1 : index == 0 ? 2 : index == 1 ? -2 : index == 2 ? -1 : 0
  46. }
  47. if (activeIndex == 4) {
  48. return index == 0 ? 1 : index == 1 ? 2 : index == 2 ? -2 : index == 3 ? -1 : 0
  49. }
  50. },
  51. calcCardTranslate (index) {
  52. return index * 180 + 320
  53. },
  54. calcTranslate (index, activeIndex, isVertical) {
  55. const distance = this.$parent.$el[isVertical ? 'offsetHeight' : 'offsetWidth']
  56. return distance * (index - activeIndex)
  57. },
  58. translateItem (index, activeIndex, oldIndex) {
  59. const parentType = this.$parent.type
  60. const parentDirection = this.parentDirection
  61. const length = this.$parent.items.length
  62. if (parentType !== 'card' && oldIndex !== undefined) {
  63. this.animating = index === activeIndex || index === oldIndex
  64. }
  65. index = this.processIndex(index, activeIndex, length)
  66. if (parentType === 'card') {
  67. if (parentDirection === 'vertical') {
  68. console.warn('[Element Warn][Carousel]vertical direction is not supported in card mode')
  69. }
  70. this.inStage = Math.round(Math.abs(index)) <= 1
  71. this.specialIndex = Math.round(Math.abs(index)) >= 3
  72. this.active = index === 0
  73. this.translate = this.calcCardTranslate(index, activeIndex)
  74. this.scale = Math.abs(index) == 0 ? 1 : Math.abs(index) == 1 ? 0.9 : Math.abs(index) == 2 ? 0.76 : 0.62
  75. } else {
  76. this.active = index === activeIndex
  77. const isVertical = parentDirection === 'vertical'
  78. this.translate = this.calcTranslate(index, activeIndex, isVertical)
  79. }
  80. this.ready = true
  81. },
  82. handleItemClick () {
  83. const parent = this.$parent
  84. if (parent && parent.type === 'card') {
  85. const index = parent.items.indexOf(this)
  86. parent.setActiveItem(index)
  87. }
  88. }
  89. },
  90. computed: {
  91. parentDirection () {
  92. return this.$parent.direction
  93. },
  94. itemStyle () {
  95. const translateType = this.parentDirection === 'vertical' ? 'translateY' : 'translateX'
  96. const value = `${translateType}(${this.translate}px) scale(${this.scale})`
  97. // console.log('转换类型', translateType)
  98. // console.log('偏移', this.translate)
  99. // console.log('大小', this.scale)
  100. const style = {
  101. transform: value
  102. }
  103. return autoprefixer(style)
  104. }
  105. }
  106. }
  107. </script>

CSS

  1. <style scoped>
  2. .el-carousel__arrow--left {
  3. left: -14px !important;
  4. }
  5. .el-carousel__arrow--right {
  6. right: -18px !important;
  7. }
  8. .el-carousel__item {
  9. cursor: pointer;
  10. z-index: 1;
  11. }
  12. .el-carousel__item--card.is-in-stage {
  13. z-index: 2;
  14. }
  15. .el-carousel__item--card.is-active {
  16. z-index: 3;
  17. }
  18. .specialIndex {
  19. z-index: 0;
  20. }
  21. </style>

父组件中使用

HTML

  1. <template>
  2. <div class="a0013-container">
  3. <div class="a0013-con bgcolor">
  4. <div class="a0013-tit">
  5. <img src="./default/tit.png" alt="" />
  6. </div>
  7. <div class="a0013-swiper">
  8. <el-carousel
  9. :interval="3000"
  10. type="card"
  11. height="320px"
  12. style="overflow-x: unset"
  13. >
  14. <Swiper v-for="(item, i) in dataList" :key="i">
  15. <div class="imgBox">
  16. <img class="imgItem" :src="item.photo.thumb" alt="" srcset="" />
  17. <div class="imgTit">{{ item.title }}</div>
  18. </div>
  19. </Swiper>
  20. </el-carousel>
  21. </div>
  22. </div>
  23. </div>
  24. </template>

JS

  1. <script>
  2. import { postCms } from "@/utils/http"
  3. import Swiper from "./component/swiper.vue"
  4. export default {
  5. name: "A0013", // 轮播图(5个)
  6. components: {
  7. Swiper
  8. },
  9. data () {
  10. return {
  11. dataList: [], // 专题数据列表
  12. }
  13. },
  14. created () {
  15. this.getData()
  16. },
  17. methods: {
  18. getData () {
  19. try {
  20. var params = {
  21. cardgroups: "Page13213213213",
  22. paging: { page_size: 1000, page_no: 1, last_id: "" },
  23. }
  24. postCms("/mobileinf/cardgroups", params)
  25. .then((res) => {
  26. res.cardgroups.forEach((item) => {
  27. this.dataList = item.cards
  28. })
  29. }).catch(err => console.log(err))
  30. } catch (err) {
  31. console.log(err)
  32. }
  33. }
  34. }
  35. }
  36. </script>

CSS

  1. <style lang="less" scoped>
  2. /deep/.el-carousel__arrow--left {
  3. left: -14px !important;
  4. }
  5. /deep/.el-carousel__arrow--right {
  6. right: -16px !important;
  7. }
  8. .a0013-container {
  9. width: 100%;
  10. .a0013-con {
  11. margin: 0 auto;
  12. width: 1200px;
  13. padding-bottom: 50px;
  14. .a0013-tit {
  15. width: 240px;
  16. margin-bottom: 30px;
  17. img {
  18. width: 240px;
  19. }
  20. }
  21. .a0013-swiper {
  22. width: 1200px;
  23. height: 100%;
  24. .el-carousel__item {
  25. width: 560px;
  26. padding: 10px;
  27. background-color: #f1f6fe;
  28. box-sizing: border-box;
  29. }
  30. .imgBox {
  31. position: relative;
  32. .imgItem {
  33. width: 540px;
  34. text-align: center;
  35. height: 300px;
  36. background-size: cover;
  37. }
  38. .imgTit {
  39. position: absolute;
  40. top: calc(50% - 36px);
  41. left: calc(50% - 138px);
  42. width: 276px;
  43. height: 72px;
  44. background: rgba(251, 251, 251, 0.55);
  45. border: 1px solid #fbfbfb;
  46. box-shadow: 0px 3px 28px 0px rgba(120, 129, 150, 0.3);
  47. border-radius: 5px;
  48. font-size: 43px;
  49. font-family: zihun100hao;
  50. font-weight: 600;
  51. color: #283147;
  52. line-height: 72px;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. </style>

因为elemengt-ui没有修改一次显示多个的属性, 找了好久找到一篇修改为一次显示6张的文章,且只有子组件没有父组件使用的代码。在此基础上进行修改进行使用,在此记录下来,方便以后使用。有兴趣的可以去原文章研究下。

参考文章:VUE ———— Element Carousel 走马灯 源码分析与改写 (显示多张)_Mickey_于浩的博客-CSDN博客_elementui走马灯卡片显示多个

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

闽ICP备14008679号