简体   繁体   English

Angular 中使用 Swiper 的双向鼠标滚轮滑块

[英]Bidirectional mousewheel sliders in Angular using Swiper

I'm trying to build two sliders which will scroll horizontally (in opposite direction) when mousewheel event is triggered.我正在尝试构建两个滑块,当触发鼠标滚轮事件时,它们将水平滚动(相反方向)。

As you can see here, it is quite easy using the Swiper API with following configs:正如您在此处看到的,使用具有以下配置的 Swiper API 非常容易:

var swiper = new Swiper('.row-top', {
    freeMode: true,
    slidesPerView: 4,
    preloadImages: true,
    lazy: true,
    loop: true,
    mousewheel: {
        invert: true,
        eventsTarget: 'body'
    },
});
var swiper = new Swiper('.row-bottom', {
    freeMode: true,
    slidesPerView: 4,
    preloadImages: true,
    lazy: true,
    loop: true,
    mousewheel: {
        invert: false,  
        eventsTarget: 'body'
    },
});

It works like a charm as you can see here: https://jsfiddle.net/sx0mc9po/正如您在这里看到的那样,它就像一个魅力: https://jsfiddle.net/sx0mc9po/

I use the mousewheel option eventsTarget: 'body' so that, the scroll event will be triggered anywhere on the page.我使用鼠标滚轮选项eventsTarget: 'body'这样,滚动事件将在页面上的任何位置触发。

In Angular I use the ngx-swiper-wrapper as directive and the Swiper works fine:在 Angular 中,我使用ngx-swiper-wrapper作为指令,并且 Swiper 工作正常:

<div class="swiper-controller">
    <div  class="swiper-container row-top" [swiper]="configTop">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
            <div class="swiper-slide">Slide 4</div>
            <div class="swiper-slide">Slide 5</div>
            <div class="swiper-slide">Slide 6</div>
            <div class="swiper-slide">Slide 7</div>
            <div class="swiper-slide">Slide 8</div>
            <div class="swiper-slide">Slide 9</div>
        </div>
    </div>

    <div  class="swiper-container row-bottom" [swiper]="configBottom">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
            <div class="swiper-slide">Slide 4</div>
            <div class="swiper-slide">Slide 5</div>
            <div class="swiper-slide">Slide 6</div>
            <div class="swiper-slide">Slide 7</div>
            <div class="swiper-slide">Slide 8</div>
            <div class="swiper-slide">Slide 9</div>
        </div>
    </div>
</div>

The config is the same as mentioned above.配置与上面提到的相同。 But Angular completely ignores the option但是 Angular 完全忽略了这个选项

mousewheel: {
    ... 
    eventsTarget: 'body'
},

My mouse pointer has to hover the swiper-container otherwise it will not scroll any container.我的鼠标指针必须指向滑动容器 hover 否则它不会滚动任何容器。 Since there are two swiper-container it only scrolls one.由于有两个 swiper-container 它只滚动一个。

I'm not sure if this is a bug or Angular isn't allowing to put a listener on body from this component.我不确定这是否是一个错误或 Angular 不允许在该组件的body上放置一个侦听器。 If so is there any workaround or am I missing something?如果是这样,有什么解决方法还是我错过了什么?

Based on their documentation, install the Swiper Mousewheel module in your controller:根据他们的文档,在 controller 中安装 Swiper Mousewheel 模块:

import { Component } from '@angular/core';

// import Swiper core and required modules
import SwiperCore, { Mousewheel, Navigation, Pagination, Scrollbar, A11y } from 'swiper';

// install Swiper modules
SwiperCore.use([Mousewheel, Navigation, Pagination, Scrollbar, A11y]);

More information: https://swiperjs.com/angular更多信息: https://swiperjs.com/angular

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM