移动端触摸滑动插件Swiper使用指南
Swiper是一款开源、免费、功能十分强大的移动端内容触摸滑动插件,目前的最新版本是Swiper4。Swiper主要面向的是手机、平板电脑等移动设备,帮助开发者轻松实现触屏焦点图、触屏Tab切换、触屏多图切换等常用效果。本文简单介绍一下Swiper的使用方法。

下载和安装Swiper
首先我们需要下载Swiper的相关文件:
我们可以直接从Github代码仓库中下载。
或者通过Bower下载:
$ bower install swiper
或者使用Atmosphere将Swiper制作成Meteor包:
$ meteor add nolimits4web:swiper
或者使用NMP(JavaScript包管理工具)下载:
$ npm install swiper
下载压缩包后解压,我们需要用到的js文件和css文件都在dist目录中。
从CDN引用Swiper
如果你不想将Swiper文件资源放到自己的项目中或者服务器上,那么可以使用CDN上的资源,这样可以让不同地区的用户有最快的加载速度,也可以减轻你服务器的负担,下面是可用的CDN文件列表:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.x.x/css/swiper.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.x.x/css/swiper.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.x.x/js/swiper.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.x.x/js/swiper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.x.x/js/swiper.esm.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.x.x/js/swiper.esm.bundle.js"></script>
不要忘记将版本号4.x.x替换成自己使用的版本号
将Swiper文件包含到网站中
接下来我们将Swiper的JS文件和CSS文件分别包含到自己的网站或者App中,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
...
<link rel="stylesheet" href="path/to/swiper.min.css">
</head>
<body>
...
<script src="path/to/swiper.min.js"></script>
</body>
</html>
为滑块添加HTML布局结构
下面我们创建最基本的布局:
<!-- Slider main container -->
<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>
<!-- If we need pagination -->
<div class="swiper-pagination"></div> <!-- If we need navigation buttons -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div> <!-- If we need scrollbar -->
<div class="swiper-scrollbar"></div>
</div>
设置滑块大小
我们可以为Swiper滑块自定义大小,通过CSS实现:
.swiper-container {
width: 600px;
height: 300px;
}
初始化
最后我们需要调用Swiper库初始化滑块,设置非常方便。
将下面的代码添加到<body>末尾:
<body>
...
<script>
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
direction: 'vertical',
loop: true, // If we need pagination
pagination: {
el: '.swiper-pagination',
}, // Navigation arrows
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
}, // And if we need scrollbar
scrollbar: {
el: '.swiper-scrollbar',
},
})
</script>
</body>
如果你使用jQuery库,那么上面这段初始化的代码可以放在页面任何位置,但必须在document.ready代码块中调用。示例代码如下:
$(document).ready(function () {
//initialize swiper when document ready
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
direction: 'vertical',
loop: true
})
});
或者在window.onload方法中调用(不推荐):
window.onload = function () {
//initialize swiper when document ready
var mySwiper = new Swiper ('.swiper-container', {
// Optional parameters
direction: 'vertical',
loop: true
})
};
作为CommonJs模块
Swiper与CommonJs模块完全兼容,可以在类似Node.js的环境中使用:
var Swiper = require('swiper');
var mySwiper = new Swiper('.swiper-container', { /* ... */ });
作为ES模块
Swiper包附带ES模块版本,可以在支持ES的地方使用,也可以与Webpack或Rollup等捆绑一起使用:
import Swiper from 'swiper';
var mySwiper = new Swiper('.swiper-container', { /* ... */ });
Swiper的功能确实比较强大,还有很多高级的配置,大家可以在Swiper API文档(英文)中了解。下面附上API文档的章节目录:
Swiper API Docs
Swiper Components
- Navigation
- Pagination
- Scrollbar
- Autoplay
- Parallax
- Lazy Loading
- Effects
- Zoom
- Keyboard And Mousewheel Control
- Virtual Slides
- Hash Navigation
- History Navigation
- Controller
- Accessibility
移动端触摸滑动插件Swiper使用指南的更多相关文章
- 移动端触摸滑动插件Swiper
移动端触摸滑动插件Swiper 04/02/2015 一.了解Swiper 目前移动端项目一般都需要具有触屏焦点图的效果,如果你也需要实现这一功能的话,Swiper是一个不错的选择. 1.他不需要加载 ...
- 仿移动端触摸滑动插件swiper,的简单实现
/** * @author lyj * @Date 2016-02-04 * @Method 滑动方法 针对一个大容器内部的容器做滑动封装 * @param * args args.swipeDo ...
- swiper嵌套小demo(移动端触摸滑动插件)
swiper(移动端触摸滑动插件) tip:自己敲得Swiper 的小demo,可以复制粘贴看看效果哦. swiper的js包css包下链接地址 : https://github.com/Clear ...
- Swipe-移动端触摸滑动插件swipe.js
原文链接:http://caibaojian.com/swipe.html 插件特色 viaswipe.JS是一个比较有名的触摸滑动插件,它能够处理内容滑动,支持自定义选项,你可以让它自动滚动,控制滚 ...
- Swiper --移动端触摸滑动插件
Swiper使用方法 1.首先加载插件,需要用到的文件有swiper.min.js和swiper.min.css文件. <!DOCTYPE html> <html> <h ...
- swiper.js 移动端触摸滑动插件
API链接: http://www.swiper.com.cn/api/start/2014/1218/140.html <body> <div class="swiper ...
- JS封装移动端触摸滑动插件应用于导航banner【精装版】
自己封装了一个小插件,一个小尝试. 可用于类似于京东导航等效果,效果多样化,很方便. 欢迎大家提点意见. mrChenSwiper( {parent, child, type, parentN, c ...
- 触摸滑动插件 Swiper
Swiper Swiper 是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端. Swiper中文网里已有详细的使用介绍,我就不多做介绍了. http://www.swiper ...
- 最好的移动触摸滑动插件:Swiper
最近在使用的一个移动触摸滑动插件Swiper,功能很强大,跟之前的那个swipe.JS相比,同时支持在PC端滑动,支持上下方向滑动,支持多张图片滑动,支持多屏滑动,凡是你想得到的几乎都可以实现.最近作 ...
随机推荐
- ethcode
pragma solidity ^0.4.0;contract Ballot { struct Voter { uint weight; bool voted; uint8 vote; address ...
- FileWriter与BufferedWriter的适用场景
IO这块,各种Writer,Reader,让人眼晕 而在网上基本找不到在什么时候用哪个类,并且网上的IO demo 很多用法都是错的 在这简单的分析一下FileWriter与BufferedWrite ...
- 打通版微社区(6):部署微信插件及开通QQ云服务
写在前面: 此文是我最后写的.其实实际部署的时候,我是先安装了论坛并试图开通微信的微社区.发现微社区需要在微信公众平台的开发者中心里配置 "网页账号,网页授权获取用户基本信息"为论 ...
- Effective C++(1-2) 编译器替换预处理器
1 C++最主要的四部分: C Object-Oriented C++: 面向对象 Template C++:泛型编程 STL C++高效编程守则视状况而变化,取决于你使用C++的哪一部分. 2 尽量 ...
- Java学习---MD5加密算法
前言 在我们日常的程序开发中,或多或少会遇到一些加密/解密的场景,比如在一些接口调用的过程中,我们(Client)不仅仅需要传递给接口服务(Server)必要的业务参数,还得提供Signature(数 ...
- Tsql 获取服务器信息
Tsql 获取服务器属性,如服务器版本.服务器名 ref:http://technet.microsoft.com/zh-cn/library/ms174396.aspx select serverp ...
- python 使用set对列表去重后,保持原来列表的顺序排列
testlist = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] set2list = list(set(testlist)) print ...
- Python中readline()函数 去除换行符
从Python中readline()函数读取的一行内容中含有换行符\n,很多时候我们需要处理不含有换行符的字符串,此时就要去掉换行符\n. 方法是使用strip()函数. 例子如下: f = open ...
- js布局库
1.viz.js The solution was that someone cross compiled Graphviz to Javascript using llvm + emscripten ...
- 动画的分类:属性(几何)动画、内容(视频)动画:gpu vs cpu
属性动画通过gpu根据属性来呈现: 内容动画通过cpu解码内容按照时间呈现给gpu: (或者gpu直接解码现实?)