参考:

- Swiper のHTML部分
- Styles (翻訳省略)
- Initialize Swiper (翻訳省略)
- Parameters
- Methods & Properties
- Events
- Modules
- Custom Build
- TypeScript Definitions
Swiper のHTML部分
<!-- Slider main container -->
<div class="swiper-container">
<!-- 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>
Styles (翻訳省略)
Swiper package contains different sets of CSS, Less and SCSS styles:
CSS Styles
CSS styles for bundle version:
swiper-bundle.css- all Swiper styles including all modules styles (like Navigation, Pagination, etc.)swiper-bundle.min.css- same as previous but minified
CSS styles for core version and modules:
swiper.min.css- only core Swiper stylescomponents/a11y/a11y.min.css- styles required for A11y modulecomponents/controller/controller.min.css- styles required for Controller modulecomponents/effect-coverflow/effect-coverflow.min.css- styles required for Coveflow Effect modulecomponents/effect-cube/effect-cube.min.css- styles required for Cube Effect modulecomponents/effect-fade/effect-fade.min.css- styles required for Fade Effect modulecomponents/effect-flip/effect-flip.min.css- styles required for Flip Effect modulecomponents/lazy/lazy.min.css- styles required for Lazy modulecomponents/navigation/navigation.min.css- styles required for Navigation modulecomponents/pagination/pagination.min.css- styles required for Pagination modulecomponents/scrollbar/scrollbar.min.css- styles required for Scrollbar modulecomponents/thumbs/thumbs.min.css- styles required for Thumbs modulecomponents/zoom/zoom.min.css- styles required for Zoom module
Less Styles
Less styles are separate styles for core version and modules:
swiper.less- only core Swiper stylescomponents/a11y/a11y.less- styles required for A11y modulecomponents/controller/controller.less- styles required for Controller modulecomponents/effect-coverflow/effect-coverflow.less- styles required for Coveflow Effect modulecomponents/effect-cube/effect-cube.less- styles required for Cube Effect modulecomponents/effect-fade/effect-fade.less- styles required for Fade Effect modulecomponents/effect-flip/effect-flip.less- styles required for Flip Effect modulecomponents/lazy/lazy.less- styles required for Lazy modulecomponents/navigation/navigation.less- styles required for Navigation modulecomponents/pagination/pagination.less- styles required for Pagination modulecomponents/scrollbar/scrollbar.less- styles required for Scrollbar modulecomponents/thumbs/thumbs.less- styles required for Thumbs modulecomponents/zoom/zoom.less- styles required for Zoom module
SCSS Styles
SCSS styles are also separate styles for core version and modules:
swiper.scss- only core Swiper stylescomponents/a11y/a11y.scss- styles required for A11y modulecomponents/controller/controller.scss- styles required for Controller modulecomponents/effect-coverflow/effect-coverflow.scss- styles required for Coveflow Effect modulecomponents/effect-cube/effect-cube.scss- styles required for Cube Effect modulecomponents/effect-fade/effect-fade.scss- styles required for Fade Effect modulecomponents/effect-flip/effect-flip.scss- styles required for Flip Effect modulecomponents/lazy/lazy.scss- styles required for Lazy modulecomponents/navigation/navigation.scss- styles required for Navigation modulecomponents/pagination/pagination.scss- styles required for Pagination modulecomponents/scrollbar/scrollbar.scss- styles required for Scrollbar modulecomponents/thumbs/thumbs.scss- styles required for Thumbs modulecomponents/zoom/zoom.scss- styles required for Zoom module
Initialize Swiper (翻訳省略)
Now, when we have Swiper's HTML, we need to initialize it using the following function:
new Swiper(swiperContainer, parameters)- initialize swiper with options
- swiperContainer - HTMLElement or string (with CSS Selector) of swiper container HTML element. Required.
- parameters - object - object with Swiper parameters. Optional.
- Method returns initialized Swiper instance
For example:
const swiper = new Swiper('.swiper-container', {
speed: 400,
spaceBetween: 100,
});
After you initialize Swiper it is possible to access to Swiper's instance on its HTMLElement. It is swiper property of Swiper's HTML container element:
const swiper = document.querySelector('.swiper-container').swiper;
// Now you can use all slider methods like
swiper.slideNext();Parameters
Let's look on list of all available parameters:
| Name | Type | Default | Description | ||||
|---|---|---|---|---|---|---|---|
| allowSlideNext -次のスライドに移れるか | boolean | true | falseに設定すると、次のスライド方向(右または下)へのスワイプが無効になります。 | ||||
| allowSlidePrev -前のスライドに移れるか | boolean | true | falseに設定すると、前のスライド方向(左または上)へのスワイプが無効になります。 | ||||
| allowTouchMove -スワイプなどのタッチ 操作でスライドを 移動できるか | boolean | true | falseの場合、スライドを切り替えるには、slidePrevやslideNextなどの外部API関数を使用するしかありません。 | ||||
| autoHeight | boolean | false | trueに設定すると、スライダー・ラッパーの高さは、現在アクティブなスライドの高さに合わせます | ||||
| breakpoints | object | レスポンシブブレークポイント(画面サイズ)ごとに、異なるパラメータを設定できるようになります。すべてのパラメータがブレイクポイントで変更できるわけではなく、slidesPerView、slidesPerGroup、spaceBetween、slidesPerColumnのような、異なるレイアウトやロジックを必要としないパラメータのみ変更できます。ループやエフェクトなどのパラメータは動作しません。const swiper = new Swiper('.swiper-container', {
// Default parameters
slidesPerView: 1,
spaceBetween: 10,
// Responsive breakpoints
breakpoints: {
// when window width is >= 320px
320: {
slidesPerView: 2,
spaceBetween: 20
},
// when window width is >= 480px
480: {
slidesPerView: 3,
spaceBetween: 30
},
// when window width is >= 640px
640: {
slidesPerView: 4,
spaceBetween: 40
}
}
})const swiper = new Swiper('.swiper-container', {
slidesPerView: 1,
spaceBetween: 10,
// using "ratio" endpoints
breakpoints: {
'@0.75': {
slidesPerView: 2,
spaceBetween: 20,
},
'@1.00': {
slidesPerView: 3,
spaceBetween: 40,
},
'@1.50': {
slidesPerView: 4,
spaceBetween: 50,
},
}
}); | |||||
| breakpointsBase -siper-containerの幅で ブレイクポイントを 設置するか | string | 'window' | ブレイクポイントのベース(ベータ版)。windowまたはcontainerを指定します。windowに設定されている場合(デフォルト)、ブレイクポイントキーはwindowの幅を意味します。containerに設定されている場合、ブレークポイント・キーはSwiperのコンテナ幅として扱われます。
| ||||
| centerInsufficientSlides | boolean | false | 有効にすると、スライド数がslidesPerViewより少ない場合、スライドを中央に配置します。loopモードとslidesPerColumnの使用は意図していません。 | ||||
| centeredSlides | boolean | false | trueの場合、アクティブなスライドは中央に表示され、常に左側に表示されるわけではありません。
| ||||
| centeredSlidesBounds | boolean | false | trueの場合、アクティブなスライドは、スライダーの最初と最後にギャップを追加することなく中央に配置されます。centeredSlides: trueを指定してください。loop や paginationとの併用は想定していません。 | ||||
| containerModifierClass | string | 'swiper-container-' | さまざまなパラメータに応じてswiper container部分に追加される接頭辞モディファイアCSSクラス swiper-container、swiper-wrapper、swiper-slideは、class名は基本的にそのままで使い、class名を変更する場合はパラメータ(containerModifierClass、wrapperClass、slideClass)を使用し、変更します。 | ||||
| cssMode | boolean | false | 有効にすると、最新のCSS Scroll Snap APIを使用します。Swiperのすべての機能をサポートしているわけではありませんが、シンプルな構成であれば、より優れたパフォーマンスを発揮する可能性があります。 有効にするとサポートされないものは以下の通りです。
| ||||
| direction -縦スライドか横スライドか | 'horizontal' | 'vertical' | 'horizontal' | 「水平(horizontal)」または「垂直(vertical)(縦型スライダーの場合)」を指定できます。 | ||||
| edgeSwipeDetection | boolean | string | false | アプリでのスワイプバック作業(=戻るボタンを幼くても画面端をスワイプで前の画面に戻る)のためのSwiperイベントの解放を有効にします。preventに設定すると、代わりにシステムのスワイプバックナビゲーションを防止します。 | ||||
| edgeSwipeThreshold | number | 20 | 画面の左端からアプリのスワイプバックのタッチイベントを解除するまでの領域(px単位)(=左端から◯pxまでの領域でスワイプするとスワイプバックする?) | ||||
| effect | 'slide' , 'fade' , 'cube' 'coverflow' 'flip' | 'slide' | |||||
| enabled | boolean | true | Swiperを初期状態で有効にしているかどうか。Swiperを無効にすると、すべてのナビゲーション要素が非表示になり、いかなるイベントや相互作用にも反応しなくなります | ||||
| followFinger | boolean | true | 無効にした場合、スライダーは指を離した時のみアニメーションし、指を置いている間は動きません。 | ||||
| freeMode | boolean | false | 有効にすると、スライドの位置が固定されません。
| ||||
| freeModeMinimumVelocity | number | 0.02 | フリーモードモメンタムの発生に必要な最低タッチ移動速度
| ||||
| freeModeMomentum | boolean | true | 有効にすると、スライドを離した後もしばらくの間、スライドが動き続けます。
| ||||
| freeModeMomentum Bounce | boolean | true | フリーモードでモーメンタムバウンスを無効にしたい場合は、falseを設定します。
| ||||
| freeModeMomentum BounceRatio | number | 1 | 値が大きいほど、モーメンタムバウンス効果が大きくなります。
| ||||
| freeModeMomentumRatio | number | 1 | 値が大きいほど、スライダを離した後の運動量(完成スクロールみたいなののスクロール量)が大きくなります。 | ||||
| freeModeMomentum VelocityRatio | number | 1 | 値が大きいほど、スライダを離した後の運動量が大きくなります。 =数字を大きくすれば、慣性スクロール的なやつの速度を早くできる。 | ||||
| freeModeSticky | boolean | false | フリーモードでのスライドポジションへのスナップを有効にするには、enabledに設定します。
| ||||
| grabCursor | boolean | false | このオプションは、デスクトップの使い勝手を少し改善する可能性があります。trueの場合、Swiperにカーソルを置いたときに "grab "カーソルが表示されます。
| ||||
| height | number | null | null | スワイパーの高さ(単位:px)。パラメータは、Swiperの高さを強制的に変更します。Swiperが隠れているときにSwiperを初期化する場合や、SSRおよびTest環境でSwiperを正しく初期化する場合にのみ有用です。
| ||||
| init | boolean | true | インスタンス作成時にSwiperを自動的に初期化するかどうかを指定します。無効の場合は、swiper.init()を呼び出して手動で初期化する必要があります。
| ||||
| initialSlide | number | 0 | 最初に表示されるスライドのインデックス番号。 | ||||
| longSwipes | boolean | true | 長時間のスワイプを無効にしたい場合は、falseに設定してください。
| ||||
| longSwipesMs | number | 300 | 長時間のスワイプ時に次/前のスライドへのスワイプをトリガーするための最小持続時間(ms)
| ||||
| longSwipesRatio | number | 0.5 | 長時間のスワイプで次/前のスライドに移動する比率
| ||||
| loop | boolean | false | 連続ループモードを有効にするには、trueを設定します。 ループモードの動作の性質上、重複したスライドが追加されます。そのような重複したスライドには、追加のクラスがあります。
slidesPerView: 'auto'と一緒に使う場合は、loopedSlidesパラメータにループ(複製)するスライドの数を指定する必要があります。
| ||||
| loopAdditionalSlides | number | 0 | ループ作成後にクローン化されるスライドの数の追加
| ||||
| loopFillGroupWithBlank | boolean | false | 有効にしてループモードにすると、スライドの数が足りないグループを空白のスライドで埋めます。slidesPerGroupパラメータと併用すると良いでしょう。
| ||||
| loopPreventsSlide | boolean | true | 有効にすると、トランジションがすでに進行しているときに、Swiperのスライドの前/次のトランジションを防ぐことができます(ループを有効にすると効果があります)。
| ||||
| loopedSlides | number | null | null | ループモードでslidesPerView:'auto'を使用する場合は、このパラメータを使用して、何枚のスライドをループ(複製)させるかをSwiperに伝える必要があります。
| ||||
| nested | boolean | false | 正しいタッチイベントのインターセプトのために、Swiperでtrueに設定します。親機と同じ方向を使用するスワイパーにのみ使用します。
| ||||
| noSwiping | boolean | true | noSwipingClassで指定されたクラスにマッチした要素に対するスワイプの有効化/無効化
| ||||
| noSwipingClass | string | 'swiper-no-swiping' | noSwipingの要素のcssクラスの指定
| ||||
| noSwipingSelector | string | noSwipingClassの代わりに、スワイプを無効にする要素を指定することができます。例えば、'input' はすべての入力に対してスワイプを無効にします。
| |||||
| normalizeSlideIndex | boolean | true | Normalize slide index.
| ||||
| observeParents | boolean | false | Swiperの親要素のミューテーションも監視する必要がある場合は、trueに設定します。
| ||||
| observeSlideChildren | boolean | false | Swiperスライドの子要素のミューテーションも見る必要がある場合は、trueに設定します。
| ||||
| observer | boolean | false | Swiperとその要素のMutation Observerを有効にするには、trueを設定します。この場合、Swiperは、そのスタイル(非表示/表示など)を変更したり、その子要素(スライドの追加/削除など)を変更したりするたびに更新(再初期化)されます。
| ||||
| on | object | イベントハンドラーの登録
| |||||
| passiveListeners | boolean | true | モバイルデバイスでのスクロールのパフォーマンスを向上させるため、可能な限りパッシブイベントリスナーがデフォルトで使用されます。しかし、e.preventDefaultを使用する必要があり、それに抵触する場合は、このパラメータを無効にしてください。
| ||||
| preloadImages | boolean | true | 有効にすると、Swiperはすべての画像を強制的にロードします。
| ||||
| preventClicks | boolean | true | スワイプ時にリンクを誤ってクリックしてしまうことを防ぐために、trueを設定します。
| ||||
| preventClicksPropagation | boolean | true | スワイプ中のリンクに対するクリックイベントの伝搬を停止するには、trueを設定します。
| ||||
| preventInteraction OnTransition | boolean | false | 有効にすると、遷移中にスワイプやナビゲーション/ページネーションボタンでスライドを変更できなくなります。
| ||||
| resistance | boolean | true | レジスタント・バウンズを無効にしたい場合は、falseに設定してください。
| ||||
| resistanceRatio | number | 0.85 | このオプションでは、抵抗比をコントロールすることができます。
| ||||
| resizeObserver | boolean | false | 有効にすると、(ブラウザでサポートされていれば)ResizeObserverを使用して、(ウィンドウのリサイズを監視する代わりに)コンテナのリサイズを検出します。 | ||||
| roundLengths | boolean | false | Trueを設定すると、通常の解像度の画面でテキストがぼやけないように、スライドの幅と高さの値を丸めることができます(通常の解像度の画面がある場合)。
| ||||
| runCallbacksOnInit | boolean | true | スイパーの初期化時にTransition/SlideChange/Start/Endイベントを発生させます。これらのイベントは、initialSlideが0でない場合や、ループモードを使用している場合に初期化時に発生します。
| ||||
| setWrapperSize | boolean | false | このオプションを有効にすると、プラグインはスワイパー・ラッパーの幅/高さをすべてのスライドの合計サイズと同じにします。フレックスボックスレイアウトに対応していないブラウザでは、互換性のための代替オプションとして使用されることがほとんどです。
| ||||
| shortSwipes | boolean | true | ショートスワイプを無効にしたい場合は、falseに設定してください。
| ||||
| simulateTouch | boolean | true | trueの場合、Swiperはタッチイベントのようなマウスイベントを受け付けます(クリック&ドラッグでスライドを変更することができます)。
| ||||
| slideActiveClass | string | 'swiper-slide-active' | 現在アクティブなスライドのCSSクラス名 ※クラスを変更することで、SwiperのCSSも変更されたクラスを反映させる必要があります。 ※Swiper Angular/React/Svelte/Vueコンポーネントではサポートされていません。
| ||||
| slideBlankClass | string | 'swiper-slide-invisible-blank' | loopFillGroupWithBlankが有効な場合に、ループモードの塗りつぶしグループに追加される空白のスライドのCSSクラス名
| ||||
| slideClass | string | 'swiper-slide' | SlideのCSSクラス名
| ||||
| slideDuplicateActiveClass | string | 'swiper-slide-duplicate-active' | 現在のアクティブなスライドを表す複製されたスライドのCSSクラス名
| ||||
| slideDuplicateClass | string | 'swiper-slide-duplicate' | ループモードで複製されるスライドのCSSクラス名
| ||||
| slideDuplicateNextClass | string | 'swiper-slide-duplicate-next' | アクティブなスライドの次のスライドを表す複製されたスライドのCSSクラス名
| ||||
| slideDuplicatePrevClass | string | 'swiper-slide-duplicate-prev' | 複製されたスライドのCSSクラス名(アクティブなスライドの前のスライドを表す) ※クラスを変更すると、SwiperのCSSも変更されたクラスを反映させる必要があります。 ※Swiper Angular/React/Svelte/Vueではサポートされていません。 | ||||
| slideNextClass | string | 'swiper-slide-next' | 現在アクティブなスライドの直後に表示されるスライドのCSSクラス名
| ||||
| slidePrevClass | string | 'swiper-slide-prev' | 現在アクティブなスライドの直前に表示されるスライドのCSSクラス名
| ||||
| slideToClickedSlide | boolean | false | trueに設定し、任意のスライドをクリックすると、このスライドに遷移します。
| ||||
| slideVisibleClass | string | 'swiper-slide-visible' | 現在表示されているスライドのCSSクラス名 ※ クラスを変更すると、SwiperのCSSも変更されたクラスを反映させる必要があります。 ※ Swiper Angular/React/Svelte/Vueではサポートされていません。 | ||||
| slidesOffsetAfter | number | 0 | コンテナの最後(すべてのスライドの後)に追加のスライドオフセットを(pxで)追加する。
| ||||
| slidesOffsetBefore | number | 0 | コンテナの最初(すべてのスライドの前)に、追加のスライドオフセットを(px単位で)追加します。
| ||||
| slidesPerColumn | number | 1 | 多列レイアウトの場合、1列あたりのスライド数
| ||||
| slidesPerColumnFill | 'row' | 'column' | 'column' | 「column」または「row」を指定します。スライドがどのように行を埋めるかを、列または行ごとに定義します。
| ||||
| slidesPerGroup | number | 1 | グループスライドを定義して有効にするために、スライドの数を設定します。slidesPerView > 1で使用すると便利です。
| ||||
| slidesPerGroupSkip | number | 0 | このパラメータは以下のように動作します。slidesPerGroupSkipが0(デフォルト)の場合、グループ化から除外されるスライドはなく、結果としてこの変更がない場合と同じ動作になります。 slidesPerGroupSkipが1以上の場合、最初のX枚のスライドは単一のグループとして扱われますが、それ以降のすべてのスライドはslidesPerGroupの値によってグループ化されます。
| ||||
| slidesPerView | number | 'auto' | 1 | 1ビューあたりのスライド数(スライダーのコンテナに同時に表示されるスライド)。
| ||||
| spaceBetween | number | 0 | スライド間の距離(単位:px)。 | ||||
| speed | number | 300 | スライド間の遷移時間(単位:ms) | ||||
| swipeHandler | CSSSelector | HTMLElement null | null | スワイプ時に唯一利用可能なハンドラとして動作するページネーションを持つコンテナのCSSセレクタまたはHTML要素の文字列
| ||||
| threshold | number | 0 | しきい値(単位:px)。「タッチ距離(touch distance)」がこの値よりも低い場合、スワイパーは動きません。
| ||||
| touchAngle | number | 45 | タッチ動作のトリガーとなる許容角度(度)
| ||||
| touchEventsTarget | 'container' | 'wrapper' | 'container' | タッチイベントをリッスンする対象となる要素。「container」(swiper-containerのタッチ・イベントを受け付ける)または「wrapper」(swiper-wrapperのタッチ・イベントを受け付ける)のいずれかです。
| ||||
| touchMoveStopPropagation | boolean | false | 有効にすると、"touchmove "の伝搬が停止します。
| ||||
| touchRatio | number | 1 |
| ||||
| touchReleaseOnEdges | boolean | false | スライダーの端の位置(始点、終点)でタッチイベントを解除し、さらにページをスクロールできるようにする。
| ||||
| touchStartForce PreventDefault | boolean | false | touchstart(mousedown)イベントのデフォルトを常に防ぐように強制する。
| ||||
| touchStartPreventDefault | boolean | true | 無効にした場合、タッチスタート(マウスダウン)イベントは防止されません。
| ||||
| uniqueNavElements | boolean | true | 有効(デフォルト)で、ナビゲーション要素のパラメータが文字列(「.pagination」など)として渡された場合、Swiperは最初に子要素を介してそのような要素を探します。ページネーション、prev/nextボタン、スクロールバー要素に適用されます。
| ||||
| updateOnImagesReady | boolean | true | 有効にすると、すべてのインナーイメージ(タグ)が読み込まれた後、Swiperが再初期化されます。必須 preloadImages: true
| ||||
| updateOnWindowResize | boolean | true | ウィンドウのサイズを変更すると、Swiperはスライドの位置を再計算する(オリエンテーション変更)。
| ||||
| url | string | null | null | サーバーサイドでレンダリングされ、ヒストリーが有効になっている場合のアクティブなスライド検出に必要です。
| ||||
| userAgent | string | null | null | userAgent文字列。サーバーサイドでレンダリングする際のブラウザ/デバイスの検出に必要です。 | ||||
| virtualTranslate | boolean | false | このオプションを有効にすると、 スワイパーは通常通り操作されますが、 スワイパーは動きませんし、 ラッパー上のリアル・トランスレート値も設定されません。カスタム・スライド・トランジションを作成する必要がある場合に便利です。
| ||||
| watchOverflow | boolean | false | 有効にすると、Swiperは無効になり、スライドに十分な数のスライドがない場合、ナビゲーションボタンを隠します。
| ||||
| watchSlidesProgress | boolean | false | この機能を有効にすると、各スライドの進捗状況が計算されます。
| ||||
| watchSlidesVisibility | boolean | false |
| ||||
| width | number | null | null | スワイパーの幅(単位:px)。 パラメータは Swiper の幅を強制的に変更します。Swiperが隠されているときにSwiperを初期化する場合や、SSRおよびTest環境でSwiperを正しく初期化する場合にのみ有用です。
| ||||
| wrapperClass | string | 'swiper-wrapper' | スライドのラッパーのCSSクラス名。 Not supported in Swiper Angular/React/Svelte/Vue | ||||
Methods & Properties
スライダーを初期化すると、初期化されたインスタンスが変数(上記の例ではswiper変数)に格納され、便利なメソッドやプロパティが用意されます。:
| Properties | ||||
|---|---|---|---|---|
| swiper.$el | Dom7Array | スライダーコンテナのHTML要素を持つDom7要素。バニラのHTML要素を取得するにはswiper.elを使用します。
| ||
| swiper.$wrapperEl | Dom7Array | スライダーのラッパー HTML 要素を持つ Dom7 要素。バニラHTML要素を取得するにはswiper.wrapperElを使用します。
| ||
| swiper.activeIndex | number | 現在アクティブなスライドのインデックス番号 ループモードでは、ループ/複製されたスライドの数だけ、アクティブなインデックスの値が常にシフトすることに注意してください。
| ||
| swiper.allowSlideNext | boolean | このプロパティにfalse/trueを割り当てることで、次のスライドへのスライド機能を無効/有効にすることができます。 | ||
| swiper.allowSlidePrev | boolean | このプロパティにfalse/trueを割り当てることで、前のスライドへのスライド機能を無効/有効にすることができます。 | ||
| swiper.allowTouchMove | boolean | このプロパティに false / true を設定することで、マウスで掴んだり、指で触れたりしたときにスライダーを動かす機能を無効にしたり、有効にしたりすることができます。 | ||
| swiper.animating | boolean | スイパーが移行中(スライド中)の場合はtrue
| ||
| swiper.clickedIndex | number | 最後にクリックされたスライドのインデックス番号 | ||
| swiper.clickedSlide | HTMLElement | 最後にクリックされたスライドへのリンク (HTMLElement)
| ||
| swiper.el | HTMLElement | スライダーコンテナのHTML要素 | ||
| swiper.height | number | コンテナの height | ||
| swiper.isBeginning | boolean | スライダーが最も "左 "または "上 "にある場合はtrue
| ||
| swiper.isEnd | boolean | スライダーが最も "右 "または "下 "にある場合はtrue | ||
| swiper.params | SwiperOptions | 初期化されたパラメータが渡されたオブジェクト
| ||
| swiper.previousIndex | number | 前にアクティブだったスライドのインデックス番号
| ||
| swiper.progress | number | 現在のwrapper translateの進捗状況(0から1)
| ||
| swiper.realIndex | number | ループモードで重複したスライドを考慮して、現在アクティブなスライドのインデックス番号
| ||
| swiper.slides | Dom7Array | Dom7のスライドHTML要素の配列のようなコレクション。特定のスライドのHTML要素を取得するには、swiper.slides[1]を使用します。
| ||
| swiper.touches | object | 以下のタッチイベントのプロパティを持つオブジェクトです。
| ||
| swiper.translate | number | ラッパー・トランスレートの現在の値
| ||
| swiper.width | number | コンテナの横幅 | ||
| swiper.wrapperEl | HTMLElement | WrapperのHTML element | ||
| swiper.defaults | SwiperOptions | Swiper default options | ||
| swiper.extendedDefaults | SwiperOptions | Object with global Swiper extended options | ||
| Methods | ||||
| swiper.addSlide(index, slides) | 必要なインデックスに新しいスライドを追加します。スライドは、新しいスライドを含む HTML 要素または HTML 文字列、またはそのようなスライドを含む配列になります。:
addSlide(1, '<div class="swiper-slide">Slide 10"</div>')
addSlide(1, [
'<div class="swiper-slide">Slide 10"</div>',
'<div class="swiper-slide">Slide 11"</div>'
]);
| |||
| swiper.appendSlide(slides) | 新しいスライドを最後に追加します。スライドは、例えば、新しいスライドを含むHTML要素やHTML文字列、またはそのようなスライドを含む配列になります: appendSlide('<div class="swiper-slide">Slide 10"</div>')
appendSlide([
'<div class="swiper-slide">Slide 10"</div>',
'<div class="swiper-slide">Slide 11"</div>'
]);
| |||
| swiper.attachEvents() | Attach all events listeners again
| |||
| swiper.changeDirection(direction, needUpdate) | スライダーの向きを水平から垂直に、そして元に戻す.
| |||
| swiper.destroy(deleteInstance, cleanStyles) | スライダーのインスタンスを破棄し、すべてのイベントリスナーを切り離す
| |||
| swiper.detachEvents() | すべてのイベントリスナーが働かなくなる。 | |||
| swiper.disable() | Swiperを無効にします(有効になっていた場合)。Swiperを無効にすると、すべてのナビゲーション要素が非表示になり、すべてのイベントや相互作用に反応しなくなります | |||
| swiper.emit(event, args) | Fire event on instance | |||
| swiper.enabled() | スワイパーの有効化(無効になっていた場合) | |||
| swiper.getBreakpoint(breakpoints) | ||||
| swiper.getTranslate() | swiper wrapper css3 transform translateの現在の値を取得します。
| |||
| swiper.init() | Initialize slider | |||
| swiper.off(event, handler) | Remove event handler
| |||
| swiper.offAny(handler) | すべてのイベントで発生するイベントリスナーの削除 | |||
| swiper.on(event, handler) | イベントハンドラの追加
| |||
| swiper.onAny(handler) | Add event listener that will be fired on all events | |||
| swiper.once(event, handler) | Add event handler that will be removed after it was fired
| |||
| swiper.prependSlide(slides) | 新しいスライドを冒頭に追加します。スライドは、新しいスライドを含むHTML要素またはHTML文字列、またはそのようなスライドを含む配列になります。: prependSlide('<div class="swiper-slide">Slide 0"</div>')
prependSlide([
'<div class="swiper-slide">Slide 1"</div>',
'<div class="swiper-slide">Slide 2"</div>'
]); | |||
| swiper.removeAllSlides() | Remove all slides | |||
| swiper.removeSlide(slideIndex) | 選択されたスライドを削除します。slideIndexには、削除するスライドのインデックスを指定した数値か、インデックスを指定した配列を指定します。. removeSlide(0); // remove first slide
removeSlide([0, 1]); // remove first and second slides
removeAllSlides(); // Remove all slides
| |||
| swiper.setGrabCursor() | カーソルのグラブを有効にする。 | |||
| swiper.setProgress(progress, speed) | スワイパーの移動量を設定します(0~1)。
| |||
| swiper.setTranslate(translate) | swiper wrapperのカスタムcss3トランスフォームのtranslate値を設定する
| |||
| swiper.slideNext(speed, runCallbacks) | 次のスライドに移行します
| |||
| swiper.slidePrev(speed, runCallbacks) | 前のスライドへのトランジションを実行します。
| |||
| swiper.slideReset(speed, runCallbacks) | スワイパーの位置を現在のアクティブなスライドにリセットし、「speed」パラメータと同じ時間が続く.
| |||
| swiper.slideTo(index, speed, runCallbacks) | 「index」パラメータに設定されたインデックス番号のスライドに、「speed」パラメータに設定された時間で、トランジションを実行します。
| |||
| swiper.slideToClosest(speed, runCallbacks) | 「speed」パラメータと同じ時間だけ、スワイパーの位置を最も近いスライド/スナップポイントにリセットします。
| |||
| swiper.slideToLoop(index, speed, runCallbacks) | .slideTo( )と同じことをしますが、有効化されたループと一緒に使われた場合のことです。つまり、このメソッドは、渡されたインデックスに一致するrealIndexを持つスライドにスライドします。
| |||
| swiper.translateTo(translate, speed, runCallbacks, translateBounds) | スイパラッパーのカスタムcss3トランスフォームのtranslate値をアニメーション化する
| |||
| swiper.unsetGrabCursor() | グラブカーソルの解除 | |||
| swiper.update() | スライドを手動で追加/削除したり、非表示/表示にしたり、SwiperでカスタムのDOM変更を行った後に呼び出す必要があります。 このメソッドには、個別に使用できる以下のメソッドのサブコールも含まれています。:
| |||
| swiper.updateAutoHeight(speed) | autoHeightが有効な場合、'speed'パラメータと同じ時間だけ、スワイパーの高さを強制的に更新します。
| |||
| swiper.updateProgress() | スワイパーの進捗状況を再計算する | |||
| swiper.updateSize() | スワイパーコンテナのサイズを再計算 | |||
| swiper.updateSlides() | スライドの枚数とオフセットを再計算します。JavaScriptでスライドを追加・削除した後に便利です。 | |||
| swiper.updateSlidesClasses() | スライドやbulletsでアクティブ/プレヴュー/ネクストのクラスを更新する | |||
| swiper.extendDefaults(options) | Extend global Swiper defaults | |||
| swiper.use(modules) | Installs modules on Swiper in runtime | |||
Events
Swiperには、便利なイベントがたくさん付いています。イベントは2つの方法で割り当てることができます。
- Using
onparameter on swiper initialization:
const swiper = new Swiper('.swiper-container', {
// ...
on: {
init: function () {
console.log('swiper initialized');
},
},
});- Using
onmethod after swiper initialization.
const swiper = new Swiper('.swiper-container', {
// ...
});
swiper.on('slideChange', function () {
console.log('slide changed');
});| Name | Arguments | Description | ||
|---|---|---|---|---|
| activeIndexChange | (swiper) | アクティブなインデックスの変更時にイベントが発生 | ||
| afterInit | (swiper) | 初期化直後にイベントが発生 | ||
| beforeDestroy | (swiper) | Swiperが破壊される直前にイベントが発生。 | ||
| beforeInit | (swiper) | 初期化の直前にイベントが発生 | ||
| beforeLoopFix | (swiper) | イベントは "ループフィックス "の直前に発生します。
| ||
| beforeResize | (swiper) | リサイズハンドラの前にイベントが発生する
| ||
| beforeSlideChangeStart | (swiper) | スライドチェンジが始まる前に発生するイベント | ||
| beforeTransitionStart | (swiper, speed, internal) | トランジション開始前にイベントが発生
| ||
| breakpoint | (swiper, breakpoint Params) | ブレークポイントの変更時にイベントが発生する | ||
| changeDirection | (swiper) | 方向転換時(水平→垂直など)にイベントが発生する | ||
| click | (swiper, event) | ユーザーがSwiperをクリック/タップするとイベントが発生します。引数としてtouchendイベントを受け取ります。 | ||
| destroy | (swiper) | スワイパーをdestroyしたときに実行される | ||
| doubleClick | (swiper, event) | イベントは、ユーザーがSwiperをダブルクリック/タップしたときに発生します。 | ||
| doubleTap | (swiper, event) | ユーザーがSwiperのコンテナをダブルタップするとイベントが発生します。引数としてtouchendイベントを受け取る | ||
| fromEdge | (swiper) | イベントは、Swiperが開始位置または終了位置から移動したときに発生します。
| ||
| imagesReady | (swiper) | イベントはすべてのインナーイメージが読み込まれた直後に発生します。 updateOnImagesReadyも有効にしてください。
| ||
| init | (swiper) | スワイパーの初期化後、すぐに発火する。
const swiper = new Swiper('.swiper-container', {
init: false,
// other parameters
});
swiper.on('init', function() {
// do something
});
// init Swiper
swiper.init();// Otherwise use it as the parameter:
const swiper = new Swiper('.swiper-container', {
// other parameters
on: {
init: function () {
// do something
},
}
});
| ||
| loopFix | (swiper) | イベントは "ループフィックス "後に発生します。
| ||
| momentumBounce | (swiper) | 勢いよく跳ねた時(一番最後のスライドに行った時にバウンスする感じのアニメーションが起きる)にイベントが発生する。 freeModeMomentumBounce: trueのとき | ||
| observerUpdate | (swiper) | オブザーバーが有効になっていて、DOMの変異を検出するとイベントが発生します。
| ||
| orientationchange | (swiper) | 向きが変わったときにイベントが発生する(例:横向き→縦向き) | ||
| progress | (swiper, progress) | イベントはSwiperの進捗状況が変更されたときに発生します。引数として、常に0から1の進捗状況を受け取ります。
| ||
| reachBeginning | (swiper) | イベントはSwiperが最初(初期位置)に到達したときに発生します。 | ||
| reachEnd | (swiper) | イベントは、Swiperが最後のスライドに到達したときに発生します。 | ||
| realIndexChange | (swiper) | 現在表示されてるスライドのインデックスの変化でイベントが発生する | ||
| resize | (swiper) | イベントはウィンドウのリサイズ時に、swiperのonresize操作の直前に発生します。
| ||
| setTransition | (swiper, transition) | イベントは、スワイパーがアニメーションを開始するたびに発生します。現在の遷移時間(ms)を引数として受け取ります。 | ||
| setTranslate | (swiper, translate) | イベントは、スイーパーのラッパーの位置が変わったときに発生します。現在の翻訳値を引数として受け取ります。
| ||
| slideChange | (swiper) | 現在のアクティブなスライドが変更されるとイベントが発生します。
| ||
| slideChangeTransitionEnd | (swiper) | イベントは、他のスライド(次または前)へのアニメーションの後に発生します。
| ||
| slideChangeTransitionStart | (swiper) | イベントは、他のスライド(次または前)へのアニメーションの開始時に発生します。
| ||
| slideNextTransitionEnd | (swiper) | slideChangeTransitionEnd "と同じですが、"forward "方向のみです。
| ||
| slideNextTransitionStart | (swiper) | Same as "slideChangeTransitionStart" but for "forward" direction only
| ||
| slidePrevTransitionEnd | (swiper) | slideChangeTransitionEnd "と同じですが、"backward "方向のみです。
| ||
| slidePrevTransitionStart | (swiper) | slideChangeTransitionStart "と同じですが、"backward "方向のみです。 | ||
| slideResetTransitionEnd | (swiper) | スライドを現在アクティブになっているスライドに位置をリセットするアニメーションの最後にイベントが発生する | ||
| slideResetTransitionStart | (swiper) | イベントは、スライドを現在アクティブになっているスライドに位置をリセットするアニメーションの開始時に発生します。 | ||
| sliderFirstMove | (swiper, event) | イベントは最初のタッチ/ドラッグの動きで実行されます。 | ||
| sliderMove | (swiper, event) | イベントは、ユーザーがSwiperにタッチして指を動かしたときに発生します。touchmoveイベントを引数として受け取ります。 | ||
| slidesGridLengthChange | (swiper) | スライドグリッド(の数など?)が変更されたときに発生するイベント (おそらく)Slides Grid=spaceBetween, slidesPerView, slidesPerColumn, slidesPerColumnFill, slidesPerGroup, slidesPerGroupSkip, centeredSlides, centeredSlidesBounds, slidesOffsetBefore, slidesOffsetAfter, normalizeSlideIndex, centerInsufficientSlides | ||
| slidesLengthChange | (swiper) | スライドの枚数が変更されるとイベントが発生する | ||
| snapGridLengthChange | (swiper) | スナップグリッドが変更されるとイベントが発生する | ||
| snapIndexChange | (swiper) | スナップインデックスの変更時に発生するイベント | ||
| tap | (swiper, event) | ユーザーがSwiperをクリック/タップするとイベントが発生します。引数としてtouchendイベントを受け取ります。 | ||
| toEdge | (swiper) | イベントはSwiperが開始位置または終了位置に行ったときに発生します。
| ||
| touchEnd | (swiper, event) | ユーザーがSwiperを離すとイベントが発生します。引数としてtouchendイベントを受け取ります。
| ||
| touchMove | (swiper, event) | ユーザーがSwiperにタッチして指を動かしたときにイベントが発生します。touchmoveイベントを引数として受け取ります。
| ||
| touchMoveOpposite | (swiper, event) | イベントは、ユーザーがSwiperにタッチして指を動かしたときに、directionパラメータと反対の方向に発射されます。touchmoveイベントを引数として受け取ります。
| ||
| touchStart | (swiper, event) | イベントはユーザーがSwiperにタッチしたときに発生します。引数としてtouchstartイベントを受け取ります。
| ||
| transitionEnd | (swiper) | 遷移後にイベントが発生します。
| ||
| transitionStart | (swiper) | イベントはトランジションの開始時に発生します。
| ||
| update | (swiper) | イベントはswiper.update()の呼び出し後に発生します。 |
Modules
Navigation
Navigation Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| navigation | NavigationOptions | boolean | Object with navigation parameters const swiper = new Swiper('.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
| |
| { //navigation: {}の中身 | |||
| disabledClass | string | 'swiper-button-disabled' | ナビゲーションボタンが無効になった時にCSSクラス名を追加
|
| hiddenClass | string | 'swiper-button-hidden' | ナビゲーションボタンが非表示になったときに、CSSクラス名を追加する
|
| hideOnClick | boolean | false | スライダーのコンテナをクリックした後、ナビゲーションボタンの表示を切り替える
|
| lockClass | string | 'swiper-button-lock' | ナビゲーションボタンが無効の場合にCSSクラス名を追加
|
| nextEl | CSSSelector | HTMLElement | null | null | クリックすると "next "ボタンのように動作する要素のCSSセレクタまたはHTML要素の文字列 |
| prevEl | CSSSelector | HTMLElement | null | null | クリックすると "prev "ボタンのように動作する要素のCSSセレクタまたはHTML要素の文字列 |
| } | |||
Navigation Methods
| Properties | ||
|---|---|---|
| swiper.navigation.nextEl | HTMLElement | HTMLElement of "next" navigation button
|
| swiper.navigation.prevEl | HTMLElement | HTMLElement of "previous" navigation button
|
| Methods | ||
| swiper.navigation.destroy() | Destroy navigation(破棄する) | |
| swiper.navigation.init() | Initialize navigation(初期化する) | |
| swiper.navigation.update() | Update navigation buttons state (enabled/disabled)
| |
Navigation Events
| Name | Arguments | Description |
|---|---|---|
| navigationHide | (swiper) | イベントはナビゲーションの非表示時(hide)に発生します。 |
| navigationShow | (swiper) | イベントはナビゲーション表示時(show)に発生します。 |
Pagination
Pagination Parameters
| Name | Type | Default | Description | ||
|---|---|---|---|---|---|
| pagination | Pagination Options | boolean | Object with navigation parameters const swiper = new Swiper('.swiper-container', {
pagination: {
el: '.swiper-pagination',
type: 'bullets',
},
});
| |||
| { // pagination: {}の中身↓ | |||||
| bulletActiveClass | string | 'swiper-pagination-bullet-active' | 現在アクティブなページネーションのbulletのCSSクラス名 (type:bulletsだけ???)←試してみる
| ||
| bulletClass | string | 'swiper-pagination-bullet' | CSS class name of single pagination bullet
| ||
| bulletElement | string | 'span' | ページネーションの箇条書きを表現するためのHTMLタグを定義します。bulletsページネーションタイプのみ。
| ||
| clickable | boolean | false | trueの場合、ページネーションボタンをクリックすると、該当するスライドに遷移します。箇条書きのページネーションの場合のみ
| ||
| clickableClass | string | 'swiper-pagination-clickable' | clickableをtrueにしたときにつくクラス名。 | ||
| currentClass | string | 'swiper-pagination-current' | type: 'fraction' のページネーションにおいて、現在アクティブなインデックスを持つ要素のCSSクラス名
| ||
| dynamicBullets | boolean | false | 多くのスライドで箇条書きのページネーションを使用している場合に有効です。これにより、同時にいくつかの箇条書きを表示することができます。
| ||
| dynamicMainBullets | number | 1 | dynamicBulletsが有効な場合に表示されるメインの弾丸の数です。
| ||
| el | CSSSelector | HTML null | null | ページネーションを行うコンテナのCSSセレクタまたはHTML要素の文字列 | ||
| formatFractionCurrent | function( number) | 端数処理のページ番号をフォーマットします。関数は現在の番号を受け取り、フォーマットされた値を返す必要があります。
| |||
| formatFractionTotal | function (number) | 小数点以下のページ数の合計値をフォーマットします。総数を受け取る関数では、フォーマットされた値を返す必要があります。
| |||
| hiddenClass | string | 'swiper-pagination-hidden' | ページネーションが非アクティブになったときのCSSクラス名
| ||
| hideOnClick | boolean | true | スライダーのコンテナをクリックした後、ページネーションのコンテナの表示/非表示を切り替える。
| ||
| lockClass | string | 'swiper-pagination-lock' | ページネーションが無効になっているときに設定されるCSSクラス名
| ||
| modifierClass | string | 'swiper-pagination-' | パラメーターに応じてページネーションに追加されるモディファイアのCSSクラス名の最初の部分
| ||
| progressbar FillClass | string | 'swiper-pagination-progressbar-fill' | ページネーションのprogressbarの塗りつぶし(fill)要素のCSSクラス名
| ||
| progressbar Opposite | boolean | false | ページ処理のプログレスバーを、Swiperのdirectionパラメータと反対にします。つまり、水平方向のSwiperには垂直プログレスバーを、垂直方向のSwiperには水平プログレスバーを表示します。
| ||
| progressbar OppositeClass | string | 'swiper-pagination-progressbar-opposite' |
| ||
| renderBullet | function( index, | null | このパラメータは、ページネーションのbulletsを完全にカスタマイズすることができます。ページネーションの弾丸のindex番号と必須要素のクラス名(className)を受け取る関数をここに渡す必要があります。bulletsのページネーションタイプのみ
const swiper = new Swiper('.swiper-container', {
//...
renderBullet: function (index, className) {
return '<span class="' + className + '">' + (index + 1) + '</span>';
}
}); | ||
renderCustom | function( swiper, current, total) | null | このパラメータは、
const swiper = new Swiper('.swiper-container', {
//...
renderCustom: function (swiper, current, total) {
return current + ' of ' + total;
}
});
| ||
| renderFraction | function( currentClass, totalClass) | null | このパラメータは、"fraction" pagination htmlをカスタマイズすることができます。fraction pagination タイプのみ const swiper = new Swiper('.swiper-container', {
//...
renderFraction: function (currentClass, totalClass) {
return '<span class="' + currentClass + '"></span>' +
' of ' +
'<span class="' + totalClass + '"></span>';
}
}); | ||
| renderProgressbar | function( progressbar FillClass) | null | このパラメータでは、「progress」ページネーションをカスタマイズできます。progress ページネーションの場合のみ const swiper = new Swiper('.swiper-container', {
//...
renderProgressbar: function (progressbarFillClass) {
return '<span class="' + progressbarFillClass + '"></span>';
}
});
| ||
| totalClass | string | 'swiper-pagination-total' | 端数 "のページネーションで "スナップ "の総数を持つ要素のCSSクラス名
| ||
| type | 'bullets' | 'fraction' | 'progress bar' | 'custom' | 'bullets' | ページネーションのタイプを示す文字列。"bullets"、"fraction"、"progressbar"、"custom "のいずれか。
| ||
| } | |||||
Pagination Methods
| Properties | ||
|---|---|---|
| swiper.pagination.bullets | Dom7のpagination bullets HTML要素の配列のようなコレクション。特定のスライドのHTML要素を取得するには、
| |
| swiper.pagination.el | HTMLElement | ページネーションコンテナ要素のHTMLElement
|
| Methods | ||
| swiper.pagination.destroy() | Destroy pagination(破棄する) | |
| swiper.pagination.init() | Initialize pagination(初期化する) | |
| swiper.pagination.render() | Render pagination layout(レンダリング)
| |
| swiper.pagination.update() | Update pagination state (enabled/disabled/active)
| |
Pagination Events
| Name | Arguments | Description |
|---|---|---|
| paginationHide | (swiper) | Event will be fired on pagination hide(ページネーション非表示時) |
| paginationRender | (swiper, paginationEl) | Event will be fired after pagination rendered(レンダリング後) |
| paginationShow | (swiper) | Event will be fired on pagination show(ページネーション表示後) |
| paginationUpdate | (swiper, paginationEl) | Event will be fired when pagination update(ページネーション更新時) |
Scrollbar
Scrollbar Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| scrollbar | ScrollbarOptions | boolean | Object with scrollbar parameters const swiper = new Swiper('.swiper-container', {
scrollbar: {
el: '.swiper-scrollbar',
draggable: true,
},
});
| |
| { | |||
| dragClass | string | 'swiper-scrollbar-drag' | Scrollbar draggable element CSS class
|
| dragSize | 'auto' | number | 'auto' | スクロールバーのドラッグ可能な要素のサイズ(px)
|
| draggable | boolean | true | スライダーの位置を制御できるようにスクロールバーをドラッグ可能にするには、trueを設定します。
|
| el | CSSSelector | HTMLElement | null | null | スクロールバーを持つコンテナのCSSセレクタまたはHTML要素の文字列。 |
| hide | boolean | true | ユーザーの操作で自動的にスクロールバーを隠す
|
| lockClass | string | 'swiper-scrollbar-lock' | スクロールバー要素が無効の場合の追加CSSクラス
|
| snapOnRelease | boolean | false | スクロールバーを離したときにスライダーの位置をスライドに合わせるには、trueを設定します。
|
| } | |||
Scrollbar Methods
| Properties | ||
|---|---|---|
| swiper.scrollbar.dragEl | HTMLElement | ドラッグする黒いやつのHTML要素を指定できる。 |
| swiper.scrollbar.el | HTMLElement | スクロールバー全体のHTML要素を指定できる。 |
| Methods | ||
| swiper.scrollbar.destroy() | Destroy scrollbar(破棄する) | |
| swiper.scrollbar.init() | Initialize scrollbar(初期化する) | |
| swiper.scrollbar.setTranslate() | Updates scrollbar translate | |
| swiper.scrollbar.updateSize() | Updates scrollbar track and handler sizes | |
Scrollbar Events
| Name | Arguments | Description |
|---|---|---|
| scrollbarDragEnd | (swiper, event) | Event will be fired on draggable scrollbar drag end イベントは、ドラッグ可能なスクロールバーのドラッグ終了時に発生します。 |
| scrollbarDragMove | (swiper, event) | Event will be fired on draggable scrollbar drag move イベントは、ドラッグ可能なスクロールバーのドラッグ移動時に発生します。 |
| scrollbarDragStart | (swiper, event) | Event will be fired on draggable scrollbar drag start イベントは、ドラッグ可能なスクロールバーのドラッグ開始時に発生します。 |
Autoplay (自動再生)
Autoplay Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| autoplay | AutoplayOptions | boolean | Object with autoplay parameters or boolean const swiper = new Swiper('.swiper-container', {
autoplay: {
delay: 5000,
},
}); | |
| { // autoplay: {} の中身 | |||
| delay | number | 3000 | トランジション間の遅延時間(単位:ms)。このパラメータが指定されていない場合、自動再生は無効になります。 特定のスライドに対して異なる遅延時間を指定する必要がある場合は、スライドに
<!-- このスライドだけ遅延時間を2秒にする --> <div class="swiper-slide" data-swiper-autoplay="2000"> |
| disableOnInteraction | boolean | true | falseに設定すると、ユーザーの操作(スワイプ)があっても自動再生が無効にならず、操作後は毎回再起動されます。
|
| pauseOnMouseEnter | boolean | false | 自動再生を有効にすると、マウスがSwiperコンテナ上に入ると自動再生が一時停止します。 |
| reverseDirection | boolean | false | 逆方向のオートプレイを可能にする
|
| stopOnLastSlide | boolean | false | このパラメータを有効にすると、自動再生が最後のスライドに達した時点で停止します(ループモードでは効果がありません)。 |
| waitForTransition | boolean | true | 自動再生を有効にすると、ラッパーのトランジションを待って再生を継続します。virtualTranslateを使用している場合、スライダーにトランジションがない場合は、無効にすることができます。
|
| } | |||
Autoplay Methods
| Properties | ||
|---|---|---|
| swiper.autoplay.running | boolean | 自動再生が有効かどうか。 |
| Methods | ||
| swiper.autoplay.start() | 自動再生を開始する。 | |
| swiper.autoplay.stop() | 自動再生を停止する。 | |
Autoplay Events
| Name | Arguments | Description |
|---|---|---|
| autoplay | (swiper) | 自動再生によってスライドが移動したら実行。 |
| autoplayStart | (swiper) | 自動再生が開始されると実行。 |
| autoplayStop | (swiper) | 自動再生が停止されると実行。 |
Parallax(視差効果)
パララックス(parallax)は英語で「視差」という意味があります。ざっくりというと「部分ごとに動くスピードや、動く方向に差をつける」というイメージです。Webデザインでは、スクロール効果としてパララックスがよく取り入れられます。

Parallax scrolling header-CodePen
緑の部分と、背後の黒の部分で動くスピードが違うことが分かるかと思います。このように差をつけることで、奥行き感を出したり、目を引くようなデザインにすることができる…というわけです。
Swiperは、swiper/slidesのネストされた要素のパララックス遷移効果をサポートしています。サポートされているパララックス要素のタイプは2つあります。
swiper-containerの直接の子要素です。このような要素の視差効果は、全体のスライダーの進行状況に依存します。視差のある背景に便利- スライドの子要素。このような要素の視差効果は、スライドの進行状況に依存します。
視差効果を有効にするには、parallax:trueパラメータを渡してSwiperを初期化し、必要な要素に以下のいずれか(または混合)の属性を追加する必要があります:
data-swiper-parallax- transform-translate parallax transition(視差効果のあるパララックス遷移?)を可能にします. この属性は以下の値を指定できる:number- 進行状況に応じて要素を移動させるために、px単位の値(上記の例ではタイトル、サブタイトルのように)を指定します。この場合、この要素は、スライドの位置(次または前)に応じて±pxの値で移動します。percentage- (parallax-bgと同様)を使用すると、進行状況やサイズに応じて要素が移動します。この場合、要素は、スライドの位置(次または前)に応じて、そのサイズ(水平方向の幅と垂直方向の高さ)の ±割合で移動されます。つまり、要素の幅が400pxで、data-swiper-parallax="50%"を指定した場合、要素は±200pxの範囲で移動します。
data-swiper-parallax-x- data-swiper-parallaxと同じだが、X軸だけ違うdata-swiper-parallax-y- Y軸が違うdata-swiper-parallax-scale-視差要素が "inactive"(アクティブなスライド上にない)状態にあるときの、視差要素のスケール比。data-swiper-parallax-opacity- パララックス要素が "inactive"(アクティブなスライド上にない)状態にあるときの不透明度data-swiper-parallax-duration-パララックス要素のカスタムトランジションデュレーション
data-swiper-parallaxはスライドした時に移動する距離を設定。数、もしくはパーセントで設定。
data-swiper-parallax-x、data-swiper-parallax-yで縦軸横軸を設定。
data-swiper-parallax-scaleでアクティブじゃないスライドの大きさを指定。
data-swiper-parallax-opacityでアクティブじゃないスライドの透明度を指定。
data-swiper-parallax-durationで推移時間をカスタムで指定。
<div class="swiper-container">
<!-- Parallax background element -->
<div
class="parallax-bg"
style="background-image:url(path/to/image.jpg)"
data-swiper-parallax="-23%"
></div>
<div class="swiper-wrapper">
<div class="swiper-slide">
<!-- Each slide has parallax title -->
<div class="title" data-swiper-parallax="-100">Slide 1</div>
<!-- Parallax subtitle -->
<div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
<!-- And parallax text with custom transition duration -->
<div
class="text"
data-swiper-parallax="-300"
data-swiper-parallax-duration="600"
>
<p>Lorem ipsum dolor sit amet, ...</p>
</div>
<!-- Opacity parallax -->
<div data-swiper-parallax-opacity="0.5">I will change opacity</div>
<!-- Scale parallax -->
<div data-swiper-parallax-scale="0.15">I will change scale</div>
</div>
...
</div>
</div>Parallax Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| parallax | ParallaxOptions | boolean | Object with parallax parameters const swiper = new Swiper('.swiper-container', {
parallax: true,
}); | |
| { | |||
| enabled | boolean | false | Enable, if you want to use "parallaxed" elements inside of slider |
| } | |||
Lazy Loading (遅延読み込み)
遅延読み込みを可能にするためには、まず、スライドで背景を持つ画像や要素のための特別なレイアウトが必要になります。
遅延ロードする画像には.swiper-lazyを設定。かつ、srcではなくdata-srcで設定。srcsetではなく、data-srcsetで設定。
スライドの背景画像はdata-backgroundで設定。
各スライドの後ろにdiv.swiper-lazy-preloaderを設置(白いプリロードにしたいならswiper-lazy-preloader-whiteを追加)。
かつ、オプションに2つを追加
preloadImages: false,
lazy: trueレイジーロードのオプションを使いたいなら下の書き方。
preloadImages: false,
lazy: {
loadPrevNext: true
}lazy:{}の中に遅延読み込みのオプションを書いてく。
<div class="swiper-container">
<div class="swiper-wrapper">
<!-- Lazy image -->
<div class="swiper-slide">
<img data-src="path/to/picture-1.jpg" class="swiper-lazy" />
<div class="swiper-lazy-preloader"></div>
</div>
<!-- Lazy image with srscet-->
<div class="swiper-slide">
<img
data-src="path/to/logo-small.png"
data-srcset="path/logo/logo-large.png 2x"
class="swiper-lazy"
/>
<div class="swiper-lazy-preloader"></div>
</div>
<!-- Element with lazy background image -->
<div class="swiper-slide">
<div data-background="path/to/picture-2.jpg" class="swiper-lazy">
<div class="swiper-lazy-preloader"></div>
</div>
</div>
<!-- Lazy background image on slide itself -->
<div
data-background="path/to/picture-3.jpg"
class="swiper-slide swiper-lazy"
>
<div class="swiper-lazy-preloader"></div>
</div>
</div>
</div>
以下を御覧ください:
- 遅延読み込みされた画像/要素は、それぞれ "swiper-lazy "クラスを追加する必要があります。
- 要素のレイジーイメージソースは、"src "ではなく "data-src "属性で指定する必要があります。
- 要素のレイジーイメージのソースセットは、"srcset "ではなく "data-srcset "属性で指定する必要があります。
- data-background "属性にレイジーバックグラウンドの画像ソースを指定する必要があります。
また、画像が読み込まれた後に自動的に削除されるアニメーションのプリローダースピナーをスライドに追加することもできます。:
<div class="swiper-lazy-preloader"></div>Or 暗いレイアウトには白いものを:
<div class="swiper-lazy-preloader swiper-lazy-preloader-white"></div>その後、Swiperの初期化時にレイジーローディングを有効にする必要があります :
const swiper = new Swiper('.swiper-container', {
// Disable preloading of all images
preloadImages: false,
// Enable lazy loading
lazy: true,
});もしslidesPerView: auto'またはslidesPerView > 1を使用している場合は、watchSlidesVisibilityも有効にする必要があり、Swiperは現在表示されているスライドで画像を読み込みます。
Lazy Loading Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| lazy | LazyOptions | boolean | 画像の遅延読み込みを有効にします。遅延読み込みのパラメータを持つオブジェクト、またはデフォルト設定で有効にするための真偽値 const swiper = new Swiper('.swiper-container', {
lazy: {
loadPrevNext: true,
},
});
| |
| { | |||
| checkInView | boolean | false | 最初のスライドで画像を遅延ロードする前に、スイーパーが表示されているかどうかを確認することができます。 |
| elementClass | string | 'swiper-lazy' | 遅延する要素のCSSクラス名 |
| loadOnTransitionStart | boolean | false | デフォルトでは、Swiperはこのスライドに移行した後に遅延画像をロードしますので、移行の最初に新しい画像のロードを開始する必要がある場合は、このパラメータを有効にすることができます。
|
| loadPrevNext | boolean | false | trueに設定すると、最も近いスライド画像の遅延読み込みが有効になります(前と次のスライド画像の場合)。
|
| loadPrevNextAmount | number | 1 | 遅延画像をプリロードする次/前のスライドの量。
|
| loadedClass | string | 'swiper-lazy-loaded' | 遅延読み込が完了した要素のCSSクラス名
|
| loadingClass | string | 'swiper-lazy-loading' | 遅延読み込み中の要素のCSSクラス名
|
| preloaderClass | string | 'swiper-lazy-preloader' | 遅延プリローダのCSSクラス名
|
| scrollingElement | CSSSelector | null | Dom7Array | HTMLElement |
| |
| } | |||
Lazy Loading Methods
| Methods | ||
|---|---|---|
| swiper.lazy.load() | 現在のスライダーの状態(位置)に応じた遅延画像のロード/アップデイト
| |
| swiper.lazy.loadInSlide(index) | スライドの遅延画像を指定したインデックスで強制的に読み込む
| |
Lazy Loading Events
| Name | Arguments | Description |
|---|---|---|
| lazyImageLoad | (swiper, slideEl, imageEl) | Event will be fired in the beginning of lazy loading of image
|
| lazyImageReady | (swiper, slideEl, imageEl) | Event will be fired when lazy loading image will be loaded
|
Fade Effect (フェード効果)
スライドが横(縦)方向にずれるスライドではなく、フェードインでのスライドになる
この機能を利用するには、effectパラメータが 'fade' に設定されている必要があります。後ろや下にコンテンツが表示されないようにするには、crossFade をtrueに設定する必要があります。
Fade Effect Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| fadeEffect | FadeEffectOptions | Object with Fade-effect parameters const swiper = new Swiper('.swiper-container', {
effect: 'fade',
fadeEffect: {
crossFade: true
},
});
| |
| { | |||
| crossFade | boolean | false | Enables slides cross fade
|
| } | |||
Coverflow Effect
立体的なやつが横に並ぶやつ。
この機能を有効にするには、 effectパラメータを 'coverflow'に設定する必要があります。
Coverflow Effect Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| coverflowEffect | CoverflowEffectOptions | Object with Coverflow-effect parameters. const swiper = new Swiper('.swiper-container', {
coverflowEffect: {
rotate: 30,
slideShadows: false,
},
});
| |
| { | |||
| depth | number | 100 | Depth offset in px (slides translate in Z axis)
|
| modifier | number | 1 | Effect multiplier
|
| rotate | number | 50 | Slide rotate in degrees
|
| slideShadows | boolean | true | Enables slides shadows
|
| stretch | number | 0 | Stretch space between slides (in px)
|
| } | |||
Flip Effect
くるって裏返したら次のスライドになるやつ。カードゲームのオープニングにあるやつって言えばなんとなく伝わりそう。
この機能を利用するには、 effectパラメータを「フリップ」に設定する必要があります。
Flip Effect Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| flipEffect | FlipEffectOptions | Object with Flip-effect parameters const swiper = new Swiper('.swiper-container', {
flipEffect: {
slideShadows: false,
},
});
| |
| { | |||
| limitRotation | boolean | true | Limit edge slides rotation
|
| slideShadows | boolean | true | Enables slides shadows
|
| } | |||
Cube Effect
直方体が回転するみたいなスライドになる。極力スライドさせる画像は正方形の方がきれい。
この機能を有効にするには、 effect paramを 'cube' に設定する必要があります。
Cube Effect Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| cubeEffect | CubeEffectOptions | Object with Cube-effect parameters const swiper = new Swiper('.swiper-container', {
cubeEffect: {
slideShadows: false,
},
});
| |
| { | |||
| shadow | boolean | true | Enables main slider shadow
|
| shadowOffset | number | 20 | Main shadow offset in px
|
| shadowScale | number | 0.94 | Main shadow scale ratio
|
| slideShadows | boolean | true | Enables slides shadows
|
| } | |||
Thumbs (サムネイル)
サムネイルを選択したらそこにスライドするというswiperを実現したい時のオプション。
swiperを2種類用意する。
var thumbs = new Swiper('#thumbs',{
spaceBetween: 5,
slidesPerView: 5,
watchSlidesVisibility: true,
})
var gallerySwiper = new Swiper('#view',{ //メインスワイパー
thumbs: {
swiper: thumbs,//このthumbsをサムネイルとして使うよと指定。
}
})thumbs内にオプションを入れていく。
コントローラコンポーネントに加えて、SwiperにはThumbsコンポーネントが付属しています。これは、2つのスワイパーを同期させるために使用されるコントローラよりも、より正しい方法で追加の親指スワイパーと連携するように設計されています。
Thumbs Parameters
| Name | Type | Default | Description | ||
|---|---|---|---|---|---|
| thumbs | ThumbsOptions | Object with thumbs component parameters const swiper = new Swiper('.swiper-container', {
...
thumbs: {
swiper: thumbsSwiper
}
});
| |||
| { | |||||
| autoScrollOffset | number | 0 | どのサムネイルがアクティブになっているか、端からのスライドで自動的にスクロールサムネイルを移動させるかを設定できます。例えば、1に設定し、最後に表示されているサムネイルがアクティブになった場合(端から1番目)、サムネイルを自動でスクロールします。
| ||
| multipleActiveThumbs | boolean | true | 有効にすると、複数のサムネイルスライドが有効になることがあります。
| ||
| slideThumbActiveClass | string | 'swiper-slide-thumb-active' | 有効化したサムネイルのスワイパースライドに追加されるクラス | ||
| swiper | Swiper | null | null | サムネイルとして使用されるSwiperのインスタンス、またはサムネイルのSwiperを初期化するためのSwiperパラメータを持つオブジェクト
| ||
| thumbsContainerClass | string | 'swiper-container-thumbs' | Additional class that will be added to thumbs swiper-container
| ||
| } | |||||
Thumbs Methods
| Properties | ||
|---|---|---|
| swiper.thumbs.swiper | Swiper | Swiper instance of thumbs swiper
|
| Methods | ||
| swiper.thumbs.init() | Initialize thumbs | |
| swiper.thumbs.update(initial) | Update thumb | |
Zoom
ダブルタップ・ダブルクリックでズームしたり、ピンチイン・ピンチアウトでズームしたりできる。
.swiper-zoom-containerで囲むする必要がある。data-swiper-zoomを使えば個別に最大倍率を操作できる。
Swiperは、ピンチジェスチャーで画像を拡大したり、ダブルタップで画像を拡大/縮小したりできるズームイメージ機能をサポートしています(iOSで1枚の写真を閲覧するときに表示されるものと同様です)。この場合、追加のレイアウトが必要です:
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="swiper-zoom-container">
<img src="path/to/image1.jpg" />
</div>
</div>
<div class="swiper-slide">
<div class="swiper-zoom-container">
<img src="path/to/image2.jpg" />
</div>
</div>
<div class="swiper-slide">Plain slide with text</div>
<div class="swiper-slide">
<!-- Override maxRatio parameter -->
<div class="swiper-zoom-container" data-swiper-zoom="5">
<img src="path/to/image1.jpg" />
</div>
</div>
</div>
</div>
- すべての "zoomable "画像は、
swiper-zoom-containerクラスのdivでラップする必要があります。. - デフォルトでは、
img、picture、canvasのいずれかの要素をズームすることになっています。もし、他のカスタム要素でズームさせたい場合は、swiper-zoom-targetクラスをこの要素に追加してください。例↓
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="swiper-zoom-container">
<!-- custom zoomable element -->
<div
class="swiper-zoom-target"
style="background-image: url(...)"
></div>
</div>
</div>
</div>
</div>- ズームコンテナの
data-swiper-zoom属性を使って、特定のスライドのmaxRatioパラメータをオーバーライドすることができます。
Zoom Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| zoom | ZoomOptions | boolean | ズーム機能を有効にします。オブジェクトにズームパラメータを設定するか、デフォルト設定で有効にする場合はtrueを指定します。 const swiper = new Swiper('.swiper-container', {
zoom: {
maxRatio: 5,
},
});
| |
| { | |||
| containerClass | string | 'swiper-zoom-container' | CSS class name of zoom container
|
| maxRatio | number | 3 | Maximum image zoom multiplier
|
| minRatio | number | 1 | Minimal image zoom multiplier
|
| toggle | boolean | true | Enable/disable zoom-in by slide's double tap
|
| zoomedSlideClass | string | 'swiper-slide-zoomed' | CSS class name of zoomed in container
|
| } | |||
Zoom Methods
| Properties | ||
|---|---|---|
| swiper.zoom.enabled | boolean | ズームモジュールが有効であるかどうか
|
| swiper.zoom.scale | number | 現在の画像の拡大率
|
| Methods | ||
| swiper.zoom.disable() | ズームを無効にする。 | |
| swiper.zoom.enable() | ズームを有効にする。 | |
| swiper.zoom.in() | アクティブなスライドを拡大する。 | |
| swiper.zoom.out() | アクティブなスライドを縮小する。 | |
| swiper.zoom.toggle() | アクティブなスライドの拡大・縮小を切り替える。 | |
Zoom Events
| Name | Arguments | Description |
|---|---|---|
| zoomChange | (swiper, scale, imageEl, slideEl) | ズーム変更時に発生するイベント |
Keyboard Control キーボード操作
キーボードでスライダーを操作できるようにする。
Keyboard Control Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| keyboard | KeyboardOptions | boolean | キーボードを使ったスライドのナビゲーションを可能にする。キーボードパラメータを持つオブジェクト、またはデフォルト設定で有効にするための真偽値 const swiper = new Swiper('.swiper-container', {
keyboard: {
enabled: true,
onlyInViewport: false,
},
}); | |
| { | |||
| enabled | boolean | false | キーボード操作を有効にする。 |
| onlyInViewport | boolean | true | ビューポートにあるスライダーのみ適応させる。 |
| pageUpDown | boolean | true | trueにすると、Page UpキーとPage Downキーで操作できるようになる。 |
| } | |||
Keyboard Control Methods
| Properties | ||
|---|---|---|
| swiper.keyboard.enabled | boolean | キーボード操作が有効かどうか取得できる。 |
| Methods | ||
| swiper.keyboard.disable() | キーボード操作を無効にする。 | |
| swiper.keyboard.enable() | キーボード操作を有効にする。 | |
Keyboard Events
| Name | Arguments | Description |
|---|---|---|
| keyPress | (swiper, keyCode) | Event will be fired on key press |
Mousewheel Control マウスホイール操作
マウスホイールでスライダーを操作できるようにする。
Mousewheel Control Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| mousewheel | MousewheelOptions | boolean | マウスホイールを使用して、スライドのナビゲーションを可能にします。マウスホイールのパラメータを持つオブジェクト、またはデフォルトの設定で有効にするためにはtrueのブール値を設定 const swiper = new Swiper('.swiper-container', {
mousewheel: {
invert: true,
},
});
| |
| { | |||
| eventsTarget | 'container' | 'wrapper' | CSSSelector | HTMLElement | 'container' | マウスホイールイベントを受け付けるコンテナのCSSセレクタまたはHTML要素を持つ文字列。デフォルトでは、swiper-containerです。
|
| forceToAxis | boolean | false | trueに設定すると、マウスホイールのスワイプを強制的に軸に合わせます。つまり、水平モードではマウスホイールは水平方向のスクロールでのみ動作し、垂直モードでは垂直方向のスクロールでのみ動作します。
|
| invert | boolean | false | 真を設定するとスライド方向が反転します
|
| releaseOnEdges | boolean | false | trueに設定すると、スイパーが端の位置(最初か最後)にあるときに、マウスホイールイベントを解放し、ページのスクロールを可能にします。
|
| sensitivity | number | 1 | マウスホイールデータの乗数。マウスホイールの感度を調整することができる。
|
| thresholdDelta | number | null | null | スワイパースライド変更のトリガーとなるマウスホイールのスクロールの最小差分 |
| thresholdTime | number | null | null | スワイパースライド変更のトリガーとなるマウスホイールのスクロール時間の最小値(単位:ms) |
| } | |||
Mousewheel Control Methods
| Properties | ||
|---|---|---|
| swiper.mousewheel.enabled | boolean | Whether the mousewheel control is enabled
|
| Methods | ||
| swiper.mousewheel.disable() | Disable mousewheel control | |
| swiper.mousewheel.enable() | Enable mousewheel control | |
Mousewheel Events
| Name | Arguments | Description |
|---|---|---|
| scroll | (swiper, event) | Event will be fired on mousewheel scroll |
Virtual Slides
スライドしたい要素が大量にある(100とか200とか)ある場合、流石に重い。
そうした時に、アクティブなスライドとその前後だけを読み込みDOMに追加し、次へボタンが押されたりしたらその次のスライドの前後だけを読み込みDOMに追加、前のスライドの前後にあったものはDOMから削除するとしたら軽くなる。それをしてくれるオプション。
書くと果てしなく長くなりそうだから省略。
Virtual Slides module allows to keep just required amount of slides in DOM. It is very useful in terms in performance and memory issues if you have a lot of slides, especially slides with heavyweight DOM tree or images.Note that according to Virtual Slides realization it doesn't work with loop mode, slidesPerColumn more than 1 and slidesPerView: 'auto'
Virtual Slidesモジュールは、DOMに必要な量のスライドだけを保持することができます。Virtual Slidesは、ループモード、slidesPerColumnが1以上、slidesPerView: 'auto'では動作しないことに注意してください。
Virtual Slides Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| virtual | VirtualOptions | boolean | バーチャルスライド機能を有効にします。バーチャルスライドのパラメータを持つオブジェクト、またはデフォルトの設定で有効にする場合はtrueのブール値。 const swiper = new Swiper('.swiper-container', {
virtual: {
slides: ['Slide 1', 'Slide 2', 'Slide 3', 'Slide 4', 'Slide 5'],
},
});
| |
| { | |||
| addSlidesAfter | number | 0 | アクティブなスライドの後にプリレンダリングされるスライドの量を増やす |
| addSlidesBefore | number | 0 | アクティブなスライドの前に、プリレンダリングされたスライドの量を増やす |
| cache | boolean | true | スライドのhtml要素をレンダリングする際にDOMキャッシュを有効にします。レンダリングされたスライドのHTML要素はキャッシュに保存され、そこから再利用されます。 |
| renderExternal | function(data) | null | 外部レンダリング用の関数です(React.jsやVue.jsのようにDOM操作や状態を扱う他のライブラリを使用している場合などに使用します)。引数として、以下のプロパティを持つ
|
| renderExternalUpdate | boolean | true | 有効(デフォルト)にすると、renderExternalが呼ばれた直後にSwiperのレイアウトを更新します。非同期レンダリングを行うレンダリングライブラリを使用する場合、Swiperを無効にして手動で更新すると便利です。 |
| renderSlide | function(slide, index) | null | スライドをレンダリングする関数です。引数には、スライド配列の現在のスライド項目と、 現在のスライドのインデックス番号を指定します。この関数は、スワイパースライドの外部HTMLを返さなければなりません。 |
| slides | [] | Array with slides | |
| } | |||
Virtual Slides Methods
| Properties | ||
|---|---|---|
| swiper.virtual.cache | object | Object with cached slides HTML elements |
| swiper.virtual.from | number | Index of first rendered slide |
| swiper.virtual.slides | Array with slide items passed by | |
| swiper.virtual.to | number | Index of last rendered slide |
| Methods | ||
| swiper.virtual.appendSlide(slide) | スライドを追加. | |
| swiper.virtual.prependSlide(slide) | スライドを前置します。slidesは、単一のスライドアイテムまたはそのようなスライドを含む配列です。 | |
| swiper.virtual.removeAllSlides() | Remove all slides | |
| swiper.virtual.removeSlide(slideIndexes) | Remove specific slide or slides. | |
| swiper.virtual.update(force) | Update virtual slides state | |
renderExternal Example
renderExternal はスライドのレンダリングを他のライブラリにバイパスすることができ、React.jsやVue.jsのようなライブラリでは非常に便利です。
With Vue.js
<template>
<!-- ... -->
<div class="swiper-container">
<div class="swiper-wrapper">
<!-- It is important to set "left" style prop on every slide -->
<div
class="swiper-slide"
v-for="(slide, index) in virtualData.slides"
:key="index"
:style="slideStyles"
>
{{slide}}
</div>
</div>
</div>
<!-- ... -->
</template>
<script>
import Swiper from 'swiper/bundle';
export default {
data() {
return {
// dummy slides data
slides: (function () {
var slides = [];
for (var i = 0; i < 600; i += 1) {
slides.push('Slide ' + (i + 1));
}
return slides;
})(),
// virtual data
virtualData: {
slides: [],
},
};
},
computed: {
slideStyles() {
return `left: ${this.virtualData.offset}px;`;
},
},
mounted() {
const self = this;
const swiper = new Swiper('.swiper-container', {
// ...
virtual: {
slides: self.slides,
renderExternal(data) {
// assign virtual slides data
self.virtualData = data;
},
},
});
},
};
</script>
With React.js
import React from 'react';
import Swiper from 'swiper/bundle';
export default class extends React.Component {
constructor() {
this.state = {
// dummy slides data
slides: (function () {
var slides = [];
for (var i = 0; i < 600; i += 1) {
slides.push('Slide ' + (i + 1));
}
return slides;
}()),
// virtual data
virtualData: {
slides: [],
},
}
}
componentDidMount() {
const self = this;
const swiper = new Swiper('.swiper-container', {
// ...
virtual: {
slides: self.state.slides,
renderExternal(data) {
// assign virtual slides data
self.setState({
virtualData: data,
});
}
},
});
}
render() {
{/* ... */}
<div className="swiper-container">
<div className="swiper-wrapper">
{/* It is important to set "left" style prop on every slide */}
{this.state.virtualData.slides.map((slide, index) => (
<div className="swiper-slide"
key={index}
style={{left: `${virtualData.offset}px`}}
>{slide}</div>
))}
</div>
</div>
{/* ... */}
}
}Hash Navigation ハッシュ
各スライドにdata-hash=”slide1″のような感じでdata-hashを設置すると、url#slide1といったurlでdata-hashが一致するところからスライドが始まる。
ハッシュナビゲーションは、特定のスライドへのリンクを持ち、特定のスライドを開いた状態でページをロードできるようにすることを目的としています。
動作させるには、hashNavigation:trueパラメータを渡して有効にし、data-hash属性にスライドハッシュを追加する必要があります。:
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" data-hash="slide1">Slide 1</div>
<div class="swiper-slide" data-hash="slide2">Slide 2</div>
<div class="swiper-slide" data-hash="slide3">Slide 3</div>
<div class="swiper-slide" data-hash="slide4">Slide 4</div>
<div class="swiper-slide" data-hash="slide5">Slide 5</div>
...
</div>
</div>const swiper = new Swiper('.swiper-container', {
//enable hash navigation
hashNavigation: true,
});Hash Navigation Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| hashNavigation | HashNavigationOptions | boolean | スライドへのハッシュURLナビゲーションを有効にします。ハッシュナビゲーションのパラメータを持つオブジェクト、またはデフォルト設定で有効にするにはtrueを指定します。 const swiper = new Swiper('.swiper-container', {
hashNavigation: {
replaceState: true,
},
});
| |
| { | |||
| replaceState | boolean | false | hashnavに加えて、現在のURLの状態を履歴に追加するのではなく、新しいものに置き換えるように動作します。
|
| watchState | boolean | false | trueに設定すると、ブラウザの履歴やドキュメントの場所に直接ハッシュを設定することで、(hashnavが有効な場合)スライドのナビゲーションも可能になります。
|
| } | |||
Hash Navigation Events
| Name | Arguments | Description |
|---|---|---|
| hashChange | (swiper) | Event will be fired on window hash change |
| hashSet | (swiper) | Event will be fired when swiper updates the has |
History Navigation
スライドする度にURLを書き換える。ちょっと何に使うかわからないですね。
History Navigation Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| history | HistoryOptions | boolean | すべてのスライドが独自のURLを持つ、ヒストリープッシュ状態を有効にします。このパラメータでは、 ヒストリーナビゲーションのパラメータを持つオブジェクト、またはデフォルト設定で有効にする場合はtrue const swiper = new Swiper('.swiper-container', {
history: {
replaceState: true,
},
});
<!-- will produce "slides/slide1" url in browser history --> <div class="swiper-slide" data-history="slide1"></div> | |
| { | |||
| key | string | 'slides' | Url key for slides
|
| replaceState | boolean | false | hashnavやhistoryに加えて動作し、現在のURLの状態をhistoryに追加するのではなく、新しいものに置き換えます。
|
| root | string | '' | Swiperページのルートは、Swiper履歴モードを使用するときに、ルートWebサイトのページではなく、指定するのに便利です。例えば、https://my-website.com/、https://my-website.com/subpage/、/subpage/などです。 |
| } | |||
Controller
複数スライダーがある時に、片方のスライダーを操作したらもう片方のスライダーも一緒に動くようにしたい!そんな願いを叶えます。
2つの場合はこんなん。
1 2 3 4 5 6 | var Swiper1 = new Swiper('#swiper-container1'); var Swiper2 = new Swiper('#swiper-container2',{ controller: { control: Swiper1, // ここに1つ目のスライダーの変数名 }, }) |
それより多いならこっちのがいいかも。
1 2 3 4 5 6 7 | var Swiper1 = new Swiper('#swiper-container1'); var Swiper2 = new Swiper('#swiper-container2'); var Swiper3 = new Swiper('#swiper-container3',{ controller:{ control: [Swiper1, Swiper2], // ここに1つ目と2つ目のスライダーの変数名 }, }) |
後からこれで追加もできる。
1 2 3 4 5 6 7 8 | Swiper1.controller.control = Swiper2; Swiper2.controller.control = Swiper1; //ちなみに以下のような書き方でもOKだった。 Swiper1.controller.control = [Swiper2]; Swiper2.controller.control = [Swiper1]; |
inverse: trueだと動きが逆になる。デフォルトではfalse。
Swiper2の実行前(Swiper2の行まで実行されていない)に、Swiper1の中で、Swiper2に関するコントローラー処理をやってもうまく行かない。
[Rails]Swiperで画像スライド作成 - Qiita#完成イメージ#Swiperとはスライダー(カルーセル)が作れるJavaScriptライブラリです。しかもPCでもスマホでも使えて、レスポンシブ対応可能!引用元…var mySwiper1 = new Swiper('.swiper1', {
省略。ここでcontroller...mySwiper2は定義しない }); var mySwiper2 = new Swiper('.swiper2', { slideToClickedSlide: true, // スライドクリックでそのスライドに移動する centeredSlides: true, // センター表示 slidesPerView: 3, // 一度に表示するスライド数を指定 controller: { control: mySwiper1, // 連動させるSwiperの定義名を指定 by: 'slide', // 連動Swiperをスライド単位で制御 }, }); mySwiper1.controller.control = mySwiper2; //mySwiperを実行したあとで、このコードを実行しなければならない。
Controller Parameters コントローラ
| Name | Type | Default | Description |
|---|---|---|---|
| controller | ControllerOptions | コントローラのパラメータを持つオブジェクト、またはデフォルト設定で有効にする場合はtrue const swiper = new Swiper('.swiper-container', {
controller: {
inverse: true,
},
});
| |
| { | |||
| by | 'slide' | 'container' | 'slide' | 他のスライダをコントロールする方法を定義します。スライドごと(他のスライダのグリッドを基準にして)、またはすべてのスライド/コンテナに依存して(スライダの合計パーセンテージに依存して)コントロールします。
|
| control | Swiper | | 別のSwiperインスタンス、またはこのSwiperによって制御されるべきSwiperインスタンスの配列を渡します。 | |
| inverse | boolean | false | trueに設定すると、制御が逆方向になります。 |
| } | |||
Controller Methods
| Properties | ||
|---|---|---|
| swiper.controller.control | Swiper | 別のSwiperインスタンス、またはこのSwiperによって制御されるべきSwiperインスタンスの配列を渡します。 |
Accessibility (a11y)
音声やスクリーンリーダーを使用するなら。
1 2 3 4 5 6 | var mySwiper = new Swiper('.swiper-container', { a11y: { prevSlideMessage: 'Previous slide', nextSlideMessage: 'Next slide', }, }); |
Accessibility Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| a11y | A11yOptions | a11yパラメータを持つオブジェクト、またはデフォルト設定で有効にする場合はtrue。 const swiper = new Swiper('.swiper-container', {
a11y: {
prevSlideMessage: 'Previous slide',
nextSlideMessage: 'Next slide',
},
});
| |
| { | |||
| containerMessage | string | null | null | 外側のスワイパーコンテナのスクリーンリーダー用メッセージ |
| containerRoleDescriptionMessage | string | null | null | スクリーンリーダーのために、外側のスワイパーコンテナの役割を説明するメッセージ |
| enabled | boolean | true | Enables A11y |
| firstSlideMessage | string | 'This is the first slide' | スワイパーが最初のスライドにいるときに前のボタンを押すと、スクリーンリーダー用のメッセージが表示される
|
| itemRoleDescriptionMessage | string | null | null | スライド要素の役割を説明するスクリーンリーダー用メッセージ |
| lastSlideMessage | string | 'This is the last slide' | Message for screen readers for previous button when swiper is on last slide
|
| nextSlideMessage | string | 'Next slide' | Message for screen readers for next button
|
| notificationClass | string | 'swiper-notification' | CSS class name of A11y notification
|
| paginationBulletMessage | string | 'Go to slide {{index}}' | Message for screen readers for single pagination bullet
|
| prevSlideMessage | string | 'Previous slide' | 前ボタンのスクリーンリーダー用メッセージ |
| slideLabelMessage | string | '{{index}} / {{slidesLength}}' | スライド要素のラベルを説明するスクリーンリーダー用メッセージ |
| slideRole | string | 'group' | Value of swiper slide |
| } | |||
Custom Build
You have two options of making custom version of Swiper.
Using JS Modules
If you use bundler with JS modules support in your project you can import only the modules you need:
// Import Swiper and modules
import SwiperCore, { Navigation, Pagination, Scrollbar } from 'swiper/core';
// Install modules
SwiperCore.use([Navigation, Pagination, Scrollbar]);
// Now you can use Swiper
const swiper = new Swiper('.swiper-container', {
speed: 500,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// ...
});
The following modules are exported:
Swiper- core libraryVirtual- Virtual Slides moduleKeyboard- Keyboard Control moduleMousewheel- Mousewheel Control moduleNavigation- Navigation modulePagination- Pagination moduleScrollbar- Scrollbar moduleParallax- Parallax moduleZoom- Zoom moduleLazy- Lazy moduleController- Controller moduleA11y- Accessibility moduleHistory- History Navigation moduleHashNavigation- Hash Navigation moduleAutoplay- Autoplay moduleEffectFade- Fade Effect moduleEffectCube- Cube Effect moduleEffectFlip- Flip Effect moduleEffectCoverflow- Coverflow Effect moduleThumbs- Thumbs module
Using Build Script
Swiper comes with gulp builder that allows to build custom library version where you may include only required modules. We need the following:
- Download and unzip Swiper GitHub repository to local folder
- Install Node.js (if not installed)
- Now, we need to install required dependencies. Go to the folder with downloaded and unzipped Swiper repository and execute in terminal:
$ npm install - Open
scripts/build-config.jsfile:module.exports = { // remove components you don't need components: [ 'virtual', 'keyboard', 'mousewheel', 'navigation', 'pagination', 'scrollbar', 'parallax', 'zoom', 'lazy', 'controller', 'a11y', 'history', 'hash-navigation', 'autoplay', 'effect-fade', 'effect-cube', 'effect-flip', 'effect-coverflow', 'thumbs', ], // default color of navigation elements themeColor: '#007aff', // additional color to be included colors: { white: '#ffffff', black: '#000000', }, }; - Now, we are ready to build custom version of Swiper:
$ npm run build:prod - That is all. Generated CSS and JS files and their minified versions will be available in
package/folder.
TypeScript Definitions
Swiper (since version 6) is fully typed, it exports Swiper and SwiperOptions types:
// main.ts
import { Swiper, SwiperOptions } from 'swiper';
const swiperParams: SwiperOptions = {
slidesPerView: 3,
spaceBetween: 50,
};
const swiper = new Swiper('.swiper-container', swiperParams);
You can also check auto generated TypeScript definitions explorer for all the types, options, properties and methods.




