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
    评论
为什么要学习服务端渲染 nuxt.js ? 现在我们的项目大多数都是SPA(单页面应用),在实际开发过程单页面应用比之前的模板渲染要好很多,首先单页面应用是前后端分离,架构清晰,前端负责交互逻辑,后端负责数据,前后端单独开发,独立测试。但是,SPA不利于SEO(搜索引擎优化)。让搜索引擎更为信任该网站,通过提升排名获得更多网站流量,对于某些类型的网站是非常有必要的。目前大部分的Vue项目本质是 SPA 应用,React、Angular也都是SPA应用。SPA应用广泛用于对SEO要求不高的场景。在我们开发的过程,我们有 SEO 的需求,我们需要搜索引擎更多地抓取到我们的项目内容,此时我们需要SSR。SSR保证用户尽快看到基本的内容,也使得用户体验性更好。 Nuxt.js 是一个 Node 程序,基于vue.js开发的一套服务端渲染的框架,必须使用 Node 环境。我们对 Nuxt.js 应用的访问,实际上是在访问这个 Node.js 程序的路由,程序输出首屏渲染内容 + 用以重新渲染的 SPA 的脚本代码,而路由是由 Nuxt.js 约定好的 pages 文件夹生成的,开发只需要遵循一定的约定,直接使用vue.js开发我们项目也是非常轻松的。 课程案例 (1) HOME PAGE (2) Jokes Page  (3)About Page  课程概述 在本课程,大喵将使用 nuxt.js + bootstrapVue + json-server 开发实战性质一个入门级项目,带着大家来体验服务端渲染(SSR )项目构建的过程;介绍 nuxt.js项目目录的结构,每个文件夹和文件的基本概念和作用,以及nuxt.config.js 配置文件的基本介绍;页面公共结构处理,路由页面跳转配置处理;axios 接口请求;带着大家来熟悉及掌握 bootstrapVue UI组件库的使用
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、付费专栏及课程。

余额充值