我目前面临着将断点集成到Swiper代码中的挑战.尽管我进行了try ,但我仍无法成功实现断点,而且该功能似乎没有按预期工作.我正在询问是否有其他人遇到过类似的问题,或者是否有关于在此特定代码中实现断点的成功 case .对解决这一问题的任何指导或见解都将不胜感激.

Html:

<div>
    <swiper-container style="--swiper-navigation-color: #fff; --swiper-pagination-color: #fff" class="mySwiper"
    thumbs-swiper=".mySwiper2" space-between="10" navigation="true">
    <swiper-slide *ngFor="let slide of slides">
      <img [src]="slide.image" />
    </swiper-slide>
  </swiper-container>

  <swiper-container class="mySwiper2" space-between="10" slides-per-view="6" free-mode="true"
    watch-slides-progress="true">
    <swiper-slide *ngFor="let slide of slides">
      <img [src]="slide.image" />
    </swiper-slide>
  </swiper-container>

</div>

SCSS:

/* You can add global styles to this file, and also import other style files */
    swiper-slide {
      text-align: center;
      font-size: 18px;
      background: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      background-size: cover;
      background-position: center;
      img {
        display: block;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .mySwiper {
    // defines height and width of main swiper
      height: 600px;
      width: 600px;
    }

    .mySwiper2 {
      // defines height and width of thumbs swiper
      height: 100px;
      width: 600px;
      box-sizing: border-box;
      padding: 10px 0;
      swiper-slide {
        opacity: 0.6; // this set default opacity to all slides
      }
      .swiper-slide-thumb-active {
        opacity: 1; // this reset the opacity one for the active slide
      }
    }

S:

    import { CommonModule } from '@angular/common';
    import { CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core';
    
    @Component({
      selector: 'app-thumbs-default',
      standalone: true,
      imports: [CommonModule],
      templateUrl: './thumbs-default.component.html',
      styleUrl: './thumbs-default.component.scss',
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
    export class ThumbsDefaultComponent {
    
      slides = [
    {image: 'https://swiperjs.com/demos/images/nature-1.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-2.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-3.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-4.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-5.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-6.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-7.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-8.jpg'},
    {image: 'https://swiperjs.com/demos/images/nature-10.jpg'}
  ];
    
    }

推荐答案

我不能直接通过HTML属性执行断点初始化,所以为了获得断点,我通过ViewChildngAfterViewInit进行编程初始化.另外,为了让断点正常工作,我们不应该为幻灯片设置像素宽度,我认为这会导致代码中的问题!

TS

import { CommonModule } from '@angular/common';
import {
  Component,
  OnInit,
  CUSTOM_ELEMENTS_SCHEMA,
  ViewChild,
  ElementRef,
} from '@angular/core';

@Component({
  selector: 'app-swiper',
  standalone: true,
  imporTS: [CommonModule],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  templateUrl: './swiper.component.HTML',
})
export class SwiperComponent implemenTS OnInit {
  @ViewChild('swiper2') swiper2!: ElementRef<any>;
  slides = [
    { image: 'https://swiperjs.com/demos/images/nature-1.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-2.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-3.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-4.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-5.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-6.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-7.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-8.jpg' },
    { image: 'https://swiperjs.com/demos/images/nature-10.jpg' },
  ];
  constructor() {}

  ngOnInit() {}

  ngAfterViewInit() {
    const swiperParams = {
      breakpoinTS: {
        100: {
          slidesPerView: 3,
        },
        640: {
          slidesPerView: 5,
        },
        1024: {
          slidesPerView: 6,
        },
      },
    };

    // now we need to assign all parameters to Swiper element
    Object.assign(this.swiper2.nativeElement, swiperParams);
    this.swiper2.nativeElement.initialize();
  }

  setupImageZoom() {
    alert('setting up zoom!');
  }
}

HTML

<div>
  <swiper-container
    style="--swiper-navigation-color: #fff; --swiper-pagination-color: #fff"
    class="mySwiper"
    thumbs-swiper=".mySwiper2"
    space-between="10"
    navigation="true"
  >
    <swiper-slide *ngFor="let slide of slides">
      <img [src]="slide.image" />
    </swiper-slide>
  </swiper-container>

  <swiper-container
    #swiper2
    class="mySwiper2"
    space-between="10"
    slides-per-view="6"
    free-mode="true"
    watch-slides-progress="true"
    init="false"
  >
    <swiper-slide *ngFor="let slide of slides">
      <img [src]="slide.image" />
    </swiper-slide>
  </swiper-container>
</div>

SCSS

/* Add application styles & imporTS to this file! */
@import 'swiper/css';
@import 'swiper/css/navigation';
@import 'swiper/css/pagination';

/* You can add global styles to this file, and also import other style files */
swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  background-size: cover;
  background-position: center;
  img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.mySwiper {
  height: 600px;
  width: 100%;
}

.mySwiper2 {
  // defines height and width of thumbs swiper
  height: 100px;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 0;
  swiper-slide {
    opacity: 0.6; // this set default opacity to all slides
  }
  .swiper-slide-thumb-active {
    opacity: 1; // this reset the opacity one for the active slide
  }
}

stackblitz demo

Angular相关问答推荐

在Angular 17中加载页面时打开打印对话框

具有多重签名的Angular Mocking Service

PrimeNG面包屑组件生成奇怪的&

以17角表示的自定义元素

从、管道和映射可观察到的逻辑?

如何使用解析器处理Angular 路由中存储的查询参数以避免任何额外的导航?

MAT-复选框更改事件未在onSubscriberCheck函数中返回选中的值

懒惰加载角404路径

如果Rxjs间隔请求仍在进行,则不应重置,但如果用户更改输入,则应重置

合并Cypress和Karma的代码覆盖率报告JSON文件不会产生正确的结果

为什么初始化值为 1 的BehaviorSubject 不能分配给类型number?

Angular 在关闭另一个对话框后打开另一个对话框的最佳做法是什么?

Angular 13 - 何时创建嵌入式视图?

如何在angular 5 中获取基本网址?

Angular 2:为什么在检索路由参数时使用 switchMap?

为什么用?模板绑定中的运算符?

CustomPipe 没有提供者

如何导航到同级路由?

RxJS - 发生错误时观察到的不会完成

如何将 @Input 与使用 ComponentFactoryResolver 创建的组件一起使用?