SoFunction
Updated on 2025-04-07

Example of using vue introducing swiper plug-in

This article introduces the introduction of the swiper plug-in for vue. I will share it with you. I hope it will be helpful to you.

Step 1:Install vue,

$ npm install vue 

Step 2:Create a vue project

# Global installation of vue-cli$ npm install -g vue-cli 
$ cd my-project 
$ npm install 
$ npm run dev 

The above are the installation of the vue project, the most important thing is the following steps

Step 3:Download the JS and CSS related to Swiper, put the JS in the static directory, and put the CSS in the assets directory.

Step 4:

Install runtime:

Terminal command: npm install babel-runtime

Step 5:

Modify. The file is as follows:

// /docs/user-guide/configuring 
 
 = { 
 root: true, 
 parser: 'babel-eslint', 
 parserOptions: { 
  sourceType: 'module' 
 }, 
 env: { 
  browser: true, 
 }, 
 // /feross/standard/blob/master/#javascript-standard-style 
 extends: 'standard', 
 // required to lint *.vue files 
 plugins: [ 
  'html' 
 ], 
 // add your custom rules here 
 'rules': { 
  // allow paren-less arrow functions 
  'arrow-parens': 0, 
  // allow async-await 
  'generator-star-spacing': 0, 
  // allow debugger during development 
  'no-debugger': .NODE_ENV === 'production' ? 2 : 0 
 }, 
 'globals': { 
  "Swiper": true 
 }  //This place is newly added Global injection} 

Step 6:Add carousel diagram code to your own vue file

<div v-on:mouseenter="stopPlay()" v-on:mouseleave="play()" class="swiper-container gallery-top swiper-container-horizontal"> 
  <div class="swiper-wrapper"> 
    <div v-for="value in lbt" class="swiper-slide swiper-slide-next" style="width: 100%; margin-right: 10px;" v-bind:style="{backgroundImage: 'url(' +  + ')'}"></div> 
  </div> 
  <div class="swiper-button-next swiper-button-white"></div> 
  <div class="swiper-button-prev swiper-button-white swiper-button-disabled"></div> 
</div> 
<div class="swiper-container gallery-thumbs swiper-container-horizontal"> 
  <div class="swiper-wrapper"> 
    <div v-for="value in lbt" class="swiper-slide swiper-slide-next" style="margin-right: 10px;" v-bind:style="{backgroundImage: 'url(' +  + ')'}"></div> 
  </div> 
</div> 
import Swiper from '../../static/swiper-3.4.' 
let galleryTop 
let galleryThumbs 
export default { 
 name: 'main', 
 data () { 
  return { 
   lbt: [ 
    { 
     'imgs': '../static/product/' 
    }, { 
     'imgs': '../static/product/' 
    }, { 
     'imgs': '../static/product/' 
    } 
   ] 
  } 
 }, 
 mounted () { 
  () 
 }, 
 methods: { 
  lunbo () { 
   galleryTop = new Swiper('.gallery-top', { 
    nextButton: '.swiper-button-next', 
    prevButton: '.swiper-button-prev', 
    spaceBetween: 10, 
    grabCursor: true, 
    initialSlide: 1, 
    autoplayDisableOnInteraction: false 
   }) 
   galleryThumbs = new Swiper('.gallery-thumbs', { 
    spaceBetween: 10, 
    autoplay: 4000, 
    initialSlide: 1, 
    centeredSlides: true, 
    slidesPerView: 'auto', 
    touchRatio: 0.2, 
    slideToClickedSlide: true, 
    autoplayDisableOnInteraction: false, 
    grabCursor: true 
   }) 
    = galleryThumbs 
    = galleryTop 
  }, 
  stopPlay () { 
   () 
   () 
  }, 
  play () { 
   () 
   () 
  } 
 } 
} 

@import url("../assets/swiper-3.4."); 
.gallery-top{  
  height:32rem;  
  width:100%; 
}  
.gallery-thumbs{  
  height:20%;  
  box-sizing:border-box;  
  padding:10px 0;  
  background: rgba(0, 0, 0, 0.4); 
  cursor: pointer; 
}  
.gallery-thumbs .swiper-slide{  
  width:30%;  
  height:6rem;  
  opacity:0.3;  
}  
.gallery-thumbs .swiper-slide-active{  
  opacity:1;  
}  
.swiper-slide{ 
 background-size: 100% 160%; 
 -webkit-background-size: 100% 160%; 
} 

There is another very important question here. Setting the background image in the template should be

v-bind:style="{backgroundImage: 'url(' +  + ')'}" 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.