vue使用swiper(轮播图)-真实项目使用

本文介绍了如何在Vue项目中安装并使用vue-awesome-swiper插件,展示了基本的轮播组件用法,以及在实际项目中展示数据库中找到的CVE漏洞列表的应用。
摘要由CSDN通过智能技术生成

一、安装
我直接安装的vue-awesome-swiper": "^3.1.3"指定版本

npm install vue-awesome-swiper@3.1.3 swiper --save

二、vue页面使用,写了一个小demo

<template>
  <div class="vue-swiper">
    <h1>{{ msg }}</h1>
    <div class="container">
      <swiper
        :options="swiperOption"
        ref="mySwiper"
        class="swiper-container-no-flexbox"
      >
        <!-- 这种写法也可以 -->
        <!-- <swiper-slide>
          <img src="../../static/images/1.jpg" alt class="1">
        </swiper-slide>
        <swiper-slide>
          <img src="../../static/images/2.jpg" alt class="2">
        </swiper-slide>
        <swiper-slide>
          <img src="../../static/images/3.jpg" alt class="3">
        </swiper-slide>
        <swiper-slide>
          <img src="../../static/images/4.jpg" alt class="4">
        </swiper-slide>
        <swiper-slide>
          <img src="../../static/images/5.jpg" alt class="5">
        </swiper-slide> -->

        <!-- 这种写法更常见 -->
        <swiper-slide v-for="(item, index) in bugs1" :key="index">
          <img class="swiper-img" :src="item" alt="" />
        </swiper-slide>
      </swiper>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import VueAwesomeSwiper from "vue-awesome-swiper";
Vue.use(VueAwesomeSwiper);
import Swiper from "swiper";

export default {
  name: "vueSwiper",
  data() {
    return {
      msg: "Vue-swiper",
      swiperOption: {
        notNextTick: true,
        loop: true, // 启循环模式,即滑动到最后一个 slide 后会自动切换到第一个 slide。
        slidesPerView: 3,
        centeredSlides: true,
        autoplay: {
          delay: 2000, // 自动播放间隔时间,单位为毫秒
          disableOnInteraction: false, // 用户操作后是否停止自动播放
        },
        forceToAxis: true, //鼠标竖向滚动无法控制横向切换
        slideToClickedSlide: true, //设置为true则点击slide会过渡到这个slide。
        grabCursor: true, // 当鼠标位于 swiper 上时显示抓取样式。
        setWrapperSize: true, // 根据 slides 的尺寸自动调整容器的尺寸。
        mousewheelControl: true, // 开启鼠标滚轮控制 swiper。
        observeParents: true, // 如果 swiper 元素有父级元素,并且父级元素的尺寸发生变化时,swiper 会重新初始化。
      },
      bugs1: [
        "http://g.hiphotos.baidu.com/zhidao/pic/item/c83d70cf3bc79f3d6e7bf85db8a1cd11738b29c0.jpg",
        "http://big5.wallcoo.com/photograph/summer_feeling/images/%5Bwallcoo.com%5D_summer_feeling_234217.jpg",
        "https://img0.baidu.com/it/u=4281860672,1981139753&fm=253&fmt=auto&app=138&f=JPEG?w=480&h=320",
        "https://img0.baidu.com/it/u=2862534777,914942650&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        "https://img0.baidu.com/it/u=2862534777,914942650&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
        "https://img0.baidu.com/it/u=2862534777,914942650&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
      ],
    };
  },
};
</script>


<style lang="scss" scoped>
.container {
  // width: 500px;
  overflow: hidden;
  margin: 0 auto;
  border: 1px solid #000;
}
img {
  max-width: 100%;
  display: block;
}
.swiper-slide-shadow-right,
.swiper-slide-shadow-left {
  background-image: none !important;
  opacity: 0 !important;
}

.swiper-slide {
  transform: scale(0.7);
  transition: all 0.3s linear;
}
.swiper-slide.swiper-slide-active {
  transform: scale(1);
}
@import "../../node_modules/swiper/dist/css/swiper.min.css";
</style>

三、真实项目使用

<template>
  <div class="cve-wrapper">
    <h1>数据库中找到的 CVE</h1>
    <div class="pcSwiper">
      <swiper
        :options="swiperOption"
        ref="mySwiper"
        class="swiper-container-no-flexbox"
      >
        <swiper ref="mySwiper" class="swiper-wrapper1" :options="swiperOption">
          <swiper-slide
            v-for="(val, index) in bugs1"
            :key="index"
            class="swiper-box"
          >
            <div class="swiperItem">
              <div class="tag-list">
                <p class="stag">{{ val.name }}</p>
                <p class="stag">{{ val.reference.length }}</p>
              </div>
              <div class="ref-list">
                <template v-for="(value, indexs) in val.reference">
                  <p v-if="indexs < 8" class="stag">{{ value }}</p>
                </template>
              </div>
            </div>
          </swiper-slide>
        </swiper>
        <swiper
          ref="swiper2"
          style="margin-top: 24px"
          class="swiper-wrapper2"
          :options="swiperOptions2"
        >
          <swiper-slide
            v-for="(val, index) in bugs2"
            :key="index"
            class="swiper-box"
          >
            <div class="swiperItem">
              <div class="tag-list">
                <p class="stag">{{ val.name }}</p>
                <p class="stag">{{ val.reference.length }}</p>
              </div>
              <div class="ref-list">
                <template v-for="(value, indexs) in val.reference">
                  <p v-if="indexs < 8" class="stag">{{ value }}</p>
                </template>
              </div>
            </div>
          </swiper-slide>
        </swiper>
      </swiper>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import VueAwesomeSwiper from "vue-awesome-swiper";
Vue.use(VueAwesomeSwiper);
import bugs from "/@/utils/data/yhBugs";

export default {
  name: "vueSwiper",
  data() {
    return {
      msg: "Vue-swiper",
      bugs,
      swiperOption: {
        spaceBetween: 24,
        notNextTick: true,
        loop: true,
        slidesPerView: 3,
        centeredSlides: true,
        autoplay: {
          delay: 0, // 自动播放间隔时间,单位为毫秒
          disableOnInteraction: false, // 用户操作后是否停止自动播放
        },
        speed: 6000,
        forceToAxis: true, //鼠标竖向滚动无法控制横向切换
        slideToClickedSlide: true, //设置为true则点击slide会过渡到这个slide。
        grabCursor: true,
        setWrapperSize: true,
        // mousewheelControl: true,
        observeParents: true,
        slidesOffsetBefore: 324,
        loopAdditionalSlides: 5,
      },
    };
  },
  computed: {
    swiperOptions2() {
      return Object.assign({}, this.swiperOption, { slidesOffsetBefore: 120 });
    },
    bugs1() {
      return this.bugs.slice(0, Math.round(this.bugs.length / 2));
    },
    bugs2() {
      return this.bugs.slice(
        Math.round(this.bugs.length / 2),
        this.bugs.length
      );
    },
  },
};
</script>
<style lang="stylus" scoped>
// @import './swiper.min.css';
@import '../../../../node_modules/swiper/dist/css/swiper.min.css';

.cve-wrapper {
  width: 100%;
  color: #000;
  background: linear-gradient(180deg, rgba(243, 248, 252, 0.7) 0%, rgba(243, 248, 252, 0.3) 100%);

  .pcSwiper {
    margin-bottom: 80px;
  }

  .moblieSwiper {
    display: none;
  }

  >h1 {
    margin: 60px auto 32px;
    text-align: center;
    font-style: normal;
    font-weight: 500;
    font-size: 24px;
    line-height: 36px;
    color: #333333;
  }

  .swiper-wrapper1, .swiper-wrapper2 {
    .swiper-box {
      background: linear-gradient(180deg, #FFFFFF 0%, #FFFFFF 80.73%, rgba(255, 255, 255, 0.85) 100%);
      backdrop-filter: blur(22.5px);
      border-radius: 4px;
      width: 384px !important;
      height: 200px;

      .tag-list {
        display: flex;
        padding: 12px 16px;
        padding-bottom: 0;

        .stag {
          font-style: normal;
          font-weight: 500;
          font-size: 14px;
          color: #666666;
          background: #F0F6FE;
          border-radius: 2px;
          padding: 5px 8px;
          margin-right: 8px;
        }
      }

      .desc {
        font-style: normal;
        font-weight: 700;
        font-size: 14px;
        color: #333333;
        margin-bottom: 14px;
        padding-left: 16px;
      }

      .ref-list {
        display: flex;
        flex-wrap: wrap;
        padding: 10px 16px;
        padding-top: 0;

        .stag {
          border-left: 1px #eee solid;
          padding: 0 20px;
          margin-top: 16px;
          width: 133px;
          text-align: center;
          color: #666666;
          font-style: normal;
          font-weight: 400;
          font-size: 14px;
        }

        .stag:nth-of-type(1), .stag:nth-of-type(5) {
          padding-left: 0;
          border: unset;
          text-align: left;
        }
      }
    }
  }
}

@media (max-width: 750px) {
  .swiper-pagination {
    left: 40%;
    padding-top: 24px;

    .swiper-pagination-bullet {
      margin-left: 12px;
    }
  }

  .cve-wrapper {
    padding-bottom: 64px;

    .pcSwiper {
      display: none;
    }

    >h1 {
      margin: 28px auto 0;
    }

    .moblieSwiper {
      display: block;
      padding: 0 20px;

      .mobileItem {
        background: linear-gradient(180deg, #FFFFFF 0%, #FFFFFF 80.73%, rgba(255, 255, 255, 0.85) 100%);
        backdrop-filter: blur(22.5px);
        border-radius: 4px;
        margin-top: 20px;

        .tag-list {
          display: flex;
          padding: 12px 16px;
          padding-bottom: 0;

          .stag {
            font-style: normal;
            font-weight: 500;
            font-size: 14px;
            color: #666666;
            background: #F0F6FE;
            border-radius: 2px;
            padding: 5px 8px;
            margin-right: 8px;
          }
        }

        .ref-list {
          display: flex;
          flex-wrap: wrap;
          padding: 10px 16px;
          padding-top: 0;

          .stag {
            border-left: 1px #eee solid;
            padding: 0 20px;
            margin-top: 16px;
            width: 120px;
            text-align: center;
            color: #666666;
            font-style: normal;
            font-weight: 400;
            font-size: 14px;
          }

          .stag:nth-of-type(2n+1) {
            padding-left: 0;
            border: unset;
            text-align: left;
          }
        }
      }
    }
  }
}
</style>


yhBug.ts 文件

interface OpensourceDatabaseBug {
  name: string
  reference: string[]
}

type OpensourceDatabaseBugs = Array<OpensourceDatabaseBug>

const bugs: OpensourceDatabaseBugs = [
{
  name: 'MySQL 8.0.27',
  reference: [
    'CVE-2022-21509',
    'CVE-2022-21526',
    'CVE-2022-21527',
    'CVE-2022-21528',
    'CVE-2022-21529',
    'CVE-2022-21530',
    'CVE-2022-21531',
    'CVE-2022-21438',
    'CVE-2022-21459',
    'BUG 106045',
    'BUG 106047',
    'BUG 106048',
    'BUG 106050',
    'BUG 106051',
    'BUG 106058',
    'BUG 106061',
    'BUG 106055'
  ]
},
{
  name: 'MySQL 8.0.29',
  reference: [
    'BUG 108241',
    'BUG 108242',
    'BUG 108243',
    'BUG 108244',
    'BUG 108246',
    'BUG 108247',
    'BUG 108248',
    'BUG 108249',
    'BUG 108251',
    'BUG 108252',
    'BUG 108253',
    'BUG 108254',
    'BUG 108255'
  ]
},
{
  name: 'MariaDB 10.3.35',
  reference: [
    'N1DEV-28501',
    'MDEV-28502',
    'MDEV-28503',
    'MDEV-28504',
    'MDEV-28505',
    'MDEV-28506',
    'MDEV-28507',
    'MDEV-28508',
    'MDEV-28509',
    'MDEV-28510',
    'MDEV-28614',
    'MDEV-28615',
    'MDEV-28616',
    'N1DEV-28617',
    'MDEV-28618',
    'MDEV-28619',
    'MDEV-28620',
    'N1DEV-28621',
    'MDEV-28622',
    'MDEV-28623',
    'MDEV-28624'
  ]
},
{
  name: 'MariaDB 10.10.0',
  reference: [
    'MDEV-29358',
    'MDEV-29359',
    'MDEV-29360',
    'N1DEV-29361',
    'MDEV-29362',
    'MDEV-29363'
  ]
},
{
  name: 'OceanBase 3.1.4',
  reference: [
    'issues 986',
    'issues 987',
    'issues 988',
    'issues 989',
    'issues 995',
    'issues 1000'
  ]
}
]

export default bugs

demo 我放在github上了,如需要请自取:swiper-vue

Vue.js 结合 Swiper 是一种常见的组合,用于创建动态且响应式的轮播图组件。Swiper 是一款流行的前端轮播库,它提供了丰富的选项和易用的 API,而 Vue 可以帮助我们管理数据绑定、状态管理和组件复用。 以下是使用 VueSwiper 实现叠层轮播图的基本步骤: 1. **安装 Swiper**: 首先,你需要在项目中引入 Swiper 的库。可以使用 npm 或 yarn 进行安装: ```bash npm install swiper@latest swiper-auto-init --save # 或者 yarn add swiper@latest swiper-auto-init ``` 2. **导入 Swiper 组件**: 在你的 Vue 文件中,导入 Swiper 的 CSS 和 JavaScript,并使用 `v-bind` 将其属性绑定到组件上: ```html <link rel="stylesheet" href="path/to/swiper.min.css"> <script src="path/to/swiper-bundle.min.js"></script> ``` ```js import { Swiper, SwiperSlide } from 'swiper'; ``` 3. **创建模板和数据**: 创建一个包含 Swiper 组件的 Vue 模板,并定义数据数组,存放轮播图片及其内容: ```html <div id="mySwiper"> <swiper :options="swiperOptions"> <swiper-slide v-for="(item, index) in images" :key="index"> <img :src="item.image" alt="Slide {{ index + 1 }}"> <!-- 添加额外内容如标题等 --> <div class="slide-content">{{ item.title }}</div> </swiper-slide> </swiper> </div> ``` ```js data() { return { images: [ // 图片数组,例如 [{'image': 'img1.jpg', 'title': '图1'}, ...] ], swiperOptions: { // Swiper 的配置选项,如 autoplay、pagination 等 }, }; }, ``` 4. **初始化 Swiper**: 在 Vue 的 mounted 生命周期钩子函数里初始化 Swiper 组件,传入数据配置: ```js mounted() { this.$refs.mySwiper.swiper.init(); } ``` 5. **调整样式**:根据需要调整 Swiper 的样式和布局,比如添加 pagination 控制点、改变切换效果等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值