swiper 一组显示5张且点击左右按钮移动一张图片,选中的当前项有放大效果

来源:csdn博客 分类: 文章浏览史 发布时间:2020-09-24 16:10:24 最后更新:2020-09-24 浏览:76
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-09-24 16:10:24

1.dom元素

<div id="box">
	<div class="swiper-container">
	  <div class="swiper-wrapper">
			<div class="swiper-slide">
				<img src="./banner1.png" alt="" />
			</div>
			 <div class="swiper-slide">
				<img src="./banner2.png" alt="" />
			</div>
			 <div class="swiper-slide">
				<img src="./banner3.png" alt="" />
			</div>
			 <div class="swiper-slide">
				<img src="./banner4.png" alt="" />
			</div>
			 <div class="swiper-slide">
				<img src="./banner5.png" alt="" />
			</div>
	  
		</div>
		<!--如果你需要分页-->
		<div class="swiper-pagination"></div>
		<!--如果你需要前进后退按钮-->
		<div class="swiper-button-next"></div>
		<div class="swiper-button-prev"></div>
</div>
</div>

2.css样式

// 引入swiper.min.css
<link rel="stylesheet" href="./swiper/dist/css/swiper.min.css" />

#box{
  width: 1324px;  
  margin: 20px auto;    
}
.swiper-container {
  width: 100%;
// 下面的padding 一定要加 (因为容器多余的部分被隐藏啦)
  padding-top: 50px;
  padding-bottom: 50px;
}
.swiper-slide {
  width: 261px;
  height: 162px;
  background-position: center;
  background-size: cover;
  transition: all 1s;

  /* 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%;
}
// 激活的样式(选中的当前项)	
.swiper-slide.swiper-slide-active{	
  transform: scale(1.2);
  z-index: 2;

}

3.js代码

// 引入swiper.min.js
<script type="text/javascript" src="./swiper/dist/js/swiper.min.js"></script>


<script>
	var swiper = new Swiper('.swiper-container', {
	  loop:true,	

       //根据slider的宽度自动分布
	  slidesPerView: 'auto',

	  // active下 居中显示
	  centeredSlides: true,	

	  // 分页
	  pagination: {
	    el: '.swiper-pagination',
	    clickable: true,
	  },

	//    前进后退按钮
	  navigation: {
	    nextEl: '.swiper-button-next',
	    prevEl: '.swiper-button-prev',
	  }
	});
  </script>

 

php技术微信