Swiper-强大的移动手机端幻灯片插件

Swiper是一款支持硬件加速过渡动画的移动手机幻灯片插件。该幻灯片可以很好的在iOS,Android,Windows Phone 8和桌面浏览器中工作。Swiper提供了大量参数和api,功能非常强大。

在这里插入图片描述在线演示 下载

安装
可以通过bower来安装该幻灯片插件。

$ bower install swiper    

使用方法
使用该幻灯片插件需要引入swiper.css和swiper.js,你可以通过cdn加速来引入这些文件:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.x.x/css/swiper.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.x.x/css/swiper.min.css">
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.x.x/js/swiper.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.x.x/js/swiper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.x.x/js/swiper.jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.x.x/js/swiper.jquery.min.js"></script>   

HTML结构
该幻灯片的基本HTML结构如下:

<!-- 幻灯片的容器 -->
<div class="swiper-container">
    <!-- Additional required wrapper -->
    <div class="swiper-wrapper">
        <!-- Slides -->
        <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>

CSS样式
你需要为幻灯片的容器设置一个宽度和高度:

.swiper-container {
    width: 600px;
    height: 300px;
}

初始化插件
可以通过下面的方法来初始化该幻灯片插件。

var mySwiper = new Swiper ('.swiper-container', {
    // Optional parameters
    direction: 'vertical',
    loop: true,
     
    // If we need pagination
    pagination: '.swiper-pagination',
     
    // Navigation arrows
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev',
     
    // And if we need scrollbar
    scrollbar: '.swiper-scrollbar',
  })                    

如果你使用jQuery或Zepto,可以使用下面的方法来初始化插件。

$(document).ready(function () {
  //initialize swiper when document ready  
  var mySwiper = new Swiper ('.swiper-container', {
    // Optional parameters
    direction: 'vertical',
    loop: true
  })        
});  

配置参数
Swiper幻灯片的可用配置参数有:

参数类型默认值描述
initialSlidenumber0初始化的slide的序号
directionstring'horizontal'幻灯片的方向,可以是'horizontal' 或 'vertical'
speednumber300幻灯片的过渡动画速度
setWrapperSizebooleanfalse设置为true时,幻灯片会将容器的宽度和高度设置为slide的宽度和高度
virtualTranslatebooleanfalseEnabled this option and swiper will be operated as usual except it will not move, real translate values on wrapper will not be set. Useful when you may need to create custom slide transition
widthnumber幻灯片的宽度
heightnumber幻灯片的高度
autoHeightbooleanfalse设置为true时幻灯片自动适应每一个slide的高度
roundLengthsbooleanfalse设置为true可以防止模糊文字的出现
nestedbooleanfalse嵌套幻灯片,水平和垂直幻灯片相互嵌套
autoplaynumber自动播放的延迟时间,如果为设置将不自动播放
autoplayStopOnLastbooleanfalse是否到达最后一个slide时停止自动播放
autoplayDisableOnInteractionbooleantrue设置为false时,用户滑动幻灯片后不会停止自动播放
watchSlidesProgressbooleanfalse是否计算每个slide的进度
watchSlidesVisibilitybooleanfalse启用该选项时在视口中的slide将被添加额外的class
freeModebooleanfalse如果设置为true,幻灯片将不会有固定的位置
freeModeMomentumbooleantrue如果设置为true,幻灯片在滑动后会有一些惯性
freeModeMomentumRationumber1值越大惯性越大
freeModeMomentumBouncebooleantrue如果不想要惯性效果设置为false
freeModeMomentumBounceRationumber1值越大弹性效果越强
freeModeMinimumVelocitynumber0.02free mode的最小手指触摸速度
effectstring'slide'可以是"slide", "fade", "cube", "coverflow" 或 "flip"
fadeobjectfade: {
crossFade: false
}
fade效果参数
cubeobjectcube: {
slideShadows: true,
shadow: true,
shadowOffset: 20,
shadowScale: 0.94
}
立方体效果参数
coverflowobjectcoverflow: {
rotate: 50,
stretch: 0,
depth: 100,
modifier: 1,
slideShadows : true
}
coverflow效果参数
flipobjectflip: {
slideShadows : true
limitRotation: true
}
flip效果参数
parallaxbooleanfalse设置为true时可以制作视觉差效果
spaceBetweennumber0幻灯片slide之间的距离
slidesPerViewnumber 或 'auto'1幻灯片每一个视图的数量
slidesPerColumnnumber1幻灯片每一列的数量
slidesPerColumnFillstring'column'幻灯片如何填充行和列,可以是column' 或 'row'
slidesPerGroupnumber1幻灯片分组
centeredSlidesbooleanfalse设置为true时,当前激活的幻灯片居中显示
slidesOffsetBeforenumber0在容器中第一个幻灯片slide之前添加一些空白的像素,单位像素
slidesOffsetAfternumber0在容器中最后一个幻灯片slide之后添加一些空白的像素,单位像素
grabCursorbooleanfalse在桌面设备中设置为true,鼠标光标变为grab形状
touchEventsTargetstring'container'touch事件监听的目标元素
touchRationumber1Touch ration
touchAnglenumber45移触摸的触发角度
simulateTouchbooleantrue是否接收鼠标事件
shortSwipesbooleantrue设置为false禁止short swipes
longSwipesbooleantrue设置为false禁止long swipes
longSwipesRationumber0.5在long swipes中触发next/previous滑动的比例
longSwipesMsnumber300在long swipes中触发next/previous滑动最小持续时间(毫秒)
followFingerbooleantrue如设置为false,幻灯片只有在手指释放时才动画
onlyExternalbooleanfalse如果设置为true,那么只有通过API来切换幻灯片
thresholdnumber0阈值,单位像素
touchMoveStopPropagationbooleantrue设置为true时,分页的“touchmove”被禁止
iOSEdgeSwipeDetectionbooleanfalseEnable to release Swiper events for swipe-to-go-back work in iOS UIWebView
iOSEdgeSwipeThresholdnumber20Area (in px) from left edge of the screen to release touch events for swipe-to-go-back in iOS UIWebView
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值