<link rel="stylesheet" href="/utils/lib/swiper-11.0.5/package/swiper-bundle.min.css">
<script src="/utils/lib/swiper-11.0.5/package/swiper-bundle.min.js"></script>
<div class="team-swiper swiper">
<div class="swiper-wrapper">
<div class="team-card swiper-slide">Slide 1</div>
<div class="team-card swiper-slide">Slide 2</div>
<div class="team-card 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>
<div class="promotion-swiper swiper">
<ul class="swiper-wrapper">
<li class="promotion-card swiper-slide">Slide 1</li>
<li class="promotion-card swiper-slide">Slide 2</li>
<li class="promotion-card swiper-slide">Slide 3</li>
...
</ul>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-scrollbar"></div>
</div>
.swiper {
width: 100%;
max-width: 800px;
}
const swiper = new Swiper('.swiper', {
// Optional parameters
direction: 'vertical',
loop: true,
clickable:true,
// If we need pagination
pagination: {
el: '.swiper-pagination',
},
// Navigation arrows
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// And if we need scrollbar
scrollbar: {
el: '.swiper-scrollbar',
},
});
const teamSwiper = new Swiper('.team-swiper', {});
const swiper = new Swiper('.swiper', {
// 各配置项
});
| 类别 | 说明 |
|---|---|
| direction | 轮播方向:horizontal | vertical |
| autoplay | 自动播放:false | true |
| breakpoints | 断点设置;仅支持部分属性,如 spaceBetween、slidesPerView等 |
| centeredSlides | 居中当前幻灯,适合多个幻灯的情况;false | true | auto* |
| effect | 过渡效果;slide | fade | cube | coverflow | flip | creative | cards |
| xxxeffect | 配合effect进一步定制过渡效果,如cubeeffect |
| grabCursor | 抓取|拖拽鼠标样式,概述桌面端体验:false | true |
| loop | 循环模式;false | true |
| mousewheel | 鼠标滚轮触发导航:false | true |
| slidesPerView | 轮播时可以查看幻灯片的个数;默认1张;可以是小数,如 2.2 |
| spaceBetween | 幻灯片间隔;单位px;不要为幻灯片指定margin |
| speed | 幻灯过渡时间;单位ms;300 |
| on | 注册事件处理 |
swiper.autoplay
| 分类 | 说明 |
|---|---|
| activeIndex | 当前活动幻灯片;循环模式下容易出现异常 |
| realIndex | 幻灯片实际索引;推荐使用 |
| autoplay | 自动播放;鼠标点击修改该属性,仍然会播放一次才停止 |
| mousewheel | 使用鼠标滚轮导航 |
swiper.init()
| 分类 | 说明 |
|---|---|
| init(el) | 初始化 |
| swiper.on(event, handler) | 注册事件 |
.同轮播图的交互;支持两种形式;更多信息,请访问 轮播图 - Events
.尽量不要使用箭头函数,避免 this 丢失
const swiper = new Swiper('.swiper', {
// ...
on: {
init: function () {
console.log('swiper initialized');
},
},
});
const swiper = new Swiper('.swiper', {
// ...
});
swiper.on('slideChange', function () {
console.log('slide changed');
});
| 分类 | 说明 |
|---|---|
| init | 初始化 |
| slideChange | 当前活动幻灯切换完毕 |
| 参数 | 说明 |
|---|---|
| type | 类型;进度条 progressbar | 子弹 bullets,默认 | 分数 fraction | 自定义 custom
每个类型都可进一步定制 |
| clickable | 单击分页跳转到对应页面,默认为 false |
| bulletClass | 和子弹类型分页器相关的默认类;swiper-pagination-bullet-custom |
| dynamicBullets | 突出/动态展示少量页码;默认是 false |
| renderBullet | (index,className) => {}
定制子弹类型分页器;返回定制的DOM结构 |
| currentClass | 和分数类型分页器相关的默认类;swiper-pagination-current |
| totalClass | 和分数类型分页器相关的默认类;swiper-pagination-total |
| renderFraction | (currentClass, totalClass) => {}
定制分数类型分页器;返回定制的DOM结构 |
| renderCustom | 定制和自定义类型custom相关的分页器;function(swiper, current, total) |
.swiper .swiper-pagination-bullet {
width: 14px;
height: 14px;
background-color: #fff;
}
.swiper .swiper-pagination-bullet-active {
background-color: #f40;
}
或
.swiper-pagination-bullet {
background-color: transparent;
width: 40px;
height: 8px;
border: 1px solid #fff;
border-radius: 4px;
opacity: 1;
}
.swiper-pagination-bullet-active {
background-color: #f40;
}
.swiper-button-prev,
.swiper-button-next {
color: var(--main-color);
}
.swiper .swiper-btn {
width: 16px;
height: 16px;
color: #fff;
transform: scale(0.6);
}
.swiper .swiper-btn:hover {
color: var(--pri-color);
}
--swiper-navigation-size: 36px; --swiper-navigation-color: #fff;
let swiper = new Swiper('.header-swiper', {
//... ...
slidesPerView: 1,
spaceBetween: 10,
breakpoints: {
768: {
slidesPerView: 2,
spaceBetween: 15
},
1024: {
slidesPerView: 3,
spaceBetween: 20
},
}
});
.swiper-pagination-bullet-active {
background-color: #f40;
}
.swiper-pagination-bullet {
width: 8px;
height: 8px;
border-radius: 4px;
transition: width .4s;
}
.swiper-pagination-bullet-active {
width: 20px;
background-color: #f40;
}
.swiper-pagination-bullets.swiper-pagination-horizontal {
width: initial;
left: initial;
height: 100%;
right: 0;
top: 0;
display: flex;
flex-direction: column;
justify-content: center;
gap: 10px;
}
const swiper = new Swiper('.swiper', {
loop: true,
autoplay: true,
speed: 1200,
effect: 'fade',
pagination: {
el: '.swiper-pagination',
clickable: true,
renderBullet: function (index, className) {
return '' + String(index + 1).padStart(2, '0') + '';
}
}
});
.swiper {
width: 100%;
max-width: 800px;
aspect-ratio: 4/3;
}
.swiper-slide {
position: relative;
}
.swiper-slide img {
position: absolute;
transform: translateX(50%);
width: 100%;
opacity: 0;
transition: 0.4s;
}
.swiper-pagination-bullets.swiper-pagination-horizontal {
bottom: 20px;
display: flex;
justify-content: center;
}
.swiper-pagination-bullets .swiper-pagination-bullet {
width: 20px;
padding: 4px 10px;
display: grid;
place-items: center;
border-radius: 10px;
background: none;
opacity: 1;
align-content: center;
font-size: 12px;
color: #000;
margin: 0;
border: 1px solid transparent;
}
.swiper-pagination-bullets .swiper-pagination-bullet-active {
border-color: #000;
}
.swiper-slide-active img {
transform: translateX(0);
opacity: 1;
}
function formatFraction(currentIndex, totalSlides) {
const formattedCurrent = String(currentIndex).padStart(2, "0");
const formattedTotal = String(totalSlides).padStart(2, "0");
return `<span class="swiper-pagination-current">${formattedCurrent}</span>/<span class="swiper-pagination-total">${formattedTotal}</span>`;
}
const swiper = new Swiper(".swiper", {
loop: true,
autoplay: true,
speed: 1200,
effect: "fade",
pagination: {
el: ".swiper-pagination",
type: "custom", // 使用自定义分页器类型
renderCustom: function (swiper, current, total) {
return formatFraction(current, total); // 直接引用formatFraction函数
},
},
on: {
init: function () {
// 初始化时更新分页器文本
this.pagination.renderedFraction = formatFraction(
this.activeIndex + 1,
this.slides.length
);
},
slideChange: function () {
// 每当幻灯片变化时更新分页器文本
this.pagination.renderedFraction = formatFraction(
this.activeIndex + 1,
this.slides.length
);
},
},
});
const swiperInfo = document.querySelectorAll('.swiper-info')
const swiper = new Swiper('.swiper', {
// ...
});
swiper.on('slideChange', function () {
console.log('slide changed');
anime({
targets: swiperInfo[swiper.realIndex].children,
opacity: [0, 1],
duration: 400,
easing: "linear",
delay: anime.stagger(400, { start: 300 }),
translateY: [anime.stagger(40, 10), 0],
})
});