vue+element-ui carousel走马灯一次轮播(显示)5张图片

这篇具有很好参考价值的文章主要介绍了vue+element-ui carousel走马灯一次轮播(显示)5张图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

效果:

vue+element-ui carousel走马灯一次轮播(显示)5张图片

子组件定义

HTML

<template>
  <div
    v-show="ready"
    class="el-carousel__item"
    :class="{
      'is-active': active,
      'el-carousel__item--card': $parent.type === 'card',
      'is-in-stage': inStage,
      specialIndex: specialIndex,
      'is-hover': hover,
      'is-animating': animating,
    }"
    @click="handleItemClick"
    :style="itemStyle"
  >
    <div
      v-if="$parent.type === 'card'"
      v-show="!active"
      class="el-carousel__mask"
    ></div>
    <slot></slot>
  </div>
</template>

JS

<script>
import { autoprefixer } from 'element-ui/src/utils/util'
const CARD_SCALE = 0.83
export default {
  name: 'ElCarouselItem',
  props: {
    name: String,
    label: {
      type: [String, Number],
      default: ''
    }
  },

  data () {
    return {
      hover: false,
      translate: 0,
      scale: 1,
      active: false,
      ready: false,
      inStage: false,
      specialIndex: false,
      animating: false
    }
  },
  created () {
    this.$parent && this.$parent.updateItems()
  },

  destroyed () {
    this.$parent && this.$parent.updateItems()
  },

  methods: {
    processIndex (index, activeIndex) {
      // console.log('activeIndex', activeIndex, index)
      // console.log('index', index)
      if (activeIndex == 0) {
        return index == 1 ? 1 : index == 2 ? 2 : index == 3 ? -2 : index == 4 ? -1 : 0
      }
      if (activeIndex == 1) {
        return index == 2 ? 1 : index == 3 ? 2 : index == 4 ? -2 : index == 0 ? -1 : 0
      }
      if (activeIndex == 2) {
        return index == 3 ? 1 : index == 4 ? 2 : index == 0 ? -2 : index == 1 ? -1 : 0
      }
      if (activeIndex == 3) {
        return index == 4 ? 1 : index == 0 ? 2 : index == 1 ? -2 : index == 2 ? -1 : 0
      }
      if (activeIndex == 4) {
        return index == 0 ? 1 : index == 1 ? 2 : index == 2 ? -2 : index == 3 ? -1 : 0
      }
    },

    calcCardTranslate (index) {
      return index * 180 + 320
    },

    calcTranslate (index, activeIndex, isVertical) {
      const distance = this.$parent.$el[isVertical ? 'offsetHeight' : 'offsetWidth']
      return distance * (index - activeIndex)
    },

    translateItem (index, activeIndex, oldIndex) {
      const parentType = this.$parent.type
      const parentDirection = this.parentDirection
      const length = this.$parent.items.length
      if (parentType !== 'card' && oldIndex !== undefined) {
        this.animating = index === activeIndex || index === oldIndex
      }
      index = this.processIndex(index, activeIndex, length)
      if (parentType === 'card') {
        if (parentDirection === 'vertical') {
          console.warn('[Element Warn][Carousel]vertical direction is not supported in card mode')
        }
        this.inStage = Math.round(Math.abs(index)) <= 1
        this.specialIndex = Math.round(Math.abs(index)) >= 3
        this.active = index === 0
        this.translate = this.calcCardTranslate(index, activeIndex)
        this.scale = Math.abs(index) == 0 ? 1 : Math.abs(index) == 1 ? 0.9 : Math.abs(index) == 2 ? 0.76 : 0.62
      } else {
        this.active = index === activeIndex
        const isVertical = parentDirection === 'vertical'
        this.translate = this.calcTranslate(index, activeIndex, isVertical)
      }
      this.ready = true
    },

    handleItemClick () {
      const parent = this.$parent
      if (parent && parent.type === 'card') {
        const index = parent.items.indexOf(this)
        parent.setActiveItem(index)
      }
    }
  },

  computed: {
    parentDirection () {
      return this.$parent.direction
    },

    itemStyle () {
      const translateType = this.parentDirection === 'vertical' ? 'translateY' : 'translateX'
      const value = `${translateType}(${this.translate}px) scale(${this.scale})`
      // console.log('转换类型', translateType)
      // console.log('偏移', this.translate)
      // console.log('大小', this.scale)
      const style = {
        transform: value
      }
      return autoprefixer(style)
    }
  }
}
</script>

CSS

<style scoped>
.el-carousel__arrow--left {
  left: -14px !important;
}
.el-carousel__arrow--right {
  right: -18px !important;
}
.el-carousel__item {
  cursor: pointer;
  z-index: 1;
}
.el-carousel__item--card.is-in-stage {
  z-index: 2;
}
.el-carousel__item--card.is-active {
  z-index: 3;
}
.specialIndex {
  z-index: 0;
}
</style>

父组件中使用

HTML

<template>
  <div class="a0013-container">
    <div class="a0013-con bgcolor">
      <div class="a0013-tit">
        <img src="./default/tit.png" alt="" />
      </div>
      <div class="a0013-swiper">
        <el-carousel
          :interval="3000"
          type="card"
          height="320px"
          style="overflow-x: unset"
        >
          <Swiper v-for="(item, i) in dataList" :key="i">
            <div class="imgBox">
              <img class="imgItem" :src="item.photo.thumb" alt="" srcset="" />
              <div class="imgTit">{{ item.title }}</div>
            </div>
          </Swiper>
        </el-carousel>
      </div>
    </div>
  </div>
</template>

JS

<script>
import { postCms } from "@/utils/http"

import Swiper from "./component/swiper.vue"
export default {
  name: "A0013", // 轮播图(5个)
  components: {
    Swiper
  },
  data () {
    return {
      dataList: [], // 专题数据列表
    }
  },
  created () {
    this.getData()
  },
  methods: {
    getData () {
      try {
        var params = {
          cardgroups: "Page13213213213",
          paging: { page_size: 1000, page_no: 1, last_id: "" },
        }
        postCms("/mobileinf/cardgroups", params)
          .then((res) => {
            res.cardgroups.forEach((item) => {
              this.dataList = item.cards
            })
          }).catch(err => console.log(err))
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

CSS

<style lang="less" scoped>
/deep/.el-carousel__arrow--left {
  left: -14px !important;
}
/deep/.el-carousel__arrow--right {
  right: -16px !important;
}
.a0013-container {
  width: 100%;
  .a0013-con {
    margin: 0 auto;
    width: 1200px;
    padding-bottom: 50px;
    .a0013-tit {
      width: 240px;
      margin-bottom: 30px;
      img {
        width: 240px;
      }
    }
    .a0013-swiper {
      width: 1200px;
      height: 100%;
      .el-carousel__item {
        width: 560px;
        padding: 10px;
        background-color: #f1f6fe;
        box-sizing: border-box;
      }
      .imgBox {
        position: relative;
        .imgItem {
          width: 540px;
          text-align: center;
          height: 300px;
          background-size: cover;
        }
        .imgTit {
          position: absolute;
          top: calc(50% - 36px);
          left: calc(50% - 138px);
          width: 276px;
          height: 72px;
          background: rgba(251, 251, 251, 0.55);
          border: 1px solid #fbfbfb;
          box-shadow: 0px 3px 28px 0px rgba(120, 129, 150, 0.3);
          border-radius: 5px;
          font-size: 43px;
          font-family: zihun100hao;
          font-weight: 600;
          color: #283147;
          line-height: 72px;
        }
      }
    }
  }
}
</style>

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

参考文章:VUE ———— Element Carousel 走马灯 源码分析与改写 (显示多张)_Mickey_于浩的博客-CSDN博客_elementui走马灯卡片显示多个文章来源地址https://www.toymoban.com/news/detail-504731.html

到了这里,关于vue+element-ui carousel走马灯一次轮播(显示)5张图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 自定义element-ui走马灯(轮播)样式

    自定义el-carousel-item指示器样式 把指示器变成圆点 效果图:  

    2024年02月13日
    浏览(32)
  • Element UI 走马灯的使用

    目录 走马灯是什么 原生js实现 Element UI的走马灯使用 el-carousel Carousel Events el-carousel-item 在有限空间内,循环播放同一类型的图片、文字等内容,走马灯也叫轮播图。 比如   JS实现轮播图效果(同时播放音频)_trigger333的博客-CSDN博客 Element - The world\\\'s most popular Vue UI framework 代

    2024年02月02日
    浏览(41)
  • elementUI 轮播图 ----Carousel 走马灯笔记

    2024年02月05日
    浏览(117)
  • ElementUI浅尝辄止14:Carousel 走马灯

    在有限空间内,循环播放同一类型的图片、文字等内容 结合使用 el-carousel 和 el-carousel-item 标签就得到了一个走马灯。幻灯片的内容是任意的,需要放在 el-carousel-item 标签中。默认情况下,在鼠标 hover 时底部的指示器时就会触发切换。通过设置 trigger 属性为 click ,可以达到点

    2024年02月10日
    浏览(35)
  • 制作轮播图经验分享——element ui走马灯的使用(附源码,效果截图)

    先附上效果图:   element ui链接地址:Carousel 走马灯 | Element Plus (gitee.io) 源码: 这里我使用的是静态图片(本地) 经验分享: 在制作轮播图的时候经常会发现图片大小不一,这时候可以在img标签里加上width:100%;height:100%即可实现图片铺满。 当然,这只是轮播图的一种,还

    2024年02月11日
    浏览(32)
  • element-plus走马灯不显示

    依赖正确,代码用法正确,但是element-plu走马灯就是不显示!! 并且盒子的宽度总是0 在现有的布局中插入官方的案例,也不能显示 但是把整个页面都使用官方案例就可以正常显示 所以,怀疑时自己的样式冲突了 就content一个盒子,看到 display: flex; 就知道不对劲,去掉flex布

    2024年02月07日
    浏览(27)
  • transition 实现div伸缩动画、3D翻转动画(vue版)、elementui走马灯

    代码   template     div         div               el-carousel :interval=\\\"4000\\\" type=\\\"card\\\" height=\\\"500px\\\"                   el-carousel-item v-for=\\\"(i,index) in imageData\\\" :key=\\\"index\\\"                     img :src=\\\"i.src\\\" style=\\\"width: 100%;height: 100%;\\\"                   /el-carousel-item       

    2024年02月02日
    浏览(26)
  • 用 React+ts 实现无缝滚动的走马灯

    走马灯是一种常见的网页交互组件,可以展示多张图片或者内容,通过自动播放或者手动切换的方式,让用户能够方便地浏览多张图片或者内容。 本次实现的不是轮播图而是像传送带一样的无限滚动的形式。 走马灯可设置一下属性: 滚动速度 滚动方向 一屏要显示项的个数

    2024年02月13日
    浏览(28)
  • 实现NoticeBar 通知栏。走马灯公告栏

    微信小程序封装公共组件——实现NoticeBar 通知栏。走马灯公告栏 代码如下(示例): index.wxml代码如下(示例): index.ts 公告通知栏父组件传notice数组,组件设置了timer可以多条进行轮播

    2024年02月10日
    浏览(40)
  • Proteus仿真--基于51单片机的走马灯实现(仿真文件+程序)

    本文主要介绍基于51单片机的走马灯仿真(完整仿真源文件及代码见文末链接) 本设计中有16个LED灯用于流水走马演示,一位数码管用于显示当前模式状态,3个按键分别用于选择模式及加减速度控制 仿真图如下 其中 K1:用于模式切换选择,有多种模式可切换 K2:加速流水/走

    2024年02月06日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包