本文へ移動
Webstudio YAS
  • About
  • Price
  • Work
  • Blog
  • Contact
  • About
  • Price
  • Work
  • Blog
  • Contact
  1. ホーム
  2. Blog
  3. Swiper.jsのautoplayが効かない

公開日: 2021.01.05

Swiper.jsのautoplayが効かない

Swiper.jsのautoplayが効かない

目次

  • 現象
  • 原因
  • 修正
  • まとめ

画像やコンテンツのスライドショーが簡単にできるJavaScriptライブラリー「Swiper.js」のautoplayオプションが効かなくて、しばらく迷走した時の話。

現象

「Swiper.js」をnpmでインストールして以下のようにインポートをして使っていたのですが、autoplayオプションが効かなかった。

// import modules:
import Swiper, { Navigation, Pagination } from 'swiper';

// configure Swiper to use modules
Swiper.use([Navigation, Pagination]);

// import styles
import 'swiper/swiper-bundle.css';

// init Swiper:
const swiper = new Swiper('.swiper-container', {
    autoplay: {
        delay: 5000
    },
});

原因

autoplayモジュールをインポートしていなかった。_| ̄|○

修正

以下のように、autoplayモジュールを追加する。

// import modules:
import Swiper, { Navigation, Pagination, Autoplay } from 'swiper';

// configure Swiper to use modules
Swiper.use([Navigation, Pagination, Autoplay]);

すべてのモジュールを読み込む場合

// import Swiper bundle with all modules installed
import Swiper from'swiper/bundle';

まとめ

どこかのサイトからコードをコピーして使っていたので、インポート部分でモジュール別に読み込んでいる事にしばらく気づかなかった。。。

コピペも楽だけど、コードの内容をちゃんと理解して持ってこないと後で厄介な事になりますね(反省)

タグ

  • autoplay
  • Swiper.js

関連記事

2019.06.09

ページ読み込み後に実行する関数の違い一覧(JavaScriptとjQuery)

ページ読み込み後に実行する関数の違い一覧(JavaScriptとjQuery)

2019.11.25

【超簡単】Masonryの使い方と最低限の実装方法

【超簡単】Masonryの使い方と最低限の実装方法

2021.05.07

【jQuery】スマホのロングタップ(長押し)イベントの実装

【jQuery】スマホのロングタップ(長押し)イベントの実装
Webstudio YAS
  • About
  • Price
  • Work
  • Blog
  • Contact
YAS プロフィール画像

長野県長野市在住
WEBエンジニア YAS

LINKS

  • X
  • Instagram
  • GitHub
Design
  • スタンダード
  • 和モダン
  • エンジニア
  • 信州ナチュラル
プライバシーポリシー

@ Webstudio YAS