前言
本文主要介绍了小米商城首页面的部分实现过程以及 swiper插件的简易使用方法
一、swiper使用方法
swiper官网——https://swiper.com.cn
1.首先加载插件,需要用到的文件有swiper-bundle.min.js和swiper-bundle.min.css文件,不同
<!DOCTYPE html>
<html>
<head>
...
<link rel="stylesheet" href="dist/css/swiper-bundle.min.css">
</head>
<body>
...
<script src="dist/js/swiper-bundle.min.js"></script>
...
</body>
</html>
2.添加HTML内容。Swiper7的默认容器是'.swiper',Swiper6之前是'.swiper-container'。
<div class="swiper">
<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>
导航等组件可以放在Swiper容器之外
3.你可能想要给Swiper定义一个大小,当然不要也行。
.swiper {
width: 600px;
height: 300px;
}
4.初始化Swiper。
<script>
var mySwiper = new Swiper ('.swiper', {
direction: 'vertical', // 垂直切换选项
loop: true, // 循环模式选项
// 如果需要分页器
pagination: {
el: '.swiper-pagination',
},
// 如果需要前进后退按钮
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// 如果需要滚动条
scrollbar: {
el: '.swiper-scrollbar',
},
})
</script>
二、小米商城轮播图实现过程
1.HTML结构
<!-- 轮播图区域 -->
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide"><a class="slide1" href="#"><img src="./image/02248aa55d5bef10c9013297074d7795.jpg" alt=""></a></div>
<div class="swiper-slide"><a class="slide2" href="#"><img src="./image/06c044611f215ddef570d3a6a9a60725.webp" alt=""></a></div>
<div class="swiper-slide"><a class="slide3" href="#"><img src="./image/4a7872075a72ffc83108c2efcbfc92e5.webp" alt=""></a></div>
</div>
<!-- 如果需要分页器 -->
<div class="swiper-pagination"></div>
<!-- 如果需要导航按钮 -->
<div class="swiper-button-prev prev swiper-button-white" ></div>
<div class="swiper-button-next next swiper-button-white"></div>
</div>
<script>
var mySwiper = new Swiper ('.swiper', {
// direction: 'vertical', 垂直切换选项
loop: true, // 循环模式选项
autoplay:true,
// 如果需要分页器
pagination: {
el: '.swiper-pagination',
clickable :true,
},
// 如果需要前进后退按钮
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// 如果需要滚动条
scrollbar: {
el: '.swiper-scrollbar',
},
})
</script>
2.CSS样式
/* 轮播图区域样式设置 */
.swiper{
width: 1226px;
height: 460px;
top:-460px;
/* 隐藏其他图片 */
overflow: hidden;
position: relative;
}
.swiper img{
width: 1226px;
height: 460px;
}
/* 设置前进/后退按钮样式 */
.prev{
position: absolute;
left: 254px;
}
/* 修改分页器的样式 */
.swiper-pagination-bullet-active {
background-color: #FFFFFF66;
}
.swiper-pagination-bullet {
background-color: #00000066;
border: 2px solid #8f9090;
}