swiper框架的使用教程

本文介绍了移动端滑动插件Swiper的使用,适用于手机和平板等移动设备。初学者可以通过下载swiper-3.4.2版本,参考demos实现所需效果。以demos中的第21个效果为例,步骤包括引用JS和CSS文件,添加HTML结构,并在head中加入相应CSS样式。在实际应用中,可在API文档中查找更多参数设置。

swiper的使用教程

今天要分享的是swiper这个插件的使用,什么是swiper呢?

Swiper常用于移动端网站的内容触摸滑动;
Swiper是纯javascript打造的滑动特效插件,面向手机、平板电脑等移动终端。


初学者使用swiper的时候,可以在官网(http://www.swiper.com.cn/)学,但是一开始的时候,建议不要直接看它的API文档。可以在先下载swiper,选择swiper-3.4.2完整压缩包下载,然后可以看它里面demos中的效果,你需要哪个效果,就用哪个!


使用方法(以其中demos中第21个效果为例):

1、引用JS和css文件

<link rel="stylesheet" href="path/to/swiper.min.css">
<script src="path/to/swiper.min.js"></script>

2、在body中加入以下格式的HTML格式

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
    
    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
    
    <!-- 如果需要滚动条 -->
    <div class="swiper-scrollbar"></div>
</div>
导航等组件可以放在container之外

3、在script中加入以下js代码

   <script>
    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
    </script>


4、在head中加入css样式

<style>
    html, body {
        position: relative;
        height: 100%;
    }
    body {
        background: #eee;
        font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
        font-size: 14px;
        color:#000;
        margin: 0;
        padding: 0;
    }
    .swiper-container {
        width: 100%;
        height: 100%;

    }
    .swiper-slide {
        text-align: center;
        font-size: 18px;
        background: #fff;

        /* Center slide text vertically */
        display: -webkit-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;
        -webkit-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;
    }
    </style>

至此就完成了一个简单的轮播,然后还有先script中要加的参数,我们可以在API文档中进行查阅,并添加。

注意的是:HTML中class的名字不能换,不要问为什么,就是那样用就OK了!




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值