¥Installation
如何在项目中包含/导入 Swiper 有以下几种方法:
¥There are few options on how to include/import Swiper into your project:
¥Install from NPM
我们可以从 NPM 安装 Swiper
¥We can install Swiper from NPM
$ npm install swiper
// import Swiper JS
import Swiper from 'swiper';
// import Swiper styles
import 'swiper/css';
const swiper = new Swiper(...);
默认情况下,Swiper 仅导出核心版本,不包含其他模块(例如导航、分页等)。你还需要导入并配置它们:
¥By default Swiper exports only core version without additional modules (like Navigation, Pagination, etc.). So you need to import and configure them too:
// core version + navigation, pagination modules:
import Swiper from 'swiper';
import { Navigation, Pagination } from 'swiper/modules';
// import Swiper and modules styles
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
// init Swiper:
const swiper = new Swiper('.swiper', {
// configure Swiper to use modules
modules: [Navigation, Pagination],
...
});
如果你想导入 Swiper 及其所有模块(bundle),则应从 swiper/bundle
导入:
¥If you want to import Swiper with all modules (bundle) then it should be imported from swiper/bundle
:
// import Swiper bundle with all modules installed
import Swiper from 'swiper/bundle';
// import styles bundle
import 'swiper/css/bundle';
// init Swiper:
const swiper = new Swiper(...);
¥Use Swiper from CDN
如果你不想在项目中包含 Swiper 文件,你可以从 CDN 使用它。可用文件如下:
¥If you don't want to include Swiper files in your project, you may use it from CDN. The following files are available:
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>
如果你在浏览器中使用 ES 模块,也有一个 CDN 版本:
¥If you use ES modules in browser, there is a CDN version for that too:
<script type="module">
import Swiper from 'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.mjs'
const swiper = new Swiper(...)
</script>
¥Download assets
如果你想在本地使用 Swiper 资源,可以直接从 https://www.jsdelivr.com/package/npm/swiper 下载。
¥If you want to use Swiper assets locally, you can directly download them from https://www.jsdelivr.com/package/npm/swiper
¥Add Swiper HTML Layout
现在,我们需要在应用中添加基本的 Swiper 布局:
¥Now, we need to add basic Swiper layout to our app:
<!-- Slider main container -->
<div class="swiper">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
...
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>
<!-- If we need navigation buttons -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<!-- If we need scrollbar -->
<div class="swiper-scrollbar"></div>
</div>
¥Swiper CSS Styles/Size
除了 Swiper's CSS styles 组件外,我们可能需要添加一些自定义样式来设置 Swiper 的大小:
¥In addition to Swiper's CSS styles, we may need to add some custom styles to set Swiper size:
.swiper {
width: 600px;
height: 300px;
}
¥Initialize Swiper
最后,我们需要在 JS 中初始化 Swiper:
¥Finally, we need to initialize Swiper in JS:
const swiper = new Swiper('.swiper', {
// Optional parameters
direction: 'vertical',
loop: 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',
},
});
¥What next?
正如你所见,将 Swiper 集成到你的网站或应用中非常容易。接下来的步骤如下:
¥As you see it is really easy to integrate Swiper into your website or app. So here are your next steps:
前往 API Documentation 了解更多关于 Swiper API 及其控制方法的信息。
¥Go to API Documentation to learn more about all Swiper API and how to control it.
查看可用的 Demos。
¥Look at available Demos.
如果你对 Swiper 有任何疑问,请在 StackOverflow 或 Swiper Discussions 中提问。
¥If you have questions about Swiper ask them in StackOverflow or Swiper Discussions.
如果你发现 GitHub 存在 bug,请创建 issue。
¥Create issue on GitHub if you found a bug.
如果你需要支持,我们有一个 Swiper Patrons 的私有 Discord 支持聊天室。
¥If you are looking for support, we have a private Discord support chat room for Swiper Patrons.