[Rails]使用Swiper创建图像幻灯片


完整图片

Image from Gyazo

什么是刷卡器

一个JavaScript库,允许您创建滑块(轮播)。
此外,它可以在PC和智能手机上使用,并且反应灵敏!
来源https://garigaricode.com/swiper/

可以用了

这次,我将阅读CDN的内容。

application.html.haml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
!!!
%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
    %title FrimaApp
    %script{src: "https://js.pay.jp/", type: "text/javascript"}
    = csrf_meta_tags
    = csp_meta_tag
    = stylesheet_link_tag    'application', media: 'all'
    %link{:href => "https://unpkg.com/swiper/css/swiper.min.css", :rel => "stylesheet"}/
    = javascript_include_tag 'application'
  %body
    %script{:src => "https://unpkg.com/swiper/js/swiper.min.js"}
    = yield

%link{:href => "https://unpkg.com/swiper/css/swiper.min.css", :rel => "stylesheet"}/

以%haad为单位

%script{:src => "https://unpkg.com/swiper/js/swiper.min.js"}
以%身体描述。

看法

show.html.haml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
              / メイン表示部分、各スライド
              .swiper-container.swiper1
                .swiper-wrapper
                  - @images.each do |images|
                    = image_tag asset_path(images.content), class:"swiper-slide"
                / 次へ、前へボタン
                .swiper-button-prev.prev1
                .swiper-button-next.next1
                / ページネーション
                .swiper-pagination

              / 2段目、各スライド
              .swiper-container.swiper2
                .swiper-wrapper
                  - @images.each do |images|
                    = image_tag asset_path(images.content), class:"swiper-slide"

JS文件

javascripts / item / show.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$(function(){
    var mySwiper1 = new Swiper('.swiper1', {
        loop: true, // ループ
        loopedSlides: (".swiper1").length, // loop: trueの場合必須 総スライド数の半分以上の値を設定
        navigation: {
            nextEl: '.next1', // 次ページボタンのセレクタ名を指定
            prevEl: '.prev1', // 前ページボタンのセレクタ名を指定
        },
        pagination: {
            el: '.swiper-pagination', // ページネーションのセレクタ名を指定
            type: 'bullets', // ●○??形式
            clickable: true, // type: 'bullets'の時のみ有効 ?クリックで直接そのスライドへ移動
        },
    });
    var mySwiper2 = new Swiper('.swiper2', {
        loop: true,
        loopedSlides: (".swiper2").length, // 最初の前、最後の後に複製される非表示のスライド数を指定
        slideToClickedSlide: true, // スライドクリックでそのスライドに移動する
        centeredSlides: true, // センター表示
        slidesPerView: 3, // 一度に表示するスライド数を指定
        controller: {
            control: mySwiper1, // 連動させるSwiperの定義名を指定
            by: 'slide',  // 連動Swiperをスライド単位で制御
        },
    });
    mySwiper1.controller.control = mySwiper2;
});

mySwiper1.controller.control = mySwiper2

在第一个和第二个Swiper之间指定轮廓器的原因是,代码是从顶部开始按顺序执行的,因此在制作第一个Swiper时,第二个Swiper尚未定义并且尚不存在。
即使您尝试在这种状态下进行合作,您也将最终判断为没有这种东西。
创建第二个Swiper时,第一个Swiper已经存在,因此可以将其写入var mySwiper2 =…。
因此,如果在完成第二个Swiper绘制后指定了第一个轮廓绘制器,它将起作用。
来源https://garigaricode.com/swiper/

SCSS

item.show.scss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
              .swiper-container {
                width: 400px;
                font-size: 0;
              }
              /* 2段目のSwiper全体のスタイル */
              .swiper2 {
                margin-top: 8px;
              }
              /* 2段目のSwiperのスライドのスタイル */
              .swiper2 .swiper-slide {
                height: 100px;
                line-height: 100px;
                -webkit-filter: brightness(0.5);
                filter: brightness(0.5); // 画像の明度調節
              }
              /* 2段目のSwiperの現在のスライドのスタイル */
              .swiper2 .swiper-slide-active {
                -webkit-filter: brightness(1);
                filter: brightness(1);
              }

-webkit-

什么是

-webkit-?
Webkit的描述称为供应商前缀,计划在CSS3中实现的功能由每个浏览器公司预先实现,以便可以在每个浏览器中使用。
来源https://code-schools.com/css-webkit/

完全的

我引用了很多。

如果您有任何建议,请发表评论! !!