Swiper 轮播插件

        项目中难免遇到要轮播要滑动的,自己写一个又太慢,复用性也是问题,我就教你 5 分钟 搞定swiper,妈妈再也不用担心我的轮播图了,下文代码直接复制即可运行,注释里面写的清清楚楚(最常用到的);

若是在vue中用的话

npm install swiper --save-dev

在要用到swiper的组件中 import 一下,将js初始化部分写入 vue mounted 钩子函数里即可

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/css/swiper.css">
    <style>
        *{
        	margin: 0;
        	padding: 0;
        }
    	.swiper-container {
    		width: 600px;
    		height: 300px;
		} 
		.swiper-slide img{
			width: 100%;
			height: 100%;
		}
    </style>
</head>
<body>
    <div class="swiper-container">
    	<div class="swiper-wrapper">
        	<div class="swiper-slide">
        		<a href="www.baidu.com">
        			<img src="https://cdn-cnc.test.17zuoye.net/s17/xue/student/public/images/wechat/newDetail/appointment_success.png"alt="">
        		</a>
        	</div>
        	<div class="swiper-slide">
        		<a href="www.baidu.com">
        			<img src="http://static.17xueba.com/test/server/image/2018/08/20180827110106378711.png" alt="">
        		</a>
        	</div>
        	<div class="swiper-slide">
        		<a href="www.baidu.com">
        			<img src="http://oimageb1.youdao.com/image?id=5410126604320844202&product=picugc" alt="">
        		</a>
        	</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>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/js/swiper.js"></script>
    <script>
   		var mySwiper = new Swiper ('.swiper-container', {
   			// 如果需要分页器
    		pagination: {
      			el: '.swiper-pagination',
      			dynamicBullets: true, //动态分页器
      			clickable: true, //分页器控制滑动
    		},
    
    		// 如果需要前进后退按钮
    		/*navigation: {
      			nextEl: '.swiper-button-next',
      			prevEl: '.swiper-button-prev',
    		},*/
    
    		// 如果需要滚动条
    		/*scrollbar: {
      			el: '.swiper-scrollbar',
    		},*/


        	effect : 'slide',//位移切换 fade-淡入 cube-立体方块 coverflow-3D流 flip-3D翻转
   			// autoplay: true,//可选选项,自动滑动
   			autoplay : {
     			delay:1000 // 滑动块 slide 停留时间
  			},
  			speed : 1000, // 自动滑动速度
  			//grabCursor : true, // 鼠标到 slide 上变手掌
    		direction: 'horizontal', // 水平   vertical //垂直
   			loop: true, // 循环模式选项
   			initialSlide :0, //默认初始化显示slide块
   			//autoHeight : true, //高度随 slide 变化
   			//runCallbacksOnInit : false,//初始化时不触发slideChange
   			on:{
    			slideChangeTransitionEnd:function(){
      				//console.log('滑动完成触发回调' + this.realIndex);
    			},
    			sliderMove: function(){
    				//console.log('人为滑动');	
    			},
 			},
 			//init: false, //false 禁止初始化,在需要的地方使用 mySwiper.init() 来初始化
  		}) 
    </script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值