mirror of
https://git.rdcenter.ru/RDCenter/skb-kit.git
synced 2026-12-05 05:49:45 +00:00
Заменила слайдер используя беблиотеку swiper
This commit is contained in:
1 parent
5b46f720c7
commit
2849fdb9cb
4 files changed
+97
-105
No files matched your search
Generated
+20
@@ -28,6 +28,7 @@
|
||||
"reactpress": "^1.0.0",
|
||||
"sass": "^1.70.0",
|
||||
"scroll-carousel-react": "^1.0.0",
|
||||
"swiper": "^11.2.5",
|
||||
"use-callback-ref": "^1.3.1",
|
||||
"web-vitals": "^2.1.4",
|
||||
"wpapi": "^1.2.2"
|
||||
@@ -17211,6 +17212,25 @@
|
||||
"boolbase": "~1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/swiper": {
|
||||
"version": "11.2.5",
|
||||
"resolved": "https://registry.npmjs.org/swiper/-/swiper-11.2.5.tgz",
|
||||
"integrity": "sha512-nG0kbIyBfeE2BPFt9nPUX03qUBF75o6+enzjIT/DfCmbh8ORlwhc4eZz1+4H/yseAgb3H+OoEYzmb64i0tYNnQ==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "patreon",
|
||||
"url": "https://www.patreon.com/swiperjs"
|
||||
},
|
||||
{
|
||||
"type": "open_collective",
|
||||
"url": "http://opencollective.com/swiper"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4.7.0"
|
||||
}
|
||||
},
|
||||
"node_modules/symbol-tree": {
|
||||
"version": "3.2.4",
|
||||
"resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz",
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
"reactpress": "^1.0.0",
|
||||
"sass": "^1.70.0",
|
||||
"scroll-carousel-react": "^1.0.0",
|
||||
"swiper": "^11.2.5",
|
||||
"use-callback-ref": "^1.3.1",
|
||||
"web-vitals": "^2.1.4",
|
||||
"wpapi": "^1.2.2"
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import { useEffect, useState, createContext } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Swiper, SwiperSlide } from 'swiper/react';
|
||||
import { Navigation, Pagination } from 'swiper/modules';
|
||||
|
||||
import Arrows from './components/Arrows';
|
||||
import SlidesList from './components/SlidesList';
|
||||
// import Dots from './components/Dots';
|
||||
import 'swiper/css';
|
||||
import 'swiper/css/navigation';
|
||||
import 'swiper/css/pagination';
|
||||
|
||||
import { getMedia } from '../../api';
|
||||
import './Slider.styles.scss';
|
||||
|
||||
export const SliderContext = createContext();
|
||||
|
||||
function Slider({ autoPlay = 0, autoPlayTime = 0, images = false }) {
|
||||
function Slider({ autoPlay = false, autoPlayTime = 3000, images = false }) {
|
||||
const [mediaList, setMediaList] = useState([]);
|
||||
const [slide, setSlide] = useState(0);
|
||||
const [itemsPerSlide, setItemsPerSlide] = useState(window.innerWidth <= 768 ? 1 : 2);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -24,67 +23,36 @@ function Slider({ autoPlay = 0, autoPlayTime = 0, images = false }) {
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
let newMediaList;
|
||||
|
||||
if (images) {
|
||||
newMediaList = images.map((value, key) => {
|
||||
return { id: key, src: value };
|
||||
});
|
||||
} else {
|
||||
newMediaList = await getMedia('gallery');
|
||||
}
|
||||
const newMediaList = images
|
||||
? images.map((value, key) => ({ id: key, src: value }))
|
||||
: await getMedia('gallery');
|
||||
|
||||
setMediaList(newMediaList);
|
||||
console.log('sliderMedia', newMediaList);
|
||||
};
|
||||
|
||||
console.log(images);
|
||||
loadData();
|
||||
}, [images]);
|
||||
|
||||
const changeSlide = (direction) => {
|
||||
if (mediaList.length <= itemsPerSlide) return;
|
||||
|
||||
let slideNumber = slide + direction;
|
||||
|
||||
if (direction === -1 && slideNumber < 0) return;
|
||||
if (direction === 1 && slideNumber + itemsPerSlide > mediaList.length) return;
|
||||
|
||||
setSlide(slideNumber);
|
||||
};
|
||||
|
||||
const goToSlide = (number) => {
|
||||
if (number >= 0 && number + itemsPerSlide <= mediaList.length) {
|
||||
setSlide(number);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!autoPlay || mediaList.length <= itemsPerSlide) return;
|
||||
|
||||
const interval = setInterval(() => {
|
||||
changeSlide(1);
|
||||
}, autoPlayTime);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [mediaList.length, slide, itemsPerSlide]);
|
||||
|
||||
return (
|
||||
<div className="slider">
|
||||
<SliderContext.Provider
|
||||
value={{
|
||||
goToSlide,
|
||||
changeSlide,
|
||||
slidesCount: mediaList.length,
|
||||
slideNumber: slide,
|
||||
mediaList
|
||||
}}
|
||||
<Swiper
|
||||
modules={[Navigation, Pagination]}
|
||||
spaceBetween={20}
|
||||
slidesPerView={itemsPerSlide} // Количество картинок в видимой зоне
|
||||
slidesPerGroup={itemsPerSlide} // Перелистывать сразу по 2
|
||||
loop={true} // Зацикленный слайдер
|
||||
navigation // Стрелки
|
||||
pagination={{ clickable: true }}
|
||||
autoplay={autoPlay ? { delay: autoPlayTime, disableOnInteraction: false } : false}
|
||||
>
|
||||
{ mediaList.length > itemsPerSlide && <Arrows /> }
|
||||
<SlidesList />
|
||||
{/* <Dots /> */}
|
||||
</SliderContext.Provider>
|
||||
{mediaList.map((media) => (
|
||||
<SwiperSlide key={media.id}>
|
||||
<img src={media.src} alt="slide" className="slide-image" />
|
||||
</SwiperSlide>
|
||||
))}
|
||||
</Swiper>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Slider;
|
||||
@@ -1,57 +1,60 @@
|
||||
.slider {
|
||||
overflow: hidden;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
& .slide-list {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center; /* Центрируем изображения */
|
||||
gap: 32px; /* Отступ между картинками */
|
||||
width: max-content;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
transition: transform 0.5s ease-in-out;
|
||||
margin: 0 auto;
|
||||
padding: 10px 100px; /* Добавляем отступы по бокам */
|
||||
|
||||
& .slide {
|
||||
flex: 1 0 50%;
|
||||
max-width: 50%; /* Ограничение ширины */
|
||||
position: relative;
|
||||
display: flex;
|
||||
justify-content: center; /* Центрируем контент */
|
||||
|
||||
& .slide-image {
|
||||
display: flex;
|
||||
max-height: 60vh;
|
||||
max-width: 100%;
|
||||
border-radius: 8px;
|
||||
}
|
||||
}
|
||||
.swiper-slide {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 60vh;
|
||||
@media (max-width: 768px) {
|
||||
height: 35vh;
|
||||
}
|
||||
}
|
||||
|
||||
/* Медиа-запрос для мобильных устройств */
|
||||
@media (max-width: 768px) {
|
||||
.slider {
|
||||
& .slide-list {
|
||||
justify-content: left;
|
||||
gap: 0; /* Убираем лишние отступы */
|
||||
padding: 0; /* Уменьшаем отступы */
|
||||
}
|
||||
.slide-image {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
max-height: 100%;
|
||||
object-fit: cover;
|
||||
object-position: center center;
|
||||
border-radius: 8px;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
& .slide-list .slide {
|
||||
flex: 1 0 100%; /* Одна картинка в ряд */
|
||||
max-width: 100%; /* Использует всю ширину */
|
||||
display: flex;
|
||||
justify-content: center; /* Центрируем */
|
||||
}
|
||||
/* стрелки */
|
||||
.swiper-button-prev,
|
||||
.swiper-button-next {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
background-color: white;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
||||
cursor: pointer;
|
||||
|
||||
& .slide-list .slide .slide-image {
|
||||
max-width: 90%; /* Делаем изображение большим, но с небольшими отступами */
|
||||
height: auto; /* Сохраняем пропорции */
|
||||
max-height: 70vh; /* Ограничиваем высоту */
|
||||
border-radius: 8px;
|
||||
}
|
||||
&::after {
|
||||
font-size: 18px;
|
||||
font-weight: 1000;
|
||||
color: black;
|
||||
}
|
||||
}
|
||||
|
||||
/* Позиционирование стрелок */
|
||||
.swiper-button-prev {
|
||||
left: 5px;
|
||||
@media (max-width: 768px) {
|
||||
left: 0px;
|
||||
}
|
||||
}
|
||||
|
||||
.swiper-button-next {
|
||||
right: 5px;
|
||||
@media (max-width: 768px) {
|
||||
right: 0px;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user