Swiper
API 文档
示例
Hooks
index.jsx
import React, { useState, useEffect } from "react";
import errorBoundary from "@ifeng/errorBoundary";
import styles from "./index.css";
import PropTypes from "prop-types";
// 分页组件
import Swiper, { Navigation, Pagination } from "swiper";
Swiper.use([Navigation, Pagination]);
const Slider = ({ content }) => {
const [currentData, setCurrentData] = useState({});
useEffect(() => {
new Swiper("#swiper1", {
autoplay: true,
loop: true,
navigation: {
prevEl: "#prev1",
nextEl: "#next1",
},
pagination: {
el: "#pagination1",
renderBullet(index, className) {
return `<span class="${className}"></span>`;
},
},
on: {
//监听轮播切换结束
slideChangeTransitionEnd() {
setCurrentData(content[this.realIndex]);
},
},
});
}, []);
return (
<div className={styles.swiperWp}>
<div className={`${styles.swiper1} swiper`} id="swiper1">
<div className="swiper-wrapper">
{content.map((item, index) => {
return (
<div className="swiper-slide" key={index}>
<a href={item.url} target="_blank" rel="noreferrer">
<img
className={styles.swiperImage}
src={item}
alt={item.title}
/>
</a>
</div>
);
})}
</div>
</div>
<div className={styles.prev1} id="prev1" />
<div className={styles.next1} id="next1" />
<div className={styles.swiperText}>
<a href={currentData.url} target="_blank" rel="noreferrer">
{currentData.title}
</a>
</div>
<div className={styles.pagination1} id="pagination1" />
</div>
);
};
Slider.propTypes = {
content: PropTypes.array,
};
export default errorBoundary(Slider);
index.css
.swiperWp {
position: relative;
margin-bottom: 40px;
& .swiper1 {
width: 640px;
height: 360px;
margin: 0;
& .swiperImage {
width: 640px;
height: 360px;
object-fit: cover;
}
& a {
width: 640px;
height: 360px;
object-fit: cover;
}
}
& .prev1,
& .next1 {
position: absolute;
top: 169px;
z-index: 2;
cursor: pointer;
width: 23px;
height: 50px;
background-size: 100%;
}
& .prev1 {
left: 0;
background-image: url("https://x0.ifengimg.com/ucms/2022_03/AA24DC9C9B51E72A65D52803CC85305330FB6162_size1_w46_h100.png");
&:hover {
background-image: url("https://x0.ifengimg.com/ucms/2022_03/C31F7ACC9F10B4FAA256BEF00FD3A545B59FFBE3_size1_w46_h100.png");
}
}
& .next1 {
right: 0;
background-image: url("https://x0.ifengimg.com/ucms/2022_03/B02C694D511068206E2D20BC490099FD30F69E10_size1_w46_h100.png");
&:hover {
background-image: url("https://x0.ifengimg.com/ucms/2022_03/4C8541040389B6F042D02DE7DCCC0D0DE8FECC92_size1_w46_h100.png");
}
}
& .pagination1 {
/* outline: 1px solid red; */
position: absolute;
/* left: 0; */
right: 0;
bottom: 7px;
margin: 0 auto;
text-align: center;
z-index: 3;
pointer-events: none;
}
& .swiperText {
/* outline: 1px solid red; */
position: absolute;
left: 0;
right: 0;
text-align: left;
/* display: flex; */
/* align-items: center; */
height: 34px;
background-color: rgba(0, 0, 0, 0.6);
/* background: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1)); */
color: white;
bottom: 0;
width: 100%;
padding: 0 15px;
/* font-size: 17px; */
line-height: 34px;
box-sizing: border-box;
z-index: 2;
cursor: pointer;
& a {
/* outline: 1px solid red; */
color: white;
width: 100%;
font-weight: 400;
font-size: 14px;
}
&:hover {
text-decoration: underline;
}
}
}
:global(#pagination1 .swiper-pagination-bullet) {
display: inline-block;
width: 4px;
height: 4px;
border-radius: 100%;
margin: 0 4px;
background-color: rgba(255, 255, 255, 0.5);
}
:global(#pagination1
.swiper-pagination-bullet.swiper-pagination-bullet-active) {
width: 12px;
border-radius: 2px;
}