swiper(swiper master)是目前应用较广泛的移动端网页触摸内容滑动js插件,可以用来做轮播和滑动.
初始化
<!doctype html><html>
<head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" type="text/css" href="swiper.css?1.1.11"/> <style> .swiper-container { width: 600px; height: 300px; }
.swiper-slide{ font-size: 50px } .swiper-slide:nth-of-type(1){ background-color: cornflowerblue; } .swiper-slide:nth-of-type(2){ background-color: coral; } .swiper-slide:nth-of-type(3){ background-color: yellowgreen; } </style>
</head>
<body> <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之外--> <script src="swiper.js?1.1.11"></script> <script>
var myswiper = new swiper ('.swiper-container', {
direction: 'vertical',// loop: true,// // // 如果需要分页器 pagination: '.swiper-pagination',// // // 如果需要前进后退按钮 nextbutton: '.swiper-button-next',
prevbutton: '.swiper-button-prev',// // // 如果需要滚动条 scrollbar: '.swiper-scrollbar',
})</script>
</body></html>
基本配置
var myswiper = new swiper ('.swiper-container', {
// 滑动方向
// horizontal水平
// vertical垂直
direction: 'horizontal',
// 初始化时候slide的索引(从0开始)
initialslide: 1,
// 手指松开至slide贴合的时间
speed:3000,
// 设置自动播放的事件间隔
autoplay: 2000,
// 可显示数量
slidesperview:2,
// 滑块居中
centeredslides:true,
})
触摸设置
var myswiper = new swiper ('.swiper-container', {
direction: 'horizontal',
// 触摸距离与slide滑动距离的比率
touchratio:0.1,
// 无法滑动
onlyexternal:true,
// 滑块跟随手指进行移动
followfinger:false,
// 定义longswipesms
longswipesms:1000,
longswipes:false,
shortswipes:false,
longswipesratio:0.5,
touchangle:10,
})
禁止切换和前进后退 <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide stop">slide 1</div> <!--<div class="swiper-slide swiper-no-swiping">slide 2</div>--> <div class="swiper-slide">slide 2</div> <div class="swiper-slide">slide 3</div> </div> </div> <button class="prev">prev</button> <button class="next">next</button> <script src="swiper.js?1.1.11"></script> <script>
var myswiper = new swiper ('.swiper-container', {
direction: 'horizontal',
noswiping:true,
noswipingclass : stop,
nextbutton : .next,
prevbutton : .prev,
}) </script>分页器 <style> .swiper-container { width: 600px; height: 300px; }
.swiper-slide{ font-size: 50px } .swiper-slide:nth-of-type(1){ background-color: cornflowerblue; } .swiper-slide:nth-of-type(2){ background-color: coral; } .swiper-slide:nth-of-type(3){ background-color: yellowgreen; } .swiper-pagination-bullet{ width: 20px; height: 20px; } .swiper-pagination-bullet-active{ background-color: yellow; } </style>
</head>
<body> <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> <script src="swiper.js?1.1.11"></script> <script>
var myswiper = new swiper ('.swiper-container', {
direction: 'horizontal',
pagination : .swiper-pagination,
paginationtype : bullets,
paginationtype : fraction,
paginationtype : progress,
paginationclickable : true,
paginationhide : true,
paginationelement : i,
paginationbulletrender : function( swiper,index,classname ){ return <span class='"+ classname +"'>+ (index+1) +</span> }
})</script>
</body>切换效果 <script>
var myswiper = new swiper ('.swiper-container', {
direction: 'horizontal',
effect : slide,
effect : fade,
effect : cube,
effect : coverflow,
effect : flip,
})</script>进程<body> <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> <button id="btn">按钮</button> <script src="swiper.js?1.1.11"></script> <script>
var myswiper = new swiper ('.swiper-container', {
direction: 'horizontal',
})
btn.onclick = function(){
alert( myswiper.progress );
alert( myswiper.slides[0].progress );
console.log( myswiper.slides[0].progress,myswiper.slides[1].progress,myswiper.slides[2].progress );
}
setinterval(function(){
console.log( myswiper.slides[0].progress,myswiper.slides[1].progress,myswiper.slides[2].progress );
},20)</script>
</body>
常用属性和回调
<body> <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> <button id="btn">按钮</button> <script src="swiper.js?1.1.11"></script> <script>
var myswiper = new swiper ('.swiper-container', {
direction: 'horizontal',
speed : 2000,
onslidechangestart : function(){
console.log( 开始滑动 );
},
onslidechangeend : function(){
console.log( 滑动结束 );
}
})
console.log( myswiper.width );
console.log( myswiper.height );
btn.onclick = function(){
console.log( myswiper.translate );
console.log( myswiper.activeindex );
console.log( myswiper.previousindex );
} </script>
</body>
以上就是一个简单实用的js插件--swiper的详细内容。