vue2移动端+swiper 实现异形的slide

目录

一、在Vue2中安装及使用Swiper

二、实现代码

三、实现效果

四、参考


一、在Vue2中安装及使用Swiper

1. 安装swiper

npm install swiper@4.5.1 --save-dev

2. 在main.js中引入

import 'swiper/dist/css/swiper.min.css';
import 'swiper/dist/js/swiper.min';

二、实现代码

以下分别是html、数据+swiper设置、样式的代码:

<template>
    <!-- 轮播图 -->
    <div class="swiper" id="swiper1">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="(item, index) in bookSwiper" :key="index">
          <img :src="item.img" class="bookImg" />
        </div>
      </div>
    </div>
</template>
<script>
import Swiper from 'swiper';

export default {
  name: "HomeView",
  data() {
    return {
      bookSwiper: [
        {
          img: "http://img.test.o2ting.com/ProductImages/2018/-0/20180206155502_726R.jpg_355x473.jpg",
          cntname: "错嫁良缘之一代军师",
        },
        {
          img: "http://iread.wo.com.cn/res/images/cover/lhsz/1491552645683/stream/smlf/image/cover.jpg",
          cntname: "鬼墓迷灯",
        },
        {
          img: "http://pic.qingting.fm/2017/0228/20170228173809513.jpg!medium",
          cntname: "九霄灵帝",
        },
      ],
    };
  },
  mounted() {
    new Swiper("#swiper1", {
      direction: "horizontal",
      speed: 600,
      loop: true,
      autoplay: false,
      observer: true,
      observeParents: true,
      effect: "coverflow",
      slidesPerView: "auto",
      centeredSlides: true,
      coverflowEffect: {
        rotate: 0, //slide做3d旋转时Y轴的旋转角度。默认50。
        stretch: -5, //每个slide之间的拉伸值,越大slide靠得越紧。 默认0。
        depth: 20, //slide的位置深度。值越大z轴距离越远,看起来越小。 默认100。
        modifier: 5,
        slideShadows: false, //开启slide阴影。默认 true。
      },
    });
  },
};
</script>
  // 轮播图
  .swiper {
    width: 100%;
    margin-bottom: 20px;

    .swiper-slide {
      width: 335px !important;
      height: 120px;
      font-size: 14px;
      text-align: center;
      line-height: 80px;
      border-radius: 8px;
      position: relative;
    }

    .bookImg {
      width: 335px !important;
      height: 120px;
      border-radius: 8px;
    }
  }

三、实现效果

1. 效果图如下:

2. 效果类似:Swiper演示中的“实现异形的slide”。

四、参考

vue使用异形swiper_书信成鸽1208的博客-CSDN博客_异形swiper

https://www.jianshu.com/p/ebfec766a99e

Swiper演示 - Swiper中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue3中使用Swiper异形slide,需要先安装Swiper: ``` npm install swiper ``` 然后,在Vue组件中引入Swiper: ``` import Swiper from 'swiper'; import 'swiper/css/swiper.css'; ``` 接着,在组件的mounted钩子函数中初始化Swiper: ``` mounted() { var mySwiper = new Swiper('.swiper-container', { slidesPerView: 'auto', centeredSlides: true, spaceBetween: 30, grabCursor: true, breakpoints: { 768: { slidesPerView: 2, spaceBetween: 10, }, 1024: { slidesPerView: 3, spaceBetween: 20, }, }, on: { init: function () { this.slides.each(function (index, el) { var $el = $(el); $el.css('background-image', $el.find('img').attr('src')); $el.find('img').hide(); }); }, }, }); }, ``` 注意,上面的代码中使用了jQuery的方法`css()`和`hide()`,需要先引入jQuery库。 最后,在组件的template中添加Swiper的HTML结构: ``` <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"> <img src="https://picsum.photos/200/300" alt="Slide 1" /> <div class="swiper-slide-content"> <h2>Slide 1</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <a href="#" class="btn">Learn More</a> </div> </div> <div class="swiper-slide"> <img src="https://picsum.photos/200/300" alt="Slide 2" /> <div class="swiper-slide-content"> <h2>Slide 2</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <a href="#" class="btn">Learn More</a> </div> </div> <div class="swiper-slide"> <img src="https://picsum.photos/200/300" alt="Slide 3" /> <div class="swiper-slide-content"> <h2>Slide 3</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <a href="#" class="btn">Learn More</a> </div> </div> </div> </div> ``` 上述代码中,每个slide都包含一个img标签和一个swiper-slide-content元素,可以自由组合。 最终效果如下: ![swiper-example](https://i.loli.net/2021/06/04/SPjyTpmh1i3aZxo.gif)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值