用Swiper实现轮播图

任务描述

本关任务:用Swiper实现一个轮播图。

相关知识

为了完成本关任务,你需要掌握:

  1. Swiper基本的HTML结构,
  2. 基本功能的实现。

搭建Swiper环境

1.下载插件

进入Swiper官网 http://www.swiper.com.cn/download/index.html,只需要下载swiper-4.2.2.min.js和swiper-4.2.2.min.css就可以了。

2.引入插件

<!DOCTYPE html>
<html>
<head>
    ...
    <link rel="stylesheet" href="path/to/swiper.min.css">
</head>
<body>
    ...
    <script src="path/to/swiper.min.js"></script>
</body>
</html>

3.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>

注:

这里的HTML结构是固定的,不可以随便更改;

这里的类是固定的,外面最大的容器类为swiper-container,放图片的容器类为swiper-wrapper,放图片的类为swiper-slide

4.你可能想要给Swiper定义一个大小,当然不要也行。

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

5.初始化Swiper:最好是挨着</body>标签

<script>        
  var mySwiper = new Swiper ('.swiper-container', {
    direction: 'vertical',
    loop: true,
    autoplay: 3000,
    speed: 1000,
    // 如果需要分页器
    pagination: {
      el: '.swiper-pagination',
    },
    
    // 如果需要前进后退按钮
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
    
    // 如果需要滚动条
    scrollbar: {
      el: '.swiper-scrollbar',
    },
  })        
  </script>
</body>

这里简单的介绍一下基本的几个参数:

  1. direction: 'horizontal',表示水平滑动,垂直滑动是 direction: vertical。默认是水平滑动;
  2. loop: true,表示最后一张图片会和第一张图片连起来播放,就像循环播放一样; loop: false,表示最后一张图片是终点;
  3. autoplay: 3000, 表示自动切换的时间间隔,3000表示3秒;
  4. speed: 1000, 表示滑动从开始到结束的时间,1000表示1秒。

代码文件

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>少儿教育</title>
		<link rel="stylesheet" href="step2/swiper.min.css" />
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			
			ul,
			li {
				list-style: none;
			}
			
			a {
				color: #888;
				text-decoration: none;
				font-size: 16px;
			}
			
			body {
				background: #fff;
				position: relative;
			}
			
			.swiper-container {
				width: 100%;
				height: 280px;
				margin-left: auto;
				margin-right: auto;
				overflow: hidden;
			}
			
			.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;
			}
			
			.swiper-slide img {
				width: 100%;
				height: 100%;
			}
		</style>
	</head>

	<body>
        <!--------  Begin  --------->
	    <!-- swiper最大的容器类-->
		<div class="swiper-container">
            
            <!-- 图片容器类-->
		    <div class=" swiper-wrapper">
                
        <!---------  End  ---------->

				<div class="swiper-slide">
					<img src="https://www.educoder.net/api/attachments/198516" alt="轮播图1" />
				</div>
				<div class="swiper-slide">
					<img src="https://www.educoder.net/api/attachments/198517" alt="轮播图2" />
				</div>
			</div>

			<div class="swiper-pagination"></div>

			
			<div class="swiper-button-prev swiper-button-white"></div>
			<div class="swiper-button-next swiper-button-white"></div>
		</div>
		<script src="step2/swiper.min.js"></script>
		<script type="text/javascript">
			var swiper = new Swiper('.swiper-container', {
				direction: 'horizontal',
                //------------- Begin  ------------
                
                //开启loop模式,设置图片为循环播放
               loop:true,
                
                //自动切换的时间为1.5秒
				autoplay: 1500,
                
                //--------------  End  ------------
				speed: 1000,
				pagination: '.swiper-pagination',
				nextButton: '.swiper-button-next',
				prevButton: '.swiper-button-prev',
				autoplayDisableOnInteraction: false,
			});
		</script>
	</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A cup of tea.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值