vue3+Typescript+glide.js轮播图

161 阅读2分钟

由于swiper插件过于笨重,glide.js是轻量级的轮播插件库。

npm i @glidejs/glide

完整实例代码如下

<template>
  <div class="glide">
    <div class="glide__track" data-glide-el="track">
      <div class="glide__slides">
        <div class="glide__slide" v-for="(item, index) in glideList" :key="index">
          <div class="slide-caption">
            <button class="explore-btn">探索更多</button>
          </div>
          <div class="backdrop"></div>
          <h1>{{ item.h1 }}</h1>
          <h3>{{ item.h3 }}</h3>
          <!-- <img :src="item.src" alt="" /> -->
        </div>
      </div>
    </div>

    <div class="glide__arrows" data-glide-el="controls">
      <button class="glide__arrow glide__arrow--left" data-glide-dir="<">&lt;</button>
      <button class="glide__arrow glide__arrow--right" data-glide-dir=">">&gt;</button>
    </div>

    <div class="glide__bullets" data-glide-el="controls[nav]">
      <button
        v-for="(item, index) in glideList"
        :key="index"
        class="glide__bullet"
        :class="{ active: nowActive === index }"
        :data-glide-dir="'=' + index"
      ></button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import Glide from '@glidejs/glide'
import '@glidejs/glide/dist/css/glide.core.min.css'

// 类型定义
interface GlideItem {
  h1: string
  h3: string
  src: string
  type: string
}

interface GlideConfig {
  type: string
  startAt: number
  autoplay: number
  hoverpause: boolean
}

// 响应式数据
const glide = ref<any>(null)
const nowActive = ref(0)

const glideConfig: GlideConfig = {
  type: 'carousel',
  startAt: 0,
  autoplay: 4000,
  hoverpause: false
}

const glideList = ref<GlideItem[]>([
  {
    h1: '科技推动人类进步,研发引领实业发展。',
    h3: '自力更生是中华民族自立于世界民族之林的奋斗基点,自主创新是我们攀登世界科技高峰的必由之路。',
    src: '/src/assets/img/glide1.jpg', // 注意更新图片路径
    type: 'img'
  },
  {
    h1: '科技推动人类进步,研发引领实业发展。',
    h3: '自力更生是中华民族自立于世界民族之林的奋斗基点,自主创新是我们攀登世界科技高峰的必由之路。',
    src: '/src/assets/img/glide2.jpg',
    type: 'img'
  },
  {
    h1: '科技推动人类进步,研发引领实业发展。',
    h3: '自力更生是中华民族自立于世界民族之林的奋斗基点,自主创新是我们攀登世界科技高峰的必由之路。',
    src: '/src/assets/img/glide3.jpg',
    type: 'img'
  }
])

// 初始化方法
const initGlide = () => {
  glide.value = new Glide('.glide', glideConfig)
  glide.value.on('move', () => {
    nowActive.value = glide.value.index
  })
  glide.value.mount()
}

// 生命周期
onMounted(() => {
  initGlide()
})
</script>

<style lang="scss" scoped>
.glide {
  position: relative;
  z-index: 10;
  width: 400px;
  height: 400px;

  &__slide {
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 100vw;
      max-width: 100%;
      height: 100vh;
      object-fit: cover;
    }
  }
}

.backdrop {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 60;
  width: 100%;
  height: 100%;
  background: rgb(42 42 42 / 69%);
  opacity: 0.3;
}

.glide__arrows {
  position: absolute;
  top: 50%;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.glide__arrow {
  width: 40px;
  height: 40px;
  margin: 0 20px;
  font-size: 18px;
  color: white;
  cursor: pointer;
  background: rgb(255 255 255 / 30%);
  border: 1px white;
  border-radius: 5px;

  &:hover {
    background: rgb(255 255 255 / 50%);
  }

  &:active {
    background: rgb(255 255 255 / 70%);
  }
}

.glide__bullets {
  position: absolute;
  bottom: 15px;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.glide__bullet {
  width: 35px;
  height: 5px;
  margin: 0 5px;
  border: none;

  &.active {
    background-color: darkorange;
  }
}

.slide-caption {
  position: absolute;
  z-index: 70;
  max-width: 60vw;
  color: #e7e9ec;
  text-align: center;
  pointer-events: auto;

  > * {
    opacity: 0;
  }

  h1 {
    font-size: 54px;
    font-weight: 600;
  }

  h3 {
    margin: 48px 0;
    font-size: 24px;
  }
}

.explore-btn {
  padding: 14px 32px;
  font-size: 18px;
  color: #e7e9ec;
  cursor: pointer;
  background-color: #fb6354;
  border: 0;
  border-radius: 4px;
  outline: none;
}
</style>