Vue 走马灯实现

这篇博客详细介绍了如何在Vue项目中使用Element UI库的carousel组件来创建一个轮播图,包括设置轮播间隔、类型、高度,并通过v-for指令绑定图片数组。同时,文章还提及了require关键字在导入静态资源时的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现效果:
在这里插入图片描述

官网组件:https://element.eleme.cn/#/zh-CN/component/installation

<template>
  <el-carousel :interval="4000" type="card" height="200px">
    <el-carousel-item v-for="(img, index) in imgArrag" :key="index">
      <a href="#">
        <img :src="img" alt="" width="100%" />
      </a>
      <router-view></router-view>
    </el-carousel-item>
  </el-carousel>
</template>
<script>
export default {
  data() {
    return {
      imgArrag: [
        require("../static/003.jpg"),
        require("../static/004.jpg"),
        require("../static/005.jpg"),
        require("../static/006.jpg"),
        require("../static/007.jpg"),
        require("../static/008.jpg"),
      ],
    };
  },
  methods: {},
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>

为什么要加 require 可见博客
https://blog.youkuaiyun.com/weixin_39031806/article/details/105445888

### Vue 中使用 Swiper 组件实现走马灯效果 为了在 Vue 应用程序中创建具有走马灯效果的轮播图,可以利用 `vue-awesome-swiper` 这一官方推荐的封装库来集成 Swiper 插件。下面是一个完整的实例说明如何配置和初始化带有暂停功能的 Swiper 轮播。 #### 安装依赖包 首先,在项目根目录下通过 npm 或 yarn 来安装所需的 swiper 和 vue-awesome-swiper: ```bash npm install --save swiper@^6.8.4 vue-awesome-swiper@^4.0.0 ``` 或者如果偏好 Yarn: ```bash yarn add swiper@^6.8.4 vue-awesome-swiper@^4.0.0 ``` #### 配置 Vue 项目 接着,在 main.js 文件里全局注册 Swiper 组件以便可以在任何地方调用它: ```javascript import Vue from 'vue' // 导入样式文件 import 'swiper/swiper-bundle.css' // 导入组件及其方法 import { Swiper, SwiperSlide } from 'vue-awesome-swiper' Vue.component('Swiper', Swiper) Vue.component('SwiperSlide', SwiperSlide) new Vue({ render: h => h(App), }).$mount('#app') ``` #### 创建轮播组件 最后一步是在单文件组件 `.vue` 中定义具体的轮播逻辑与模板结构如下所示: ```html <template> <div class="swiper-container"> <!-- 初始化并存储了轮播器 --> <swiper ref="mySwiper" :options="swiperOptions"> <swiper-slide v-for="(slide,index) in slidesData" :key="index">{{ slide }}</swiper-slide> <!-- 如果需要分页器 --> <div class="swiper-pagination"></div> <!-- 如果需要导航按钮 --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> <!-- 如果需要滚动条 --> <div class="swiper-scrollbar"></div> </swiper> </div> </template> <script> export default { name:'CarouselComponent', data(){ return{ slidesData:[1,2,3,4], // 数据源 swiperOptions:{ autoplay:{ delay:5000 }, // 自动播放间隔时间设置为五秒 loop:true, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, pagination: { el:'.swiper-pagination', clickable: true, } } }; }, mounted() { this.$refs.mySwiper.$swiper.on('mouseenter', function () { this.autoplay.stop(); // 当鼠标悬停时停止自动切换 }); this.$refs.mySwiper.$swiper.on('mouseleave', function () { this.autoplay.start(); // 当鼠标离开时重新启动自动切换 }); } }; </script> <style scoped lang="scss"> .swiper-container { height: 300px; width: 100%; background:#fff; .swiper-slide { text-align:center; font-size:18px; line-height:300px; color:#000; } /* 更多自定义样式 */ } </style> ``` 上述代码展示了如何在一个 Vue 单页面应用中引入 Swiper 并完成基本配置[^1]。此例子不仅实现了基础的图片/内容循环显示,还加入了当用户将光标移到轮播区域上时会暂时中断自动翻转的效果,并且提供了前后箭头以及底部指示点作为交互元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值