won*_*ngz 24 javascript reactjs swiper.js
在我的 ReactJS 应用程序中使用 SwiperJS。我已导入默认样式包,但无法弄清楚如何设置分页容器或项目符号的样式。
在pagination:参数中...每次我更改el: 参数时,分页就会消失。每次我更改bulletClass:样式时,我在 css 中添加的样式都不会被应用。
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, { Pagination, Navigation, A11y } from 'swiper';
import 'swiper/swiper-bundle.css';
SwiperCore.use([Navigation, Pagination, A11y]);
return (
<>
<Swiper
spaceBetween={50}
slidesPerView={1}
navigation
pagination={{
clickable: true,
el: `swiper-container swiper-container-testClass`,
bulletClass: `swiper-pagination-bullet swiper-pagination-testClass`
}}
wrapperTag='ul'
>
<SwiperSlide tag='li' key={1}>
{<div>My Data</div>}
</SwiperSlide>
</Swiper>
</>
)
Run Code Online (Sandbox Code Playgroud)
有人知道如何覆盖默认样式吗?也就是说,我希望将分页容器移动到幻灯片内容上方,而不是底部幻灯片内部(甚至看不到它)。
有问题的 API:Swiper React
小智 36
由于没有关于这些的文档信息,我通过查看捆绑文件找到了方法。
在那里您可以遇到可以修改以自定义滑块的变量,如下所示:
:root {
/*
--swiper-pagination-color: var(--swiper-theme-color);
--swiper-pagination-bullet-size: 8px;
--swiper-pagination-bullet-width: 8px;
--swiper-pagination-bullet-height: 8px;
--swiper-pagination-bullet-inactive-color: #000;
--swiper-pagination-bullet-inactive-opacity: 0.2;
--swiper-pagination-bullet-opacity: 1;
--swiper-pagination-bullet-horizontal-gap: 4px;
--swiper-pagination-bullet-vertical-gap: 6px;
*/
}Run Code Online (Sandbox Code Playgroud)
只需添加此变量并在滑动滑块上设置您想要的任何颜色/大小/属性即可。
前任:
<Swiper style={{
"--swiper-pagination-color": "#FFBA08",
"--swiper-pagination-bullet-inactive-color": "#999999",
"--swiper-pagination-bullet-inactive-opacity": "1",
"--swiper-pagination-bullet-size": "16px",
"--swiper-pagination-bullet-horizontal-gap": "6px"
}}>
{content}
</Swiper>Run Code Online (Sandbox Code Playgroud)
小智 19
要设置分页项目符号的样式,只需将此行添加到您的全局 CSS 中
.swiper-pagination-bullet-active {
background-color: #000 !important;
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,我解决的方法是:
进口声明
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/swiper.min.css";
import "swiper/components/pagination/pagination.min.css"
// Import my scss file
import styles from './styles.module.scss'
// import Swiper core and required modules
import SwiperCore, {
Pagination
} from 'swiper/core';
// install Swiper modules
SwiperCore.use([Pagination]);
Run Code Online (Sandbox Code Playgroud)
首先,用SliderWrapper类包装我的组件,如下所示:
return (
<div className={styles.SliderWrapper}>
<Swiper
pagination={true}
>
<SwiperSlide>
<div>{My Data}</div>
</SwiperSlide>
<SwiperSlide>
<div>{My Data}</div>
</SwiperSlide>
<SwiperSlide>
<div>{My Data}</div>
</SwiperSlide>
</Swiper>
</div>
)
Run Code Online (Sandbox Code Playgroud)
其次,我在浏览器中检查了应该使用哪些类来覆盖样式。就我而言是:.swiper-pagination-bullet和.swiper-pagination-bullet-active
然后,技巧是在我的 css 中使用 :global 来设置 swiper 的分页样式,我使用它,如下例所示:
.sliderWrapper {
:global(.swiper-pagination-bullet) {
background: white;
width: 1rem;
height: 1rem;
border-radius: .5rem;
opacity: 1;
}
:global(.swiper-pagination-bullet-active) {
background-color: blue;
width: 5rem;
height: 1rem;
border-radius: .5rem;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用这些类名覆盖分页项目符号的默认样式:
/* target the active bullet */
span.swiper-pagination-bullet.swiper-pagination-bullet-active {
background-color: blue;
opacity: 1;
}
/* target all bullets */
.swiper-pagination-bullet {
background-color: red;
opacity: 1;
}
Run Code Online (Sandbox Code Playgroud)
我对 Vue.js 有类似的问题,我认为 oververride 默认样式的唯一方法是使用 css!important但并不总是,例如,如果你想更改项目符号颜色,你可以访问项目符号默认样式,而不强制使用默认样式
/* change active bullet color to red */
.swiper-pagination-bullet-active {
opacity: 1;
background: var(--swiper-pagination-color, var(--swiper-theme-color));
background-color: red;
}
/*move swiper pagination on top to image */
.swiper-pagination {
position: absolute;
text-align: center;
transition: 300ms opacity;
transform: translate3d(0, 0, 0);
z-index: 10;
top: 0 !important;
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
96501 次 |
| 最近记录: |