nuxtjs中使用vue-awesome-swiper自定义分页器

1、下载安装  官方文档:vue-awesome-swiper - npm

npm i vue-awesome-swiper@4.1.1

2、在plugins文件夹新建swiper.js  文件名可自定义

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
Vue.use(VueAwesomeSwiper)

3、在nuxt.comfig.js中配置引入

css: [
    'swiper/css/swiper.min.css',
 ],
  plugins: [
    {src:'@/plugins/swiper',ssr: false},
  ],

4、页面组件中引入使用

<template>
  <div>
    <div id="swiperWrapper">
        <div v-swiper:mySwiper="swiperOption" ref="mySwiper" class="swiper-container" @mouseenter="on_enter" @mouseleave="on_leave">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="(item,index) in list1" :key="index">
              <img style="width: 100%;height: 100%;" src="https://pigmcsdotcom.pigcms.com/kuaijing/zhsq/images/banner1.png">
            </div>
          </div>
          <div class="swiper-pagination" style="display: flex;align-items: center;justify-content: center;position: absolute;bottom:10rem;left: 0;width: 100%;"></div>
        </div>
      </div>
  </div>
</template>

<script>
export default{
    data(){
        return{
            list1:[1,2],
      swiperOption:{
        loop: true,
        autoplay: {
          disableOnInteraction: false,
          delay: 2000 //3秒切换一次
        },
        slidesPerView: 'auto', //slide容器能够同时显示的slides数量
        pagination: {
          el: '.swiper-pagination',
          type: 'custom', // 自定义轮播图bullet指示点
          renderCustom: function (swiper, current, total) {
            const activeColor = '#fff'
            const normalColor = '#618cf5'
            let color = ''
            let paginationStyle = ''
            let html = ''
            let width = '9rem'
            for (let i = 1; i <= total; i++) {
              if (i === current) {
                color = activeColor
                width = '30rem'
              } else {
                color = normalColor
                width = '9rem'
              }
              if (i === total) {
                paginationStyle = `background:${color};width: ${width}; height: 9rem;border-radius:9rem;opacity:1`
              } else {
                paginationStyle = `background:${color};margin-right: 8rem; width: ${width}; height: 9rem;border-radius:9rem;opacity:1`
              }
              html += `<span class="swiper-pagination-bullet" style="${paginationStyle}"></span>`
            }
            return html
          }
        }
      },
        }
    },
 computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    },
  },
methods:{
     on_enter () { //鼠标移入停止轮播
      this.swiper.autoplay.stop()
    },
    on_leave () {//鼠标移出继续轮播
      this.swiper.autoplay.start()
    },
}
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
vue-awesome-swiper,要自定义分页,可以使用pagination组件来进行自定义。以下是一个简单的示例: ```vue <template> <div> <swiper :options="swiperOptions"> <swiper-slide v-for="(slide, index) in slides" :key="index"> <!-- 内容 --> </swiper-slide> <!-- 自定义分页 --> <div class="custom-pagination" slot="pagination"> <span v-for="(slide, index) in slides" :key="index" :class="{ active: index === activeIndex }" @click="goToSlide(index)" > {{ index + 1 }} </span> </div> </swiper> </div> </template> <script> import { swiper, swiperSlide } from "vue-awesome-swiper"; export default { components: { swiper, swiperSlide, }, data() { return { swiperOptions: { // 设置其他选项 }, slides: [ // 设置轮播项数据 ], activeIndex: 0, }; }, methods: { goToSlide(index) { this.activeIndex = index; }, }, }; </script> <style> .custom-pagination { /* 样式自定义 */ } .custom-pagination span { /* 样式自定义 */ } .custom-pagination span.active { /* 样式自定义 */ } </style> ``` 在这个示例,我们首先导入`vue-awesome-swiper`的`swiper`和`swiperSlide`组件,然后在模板使用`swiper`组件包裹轮播项,并设置`options`属性来配置其他选项。 接下来,在`swiper`组件内部,我们使用`slot`属性将自定义分页的内容放在名为"pagination"的插槽。我们使用一个`v-for`循环来渲染分页的每个页码,并在点击时调用`goToSlide`方法来切换轮播项。 最后,我们可以通过样式来自定义分页的外观,通过修改`.custom-pagination`和`.custom-pagination span`的样式来实现自定义效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值