最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3+Swiper.js實現(xiàn)無縫輪播圖組件的最佳實踐

 更新時間:2026年04月04日 10:30:25   作者:皮卡丘-杰尼龜  
vue是一款流行的前端框架,它提供了一系列的工具和組件,使得開發(fā)者可以更加便捷地創(chuàng)建交互式的Web應(yīng)用程序,輪播圖是Web應(yīng)用程序中常見的一種交互式組件,這篇文章主要介紹了Vue3+Swiper.js實現(xiàn)無縫輪播圖組件的最佳實踐,需要的朋友可以參考下

前言

在數(shù)據(jù)可視化大屏項目中,輪播圖組件是一個常見的需求。本文將詳細介紹如何使用 Vue3 和 Swiper.js 開發(fā)一個功能完善的無縫輪播圖組件,并分享實際項目中的最佳實踐。

技術(shù)棧

  • Vue 3 (Composition API)
  • TypeScript
  • Swiper.js
  • CSS3

組件概述

SeamlessCarousel 是一個基于 Swiper.js 的 Vue 3 輪播圖組件,專門用于展示虛擬電廠信息。該組件支持自動播放、手動導(dǎo)航、響應(yīng)式布局等功能,適用于數(shù)據(jù)可視化大屏展示。

組件功能預(yù)覽

我們即將開發(fā)的輪播圖組件具備以下功能:

? 自動播放與手動切換

? 中心突出顯示效果

? 響應(yīng)式布局

? 詳細信息展示

? 導(dǎo)航按鈕

? 數(shù)據(jù)綁定

步驟一:創(chuàng)建項目并安裝依賴

npm install swiper
npm install -D @types/swiper  # 如果使用TypeScript

步驟二:創(chuàng)建組件

  • 創(chuàng)建一個名為SeamlessCarousel.vue 的組件文件,并定義組件的屬性、數(shù)據(jù)、方法、事件等。
<template>
  <div class="seamless-carousel-container">
    <div class="carousel-wrapper">
      <!-- 主輪播 -->
      <swiper
        ref="mainSwiperRef"
        :preload-classes="true"
        :lazy="true"
        :initial-slide="0"
        :modules="modules"
        :space-between="180"
        :slides-per-view="3"
        :centered-slides="true"
        :loop="true"
        :loop-additional-slides="1"
        :grab-cursor="true"
        :effect="'slide'"
        :speed="800"
        :allow-touch-move="true"
        :watch-slides-progress="true"
        :watch-slides-visibility="true"
        :autoplay="{
          delay: 3000,
          disableOnInteraction: true,
          pauseOnMouseEnter: true,
          waitForTransition: false,
        }"
        :navigation="{
          nextEl: '.right-arrow',
          prevEl: '.left-arrow',
        }"
        class="main-swiper"
        @swiper="setMainSwiper"
        @slide-change="onSlideChange"
      >
        <swiper-slide v-for="(plant, index) in slides" :key="index">
          <div class="test-plant-card" :class="{
              'slide-center': activeIndex === index,
              'slide-side': activeIndex !== index,
              'initialized': activeIndex >= 0
            }">
            <div class="title" :title="plant.name">{{ plant.name }} </div>
            <div class="card-content">
              <img :src="getImage(plant.image)"/>
            <div class="common-content">
              <p>位置:{{ plant.location }}</p>
            </div>
            <div class="full-content">
              <p >類型:{{ plant.type }}</p>
              <p >數(shù)量:{{ plant.num }}臺</p>
            </div>
            </div>
          </div>
        </swiper-slide>
        <img ref="prevRef" class="left-arrow" src="@/assets/arrow-left.png"/>
        <img ref="nextRef" class="right-arrow" src="@/assets/arrow-right.png" />
      </swiper>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation, Autoplay, EffectFade, Controller } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/effect-fade';
// Swiper 模塊
const modules = [Navigation, Autoplay, EffectFade, Controller];
// Swiper 實例引用
const mainSwiperRef = ref<any>(null);
let mainSwiper: any = null;
// 當前激活的幻燈片索引
const activeIndex = ref(0);
// 獲取圖片資源
const getImage = (name: string) => {
  return new URL(`../assets/${name}.png`, import.meta.url).href
}
interface TestPlant {
  name: string
  location: string
  image?: string
  num?: number
  type?: string
}
// 定義組件屬性
interface Props {
  title?: string;
  autoPlayDelay?: number;
  speed?: number;
  hasLoaded?: boolean;
  plants: TestPlant[];
}
const props = withDefaults(defineProps<Props>(), {
  autoPlayDelay: 3000,
  speed: 800,
  hasLoaded: false,
  plants: []
});
// 定義事件
const emit = defineEmits(['swiper-change'])
// 設(shè)置主 Swiper 實例
const setMainSwiper = (swiper: any) => {
  mainSwiper = swiper;
};
// 幻燈片切換回調(diào)
const onSlideChange = (index) => {
  if (mainSwiper) {
    activeIndex.value = mainSwiper.realIndex;
    emit('swiper-change', slides.value[activeIndex.value]);
  }
};
// 使用傳入的幻燈片數(shù)據(jù)或默認數(shù)據(jù)
const slides = computed(() => props.plants || []);
// 監(jiān)聽數(shù)據(jù)加載狀態(tài)
watch(
  () => props.hasLoaded,
  (newVal) => {
    if (newVal && mainSwiper) {
      mainSwiper.update();
      emit('swiper-change', slides.value[mainSwiper.realIndex]);
    }
  },
  { immediate: true }
);
</script>
<style scoped>
<style>
.seamless-carousel-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}
.carousel-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: visible;
  padding: 0;
}
.swiper {
  height: 170px !important;
}
.swiper .left-arrow {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  display: inline-block;
  margin: auto;
  width: 34px;
  height: 34px;
  z-index: 99;
  cursor: pointer;
}
.swiper .right-arrow {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  display: inline-block;
  margin: auto;
  width: 34px;
  height: 34px;
  z-index: 99;
  cursor: pointer;
}
.main-swiper {
  width: 100%;
  height: 170px;
}
.main-swiper .swiper-slide {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  height: 170px;
}
.main-swiper .swiper-slide.swiper-slide-active .test-plant-card {
  width: 500px !important;
  border: 2px solid #FFA200;
}
/* 卡片樣式 */
.test-plant-card {
  overflow: hidden;
  transition: all 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.main-swiper .swiper-slide .test-plant-card {
  width: 190px !important;
  height: 170px;
  background: white;
  border-radius: 8px;
  padding: 22px 20px 20px;
  box-sizing: border-box;
  box-shadow: 0px 0px 10px 0px rgba(6, 0, 1, 0.05);
  flex-shrink: 0;
  transition: all 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.main-swiper .swiper-slide .test-plant-card .title {
  font-size: 20px;
  font-weight: 600;
  color: #666666;
  font-family: "MiSans-semibold";
  margin-bottom: 20px;
  width: 100%;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
/* 激活狀態(tài)下的樣式 */
.main-swiper .swiper-slide.swiper-slide-active .test-plant-card .title {
  color: #282828;
  margin-bottom: 18px;
}
</style>

使用方法

  1. 引入組件:在需要使用的地方引入組件:
<script setup>
import SeamlessCarousel from './SeamlessCarousel.vue';
// 如果需要監(jiān)聽卡片切換事件,請定義此方法
const handleSlideChange = (currentPlant) => {
  console.log('當前選中的卡片:', currentPlant);
};
</script>
  1. 定義數(shù)據(jù):定義需要展示的卡片數(shù)據(jù),可以是靜態(tài)數(shù)據(jù),也可以是動態(tài)數(shù)據(jù)。
<script setup>
const plants = [
    {
        name: 'Plant 1',
        location: 'Location 1',
        image: 'plant1.png',
        num: 10,
        type: 'Type 1'
    },
    ... more plants
]
</script>
  1. 配置參數(shù):根據(jù)需要配置組件的參數(shù),如自動播放延遲、切換速度、數(shù)據(jù)加載狀態(tài)等。
  2. 渲染組件:將組件渲染到頁面中。
<template>
  <SeamlessCarousel 
    :auto-play-delay="5000"
    :speed="1000"
    :has-loaded="true"
    @swiper-change="handleSlideChange"
  />
</template>
  1. 監(jiān)聽卡片切換事件:如果需要監(jiān)聽卡片切換事件,請定義一個方法,并綁定到組件的 swiper-change 事件上。

  2. 運行程序:在瀏覽器中運行程序,即可看到效果。

注意事項:

  1. 確保已安裝所需的依賴包。
  2. 確保已正確配置組件的參數(shù),如自動播放延遲、切換速度、數(shù)據(jù)加載狀態(tài)等。
  3. 確保已正確定義數(shù)據(jù)源,數(shù)據(jù)源可以是靜態(tài)數(shù)據(jù),也可以是動態(tài)數(shù)據(jù)。
  4. 確保已正確渲染組件,并綁定了監(jiān)聽事件。
  5. 運行程序并查看效果。

總結(jié) 

到此這篇關(guān)于Vue3+Swiper.js實現(xiàn)無縫輪播圖組件的文章就介紹到這了,更多相關(guān)Vue3+Swiper.js無縫輪播圖組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue iview實現(xiàn)動態(tài)新增和刪除

    vue iview實現(xiàn)動態(tài)新增和刪除

    這篇文章主要為大家詳細介紹了vue iview實現(xiàn)動態(tài)新增和刪除,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • vue.js 子組件無法獲取父組件store值的解決方式

    vue.js 子組件無法獲取父組件store值的解決方式

    今天小編就為大家分享一篇vue.js 子組件無法獲取父組件store值的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 利用Vue.js指令實現(xiàn)全選功能

    利用Vue.js指令實現(xiàn)全選功能

    最近做了兩個vue的項目,都需要實現(xiàn)全選反選的功能,兩個項目用了兩種實現(xiàn)方法,第一個項目用vue的computed,第二個項目用指令來實現(xiàn),用起來,發(fā)覺指令更加方便。下面就來介紹如何利用指令來實現(xiàn)全選。
    2016-09-09
  • 記VUE3+TS獲取組件類型的方法踩坑及解決

    記VUE3+TS獲取組件類型的方法踩坑及解決

    這篇文章主要介紹了VUE3+TS獲取組件類型的方法踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • cesium開發(fā)之如何在vue項目中使用cesium,使用離線地圖資源

    cesium開發(fā)之如何在vue項目中使用cesium,使用離線地圖資源

    這篇文章主要介紹了cesium開發(fā)之如何在vue項目中使用cesium,使用離線地圖資源問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue中npm如何設(shè)置倉庫地址

    vue中npm如何設(shè)置倉庫地址

    在使用npm命令時,如果直接從國外的倉庫下載依賴,下載速度很慢,甚至會下載不下來,我們可以更換npm的倉庫源,提高下載速度,這篇文章主要給大家介紹了關(guān)于vue中npm如何設(shè)置倉庫地址的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • 解決Vue路由導(dǎo)航報錯:NavigationDuplicated:?Avoided?redundant?navigation?to?current?location

    解決Vue路由導(dǎo)航報錯:NavigationDuplicated:?Avoided?redundant?navig

    這篇文章主要給大家介紹了關(guān)于解決Vue路由導(dǎo)航報錯:NavigationDuplicated:?Avoided?redundant?navigation?to?current?location的相關(guān)資料,這是最近做項目時候遇到的一個問題,現(xiàn)將解決辦法分享出來,需要的朋友可以參考下
    2023-01-01
  • SSM VUE Axios詳解

    SSM VUE Axios詳解

    Axios是在前端開發(fā)中常用的一個發(fā)送 HTTP 請求的庫,用過的都知道,篇文章主要給大家介紹了關(guān)于vue的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 解決vue路由name同名,路由重復(fù)的問題

    解決vue路由name同名,路由重復(fù)的問題

    這篇文章主要介紹了解決vue路由name同名,路由重復(fù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 前端vue實現(xiàn)甘特圖功能

    前端vue實現(xiàn)甘特圖功能

    dhtmlxGantt是一個強大的JavaScript Gantt圖表庫,提供易于使用、高度可自定義的Gantt圖表組件,它支持多項任務(wù)和進度條,以及多種列和行布局,可用于創(chuàng)建各種類型的時間線和計劃表,本文給大家介紹前端vue實現(xiàn)甘特圖的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2024-04-04

最新評論

盘山县| 正镶白旗| 交城县| 介休市| 万州区| 新和县| 梅河口市| 永善县| 株洲市| 赤城县| 阿拉善左旗| 益阳市| 馆陶县| 博乐市| 德昌县| 泉州市| 西华县| 古田县| 克山县| 阜新| 邻水| 萍乡市| 新平| 儋州市| 无锡市| 天气| 达孜县| 开江县| 灯塔市| 宣恩县| 牡丹江市| 唐山市| 洛宁县| 盱眙县| 利津县| 黄平县| 虎林市| 田东县| 阿城市| 苏尼特左旗| 桂阳县|