vue引入swiper插件

步骤一:安装vue,
           $ npm install vue
步骤二:创建vue项目
          # 全局安装 vue-cli
          $ npm install -g vue-cli
          $ cd my-project
          $ npm install
          $ npm run dev

上面这些就是安装好vue项目,最主要的就是下面的步骤

步骤三:下载好swiper相关的js和css,js放在static目录下,css放在assets目录下。
步骤四:
安装runtime:
终端命令:npm install babel-runtime
步骤五:
修改.eslintrc.js文件如下:
// http://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parser: 'babel-eslint',
  parserOptions: {
    sourceType: 'module'
  },
  env: {
    browser: true,
  },
  // https://github.com/feross/standard/blob/master/RULES.md#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': process.env.NODE_ENV === 'production' ? 2 : 0
  },
  'globals': {
    "Swiper": true
  }   //这个地方是新加入的   全局注入
}
步骤六:在自己的vue文件中添加轮播图代码
    <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(' + value.imgs + ')'}"></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(' + value.imgs + ')'}"></div>
        </div>
    </div>
import Swiper from '../../static/swiper-3.4.2.min.js'
let galleryTop
let galleryThumbs
export default {
  name: 'main',
  data () {
    return {
      lbt: [
        {
          'imgs': '../static/product/lbt1.jpg'
        }, {
          'imgs': '../static/product/lbt2.jpg'
        }, {
          'imgs': '../static/product/lbt3.jpg'
        }
      ]
    }
  },
  mounted () {
    this.lunbo()
  },
  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
      })
      galleryTop.params.control = galleryThumbs
      galleryThumbs.params.control = galleryTop
    },
    stopPlay () {
      galleryTop.stopAutoplay()
      galleryThumbs.stopAutoplay()
    },
    play () {
      galleryTop.startAutoplay()
      galleryThumbs.startAutoplay()
    }
  }
}
@import url("../assets/swiper-3.4.2.min.css");
.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%;
}
这里还有一个很重要的问题,在模板里面设置背景图,写法应该是
v-bind:style="{backgroundImage: 'url(' + value.imgs + ')'}"








  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Vue引入Swiper可以通过以下几个步骤: 1. 首先,你需要在Vue项目中安装Swiper库。可以使用npm或者yarn命令进行安装,例如: ``` npm install swiper ``` 或者 ``` yarn add swiper ``` 2. 接下来,你需要在你的Vue组件中引入Swiper的CSS和JavaScript文件。可以使用import语句将它们导入到你的组件中,例如: ```javascript import Swiper from 'swiper/bundle'; import 'swiper/swiper-bundle.css'; ``` 3. 在你的组件中,你需要准备一个容器元素来包裹你的轮播图。这个容器元素可以使用任何自定义的类名,但是官方推荐使用`swiper-container`这个类名。例如: ```html <div class="swiper-container" ref="mySwiper"> <!-- 轮播图内容 --> </div> ``` 4. 在组件的`mounted`钩子函数中,你可以使用Swiper的构造函数创建一个Swiper实例,并将容器元素作为参数传入。此外,你可以根据需要配置Swiper的选项。例如: ```javascript mounted() { new Swiper(this.$refs.mySwiper, { // 配置选项 loop: true, // 循环模式 // 其他配置项... }); } ``` 通过以上步骤,你就可以在Vue中成功引入和使用Swiper轮播图件了。记得根据你的项目具体需求进行配置和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [在vue引入swiper组件步骤](https://blog.csdn.net/a1598452168YY/article/details/128496489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vue中使用swiper件](https://blog.csdn.net/qq_51580852/article/details/126375211)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值