Vue2首页banner轮播

简介: 这篇文章介绍了如何在Vue 2框架中创建一个首页轮播图(Banner)组件,允许自定义轮播数据、切换间隔、宽度和高度,并提供了组件的实现代码和使用示例。

可自定义设置以下属性:

  • banner数组(bannerData)
  • 切换间隔(interval),默认3000ms
  • 宽度(width),默认100%
  • 高度(height),默认600px

效果如图:

69028166c2a9711ee4d5c2b1482460d9.png

①创建Banner.vue组件:

<template>
  <div class="m-banner-wrap" :style="`width: ${width}; height: ${height}`" v-if="bannerData.length">
    <div class="m-banner-list">
      <div
        class="u-banner-item fade"
        v-for="(item, index) in bannerData"
        :key="index"
        v-show="index===activeIndex"
        :style="`background: url(${item.imgUrl}) no-repeat center; background-size: cover;`"
        @mouseenter="onStop" @mouseleave="onStart">
        <a :href="item.link" target="_blank"></a>
      </div>
    </div>
    <div class="m-dot-list" v-if="bannerData.length > 1">
      <div v-for="n of bannerData.length" :key="n" :class="['u-dot-item', {active: (n-1)===activeIndex}]" @mouseenter="onEnter(n-1)">
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Banner',
  props: {
    bannerData: { // banner数组
      type: Array,
      default: () => {
        return []
      }
    },
    interval: { // 切换间隔ms
      type: Number,
      default: 3000
    },
    width: { // 宽度
      type: String,
      default: '100%'
    },
    height: { // 高度
      type: String,
      default: '600px'
    }
  },
  data () {
    return {
      activeIndex: 0,
      timer: null
    }
  },
  mounted () {
    setTimeout(() => {
      this.onStart()
    }, 1000)
    window.onfocus = () => { // 页面激活
      this.onStart()
    }
    window.onblur = () => { // 页面失焦
      this.onStop()
    }
  },
  methods: {
    onStart () {
      clearInterval(this.timer)
      if (this.bannerData.length > 1) {
        this.timer = setInterval(this.onMove, this.interval)
      }
    },
    onStop () {
      clearInterval(this.timer)
    },
    onEnter (index) {
      clearInterval(this.timer)
      this.activeIndex = index
      this.onStart()
    },
    onMove () {
      if (this.activeIndex === this.bannerData.length - 1) {
        this.activeIndex = 0
      } else {
        this.activeIndex++
      }
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
    this.timer = null
  }
}
</script>
<style lang="less" scoped>
@themeColor: #1890FF;
.m-banner-wrap {
  width: 100%;
  height: 585px;
  min-width: 1200px;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
  z-index: 9;
  .m-banner-list {
    height: 100%;
    .u-banner-item {
      min-width: 1200px;
      height: 100%;
      width: 100%;
      a {
        display: block;
        height: 100%;
      }
    }
    .fade { // 切换banner时的过渡效果
      -webkit-animation: fadein 0.3s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
      animation: fadein 0.3s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
    }
    @-webkit-keyframes fadein {
      0% {
        opacity: 0;
      }
      100% {
        opacity: 1;
      }
    }
    @keyframes fadein {
      0% {
        opacity: 0;
      }
      100% {
        opacity: 1;
      }
    }
  }
  .m-dot-list {
    width: 100%;
    line-height: 4px;
    position: absolute;
    bottom: 65px;
    text-align: center;
    .u-dot-item { // 圆点样式
      display: inline-block;
      width: 36px;
      height: 4px;
      background: #E3E3E3;
      border-radius: 1px;
      vertical-align: bottom;
      cursor: pointer;
      &:not(:last-child) {
        margin-right: 10px;
      }
    }
    .active { // 圆点选中样式
      background: @themeColor;
    }
  }
}
</style>

②在要使用的页面引入:

<Banner :bannerData="bannerData" :interval="3000"/>
import Banner from '@/components/Banner'
components: {
    Banner
}
bannerData: [
    {
        title: 'image-1',
        link: 'javascript:;',
        imgUrl: require('@/assets/images/1.jpg')
    },
    {
        title: 'image-2',
        link: 'javascript:;',
        imgUrl: require('@/assets/images/2.jpg')
    },
    {
        title: 'image-3',
        link: 'javascript:;',
        imgUrl: require('@/assets/images/3.jpg')
    }
]

相关文章
|
缓存 负载均衡 网络协议
|
JavaScript 开发者
【Vue3 组件封装】vue3 轮播图组件封装
【Vue3 组件封装】vue3 轮播图组件封装
277 0
|
Docker 容器
尝试添加 --skip-broken 来跳过无法安装的软件包 或 --nobest 来不只使用最佳选择的软件包
尝试添加 --skip-broken 来跳过无法安装的软件包 或 --nobest 来不只使用最佳选择的软件包
1845 0
尝试添加 --skip-broken 来跳过无法安装的软件包 或 --nobest 来不只使用最佳选择的软件包
|
12月前
|
敏捷开发 安全 测试技术
PingCode
【10月更文挑战第19天】PingCode
559 62
|
JavaScript
Vue3搜索框(InputSearch)
这篇文章介绍了如何在Vue 3中创建一个具有搜索、清除、加载状态等多功能的搜索框组件(InputSearch),并提供了组件的配置选项、事件处理和使用示例。
1013 6
Vue3搜索框(InputSearch)
|
JavaScript API UED
Vue3使用触摸滑动插件(Swiper)
本文介绍如何在Vue2项目中使用Swiper插件实现触摸滑动功能,并封装了两种轮播图展示形式:首页轮播图(`type: banner`)和走马灯轮播图(`type: carousel`),以及信息展播模式(`type: broadcast`)。支持自定义轮播图片、区域尺寸、动画效果等属性。通过示例代码展示了不同切换动画及自定义效果,并提供了在线预览。适用于多种应用场景,提升用户体验。
695 1
Vue3使用触摸滑动插件(Swiper)
Vue2信息提示(Modal)
这是一个基于 Vue3 的信息提示模态框(Modal)组件,提供了丰富的自定义属性,包括标题、内容、宽度、按钮文本等。它支持两种模式:确认提示框(confirm)和信息提示框(info),并有六种不同的展示效果。模态框可以水平垂直居中或固定高度水平居中显示,支持加载中状态。该组件模仿了 ant-design-vue 的样式,适用于各种场景下的信息提示。
283 1
Vue2信息提示(Modal)
|
JavaScript
Vue3抽屉(Drawer)
这是一个基于Vue的抽屉组件(Drawer),提供了丰富的自定义选项,如宽度、高度、标题、关闭按钮等,并支持顶部、右侧、底部和左侧四种方向。
517 0
Vue3抽屉(Drawer)
|
存储 缓存 分布式计算
Hadoop配置文件core-site.xml
【7月更文挑战第17天】
894 2
Hadoop配置文件core-site.xml