Bootstrap Слайды отстают при использовании многоэлементной карусели с использованием угловых 6 - PullRequest
0 голосов
/ 25 августа 2018

Моя проблема в том, что я использую Angular 6, Bootstrap v4 и jQuery для карусели из нескольких предметов. Если у меня есть данные типа [1,2,3,4,5,6] там, на карусели, они должны показать [1,2,3]. После нажатия на следующий он должен показать [4,5,6].

До этой части я достиг, я сталкиваюсь с проблемами. Перемещается только первая активная карта, также имеется отставание, а предыдущая и следующая кнопки не видны. Пожалуйста, проверьте один раз файл Stackblitz вы поймете проблему

Вот моя работа в Stackblitz

https://stackblitz.com/edit/angular-jvr6dh

HTML код

<div class="container">
  <div id="dataInfo">
      <h2>Information</h2>
      <div id="myCarousel" class="carousel slide" data-ride="carousel">
          <div class="carousel-inner row w-100 mx-auto">
            <div class="carousel-item col-md-4 active">
              <div class="card">
                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap">
                <div class="card-body">
                  <h4 class="card-title">Card 1</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
            <div class="carousel-item col-md-4">
              <div class="card">

                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap"><div class="card-body">
                  <h4 class="card-title">Card 2</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
            <div class="carousel-item col-md-4">
              <div class="card">

                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap"><div class="card-body">
                  <h4 class="card-title">Card 3</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
            <div class="carousel-item col-md-4">
              <div class="card">

                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap"><div class="card-body">
                  <h4 class="card-title">Card 4</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
            <div class="carousel-item col-md-4">
              <div class="card">

                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap"> <div class="card-body">
                  <h4 class="card-title">Card 5</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
            <div class="carousel-item col-md-4">
              <div class="card">

                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap"><div class="card-body">
                  <h4 class="card-title">Card 6</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
            <div class="carousel-item col-md-4">
              <div class="card">

                <img class="card-img-top img-fluid" src="https://images.freeimages.com/images/large-previews/85a/daisy-s-1375598.jpg"  width="100" height="100" alt="Card image cap"><div class="card-body">
                  <h4 class="card-title">Card 7</h4>
                  <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                  <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
                </div>
              </div>
            </div>
          </div>
          <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>

  </div>


</div>

. код ts:

$("#myCarousel").on("slide.bs.carousel", function(e) {
      var $e = $(e.relatedTarget);
      var idx = $e.index();
      var itemsPerSlide = 3;
      var totalItems = $(".carousel-item").length;

      if (idx >= totalItems - (itemsPerSlide - 1)) {
        var it = itemsPerSlide - (totalItems - idx);
        for (var i = 0; i < it; i++) {
          // append slides to end
          if (e.direction == "left") {
            $(".carousel-item")
              .eq(i)
              .appendTo(".carousel-inner");
          } else {
            $(".carousel-item")
              .eq(0)
              .appendTo($(this).find(".carousel-inner"));
          }
        }
      }
    });
  }

.scss код

  .carousel-inner .active,
.carousel-inner .active + .carousel-item,
.carousel-inner .active + .carousel-item + .carousel-item {
  display: block;
}

.carousel-inner .carousel-item.active:not(.carousel-item-right):not(.carousel-item-left),
.carousel-inner .carousel-item.active:not(.carousel-item-right):not(.carousel-item-left) + .carousel-item,
.carousel-inner .carousel-item.active:not(.carousel-item-right):not(.carousel-item-left) + .carousel-item + .carousel-item {
  transition: none;
}

.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
  position: relative;
  transform: translate3d(0, 0, 0);
}

.carousel-inner .active.carousel-item + .carousel-item + .carousel-item + .carousel-item {
  position: absolute;
  top: 0;
  right: -33.3333%;
  z-index: -1;
  display: block;
  visibility: visible;
}

/* left or forward direction */
.active.carousel-item-left + .carousel-item-next.carousel-item-left,
.carousel-item-next.carousel-item-left + .carousel-item,
.carousel-item-next.carousel-item-left + .carousel-item + .carousel-item,
.carousel-item-next.carousel-item-left + .carousel-item + .carousel-item + .carousel-item {
  position: relative;
  transform: translate3d(-100%, 0, 0);
  visibility: visible;
}

/* farthest right hidden item must be abso position for animations */
.carousel-inner .carousel-item-prev.carousel-item-right {
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  display: block;
  visibility: visible;
}

/* right or prev direction */
.active.carousel-item-right + .carousel-item-prev.carousel-item-right,
.carousel-item-prev.carousel-item-right + .carousel-item,
.carousel-item-prev.carousel-item-right + .carousel-item + .carousel-item,
.carousel-item-prev.carousel-item-right + .carousel-item + .carousel-item + .carousel-item {
  position: relative;
  transform: translate3d(100%, 0, 0);
  visibility: visible;
  display: block;
  visibility: visible;
}

Ответы [ 3 ]

0 голосов
/ 02 сентября 2018

Вам может быть проще написать собственное поведение карусели и убрать любую зависимость от jquery.Вы все еще можете использовать Bootstrap для стиля.

Мы также можем использовать угловую анимацию, чтобы сохранить поведение анимации. (Обратите внимание, что я впервые использую угловую анимацию, поэтому может быть более аккуратный способ сделать это)

Первый шаг - создать директиву, которая обрабатываетnext / prev поведение:

  @Input() animationDuration = 500;

  private slideLeft: AnimationFactory;
  private slideRight: AnimationFactory;
  private slideInLeft: AnimationFactory;
  private slideInRight: AnimationFactory;

  constructor(private el: ElementRef, private _builder: AnimationBuilder) {
  }

  ngOnInit() {
    this.slideLeft = this._builder.build([
      style({ transform: 'translateX(0)' }),
      animate(this.animationDuration, style({ transform: 'translateX(-100%)' })),
      style({ transform: 'translateX(0)' }),
    ]);

    this.slideRight = this._builder.build([
      style({ transform: 'translateX(0)' }),
      animate(this.animationDuration, style({ transform: 'translateX(100%)' })),
      style({ transform: 'translateX(0)' }),
    ]);

    this.slideInLeft = this._builder.build([
      style({ transform: 'translateX(100%)', right: 0 }),
      animate(this.animationDuration, style({ transform: 'translateX(0)' })),
      style({ right: 'initial' })
    ]);

    this.slideInRight = this._builder.build([
      style({ transform: 'translateX(-100%)', left: 0 }),
      animate(this.animationDuration, style({ transform: 'translateX(0)' })),
      style({ left: 'initial' })
    ]);
  }

  next(steps) {
      let active = this.el.nativeElement.querySelectorAll('.carousel-item.active');
      let inactive = this.el.nativeElement.querySelector('.carousel-item:not(.active)');
      // Start the animation
      this.animateAll(active, this.slideLeft);
      // Start the slide in animation for the next element
      this.preMoveElement(inactive);
      this.slideInLeft.create(inactive).play();

      setTimeout(() => {
        // Move the last element to start and make it active.
        active = this.el.nativeElement.querySelectorAll('.carousel-item.active');
        inactive = this.el.nativeElement.querySelector('.carousel-item:not(.active)');
        active[0].classList.remove('active');
        this.el.nativeElement.insertBefore(active[0], null);
        inactive.classList.add('active');
        if (steps && steps - 1 > 0) {
          this.next(steps - 1);
        }
      }, this.animationDuration);
   // }
  }

  prev(steps) {
    const active = this.el.nativeElement.querySelectorAll('.carousel-item.active');
    const children = this.el.nativeElement.children;
    const lastChild = children[children.length - 1];
    // Start the animation
    this.animateAll(active, this.slideRight);
    // Start the slide in animation for the next element
    this.preMoveElement(lastChild);
    this.slideInRight.create(lastChild).play();


    setTimeout(() => {
      // Remove the active class
      const lastActive = active[active.length - 1];
      lastActive.classList.remove('active');
      // Move the last element to the start, and make it active
      this.el.nativeElement.insertBefore(lastChild, children[0]);
      lastChild.classList.add('active');
      if (steps && steps - 1 > 0) {
        this.prev(steps - 1);
      }
    }, this.animationDuration);
  }

  private animateAll(elements: any[], animation: AnimationFactory) {
    elements.forEach(element => {
      animation.create(element).play();
    });
  }

  private preMoveElement(element) {
    element.style.display = 'block';
    element.style.position = 'absolute';
    setTimeout(() => {
      element.style = null;
    }, this.animationDuration);
  }

Затем вы можете получить доступ к функциям prev и next из HTML:

Измените элемент carousel-inner следующим образом:

<div class="carousel-inner row w-100 mx-auto" #cara="Carousel">

Затем мы можем использовать переменную cara для доступа к открытым функциям:

  <a class="carousel-control-prev" role="button" (click)="cara.prev(3)">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" role="button" (click)="cara.next(3)">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>

Наконец, отметьте 3 из carousel-item s как .active, поэтому3 отображаются одновременно.

Вот демонстрация StackBlitz

0 голосов
/ 18 июля 2019

Давно уже нет ответа на этот вопрос, но я не могу удержаться от другого подхода.

Отказ от ответственности Я украл 4 идеи о великом Нетанеле Базале и его удивительной карусели

Disclamer 2 это длинный, длинный ответ, поэтому для анимации читать, здесь мы собираемся получить

Карусель - это только HTML

<div class="wrap">
    <div #carousel></div>
</div>

где div #carousel перемещается влево / вправо с одной анимацией.

Первая идея от Nenatel, которую я позаимствовал, - это анимация. Он имеет только две функции: (я использую несколько переменных для параметризации функций)

  @Input() timing = '500ms ease-in';
  slideWidth;    //width of each slide
  numberSlides;  //number of slides
  offset;        //a variable to move the origen of movimient

  private transitionCarousel(time: any, slide: number) {
    const offset = this.offset - this.slideWidth * this.numberSlides;
    const myAnimation: AnimationFactory = this.buildAnimation(offset, time);
    this.player = myAnimation.create(this.carousel.nativeElement);
    this.player.play();
  }

  private buildAnimation(offset, time: any) {
    return this.builder.build([
      animate(time == null ? this.timing : 0, style({ transform: `translateX(${offset}px)` }))
    ]);
  }

Итак, мы звоним на

   transitionCarousel(null,4)  //to select the slide 4.

Хорошо, я собираюсь предположить, что у меня есть 6 разных изображений, и мы хотим показать 4 одновременно. Есть две критические позиции. Сильфон нашей карусели в разных штатах

 a)                            [0  1  2  3 ] 4  5  6
 b)                       0  1 [2  3  4  5 ] 6   
 c)                            [*  *  *  0 ]1  2  3  4  5  6
 d)           0  1  2  3  4  5 [6  *  *  * ]

Так, случай а) - это когда слайд = 0, случай б) - когда слайд = 2 Случай с), когда наш слайд 0 находится в 4-м положении, и случай d), когда слайд = 6

Очевидно, что состояния c) и d) создают нам проблему, потому что * показывает пустые пробелы (а нам не нужны белые пробелы). Итак, почему бы не повторить сначала наши изображения изображения 4, 5 и 6, а в конце изображения 0 1 и 3, так что наши состояния с) и г) становится как

 c)                            [4' 5' 6' 0 ] 1  2  3  4  5  6  0' 1' 2'
 d)  4' 5' 6' 0  1  2  3  4  5 [6  0' 1' 2']  

Хорошо, мы должны принять во внимание, что случай c) - когда слайд = -3, он идентичен слайду = 4

 c)        4' 5' 6' 0  1  2  3 [4  5  6  0']1' 2'

Итак, если после того, как движение упало на слайде меньше 0, мы мгновенно переместимся влево

Другой случай, который нужно учитывать, это d) когда мы нажимаем на правую кнопку. В этом случае нам нужно сделать два движения

 d)  4' 5' 6' 0  1  2  3  4  5 [6  0' 1' 2']  //slide=6
                           --click left button event--
                           //we move to the slide=-1 before transition
                          4' 5'[6' 0  1  2 ] 3  4  5  6  0' 1' 2' 
                          //and move to slide=3 in transition 
              4' 5' 6' 0  1  2 [3  4  5  6 ] 0' 1' 2' 

Итак, наша функция transitionCarousel становится похожей на

  private transitionCarousel(time: any, slide: number) {
    if (slide >= this.slides.length) {
        this.transitionCarousel(0, this.currentSlide - this.slidesQuantity)
        slide -= this.slidesQuantity;
    }

    const offset = this.offset - this.slideWidth * slide;
    const myAnimation: AnimationFactory = this.buildAnimation(offset, time);
    this.player = myAnimation.create(this.carousel.nativeElement);
    if (time != 0) {
      if (slide < 0) {
        this.player.onDone(() => {
          this.transitionCarousel(0, this.currentSlide)
        })
      }
      else
        this.currentSlide = slide;
    } this.player.play();
  }

Ну, нам нужна механика для дублирования слайдов. Мы собираемся сделать это, используя структурную директиву (2-я стая Нетанелу Базалу)

@Directive({
  selector: '[carouselItem]'
})
export class CarouselDirective implements OnInit {

  constructor(
    public templateRef: TemplateRef<any>,
    public viewContainer: ViewContainerRef) { }

  ngOnInit()
  {
    this.viewContainer.createEmbeddedView(this.templateRef)
  }
}

Обратите внимание, что templateRef и viewContainer объявлены как публичные, это позволяет нам копировать шаблон каждого элемента

<div *carouselItem>Hello world</div>

внутри другого.

Наша карусель становится как

<app-carousel #carousel>
  <ng-container *ngFor=" let item of items">
      <img *carouselItem [src]="item.img" >
  </ng-container>
</app-carousel>

где, например

  items = [{ img: 'https://picsum.photos/200/200?random=1' },
  { img: 'https://picsum.photos/200/200?random=2' },
  { img: 'https://picsum.photos/200/200?random=3' },
  { img: 'https://picsum.photos/200/200?random=4' },
  { img: 'https://picsum.photos/200/200?random=5' },
  { img: 'https://picsum.photos/200/200?random=6' },
  { img: 'https://picsum.photos/200/200?random=7' },
  { img: 'https://picsum.photos/200/200?random=8' }
  ];

Я хочу, чтобы моя карусель была масштабируемой, поэтому мне нужно знать, сколько слайдов можно разместить внутри карусели. Конечно, мы можем заключить карусель в div

<div style="width:600px">
    <app-carousel #carousel>
       ....
    </app-carousel>
</div>

И отправьте количество слайдов на нашу карусель, но лучше наша карусель сделает работу за нас. Поэтому нам нужно знать ширину слайдов. Здесь второй последний украл Нетанель. Идея Netanel состоит в том, чтобы иметь .html вроде

<div class="carousel-model">
    <ng-container [ngTemplateOutlet]="slides && slides.first?slides.first.templateRef:null">
    </ng-container>
</div>

если мы создадим директиву, которая выберет класс, такой как

@Directive({
  selector: '.carousel-model'
})
export class CarouselSlideElement {
}

И определил ViewChild как

  @ViewChild(CarouselSlideElement, { static: false, read: ElementRef }) slideElement: ElementRef

в ngAfterViewInit мы можем спросить о

   this.slideElement.nativeElement.getBoundingClientRect()

чтобы получить размеры

Хорошо, я вставил «таймер», потому что, если я загружаю изображения, я могу забыть о ширине этого изображения.

  ngAfterViewInit() {
    timer(0, 200).pipe(takeWhile(() => !this.slideWidth || !this.slides || !this.slides.first)).subscribe(() => {
      const square = this.slideElement.nativeElement.getBoundingClientRect();
      this.slideWidth = square.width;
      if (this.slideWidth && this.slides && this.slides.first)
        this.resizeCarousel()
    })
  }

Еще один шаг. Более сложная часть - это дублирование слайдов. Помните, что slides.first - это наш первый слайд, а slides.last наш последний слайд. Я перекрашиваю все в функцию resizeCarousel

  private resizeCarousel() {
    if (this.carousel) {
      let totalWidth = this.carousel.nativeElement.getBoundingClientRect().width;
      this.increment = Math.floor(totalWidth / this.slideWidth);

      let count = (this.increment * this.slideWidth) != totalWidth ? 1 : 0;
      this.offset = (totalWidth - 3 * (this.increment) * this.slideWidth) / 2 - this.slideWidth * count;
      console.log(totalWidth,count)
      this.slides.first.viewContainer.clear()
      this.slides.last.viewContainer.clear()

      this.slides.last.viewContainer.createEmbeddedView(this.slides.last.templateRef);
      let added = 0;
      this.slides.forEach((x, index) => {
        if (index && index >= (this.slides.length - this.increment - count)) {
          this.slides.first.viewContainer.createEmbeddedView(x.templateRef)
          added++
        }
        if (index < this.increment + count) {
          this.slides.last.viewContainer.createEmbeddedView(x.templateRef)
          added++
        }
      })
      this.slides.first.viewContainer.createEmbeddedView(this.slides.first.templateRef)
      this.currentSlide = 0;
      this.transitionCarousel(0, this.currentSlide);
    }
  }

Я добавляю изображение на каждую сторону, если они не соответствуют точно изображениям

ну, последняя "идея заимствования", которую я украл в NetBasal, hotListener для изменения размера окна

  @HostListener('window:resize', ['$event'])
  onResize(event) {
    if (this.slideWidth && this.slides && this.slides.first)
      this.resizeCarousel();
  }

И это все фолк, три функции для следующей, предыдущая и сет и "лист эль полло"

  prev() {
    this.transitionCarousel(null, this.currentSlide + this.increment);
  }

  next() {
    this.transitionCarousel(null, this.currentSlide - this.increment);
  }

  setSlide(slide: number) {
    slide = slide;
    this.transitionCarousel(null, slide);
  }

Обновление Лучше использовать анимацию

частный переходCarousel (время: любое, слайд: номер) {

const myAnimation: AnimationFactory = this.buildAnimation(time,slide);
this.player = myAnimation.create(this.carousel.nativeElement);
this.currentSlide = (slide >= this.slides.length) ? slide - this.slides.length :
                    (slide < 0) ? this.currentSlide = slide + this.slides.length :
                     slide
this.player.play();

}

приватная анимация сборки (время: любое, слайд: номер) { const animation: number = (slide> = this.slides.length)? 1: (слайд <0)? 2: 0; </p>

const offsetInitial = (slide >= this.slides.length)?
        this.offset - this.slideWidth * (this.currentSlide - this.slides.length):
        0;
let offsetFinal = (slide < 0)?
        this.offset - this.slideWidth * (slide + this.slides.length):
        0;

const offset = (slide >= this.slides.length)?
         this.offset - this.slideWidth * (slide-this.slides.length):
         this.offset - this.slideWidth * slide;

return animation==1 ? this.builder.build([
  style({ transform: `translateX(${offsetInitial}px)` }),
  animate(time == null ? this.timing : 0, style({ transform: `translateX(${offset}px)` }))
]) : animation==2 ? this.builder.build(sequence([
  animate(time == null ? this.timing : 0, style({ transform: `translateX(${offset}px)` })),
  style({ transform: `translateX(${offsetFinal}px` })]))
    : this.builder.build([
      animate(time == null ? this.timing : 0, style({ transform: `translateX(${offset}px)` }))
    ]);

}

0 голосов
/ 29 августа 2018

Исходя из вашего примера кода, похоже, что упрощение самого кода Bootstrap для использования стандартной карусели - лучший подход.Ссылка ниже является ответвлением примера, который вы предоставили со следующими изменениями:

https://stackblitz.com/edit/angular-yaevix

  • Удалены все пользовательские манипуляции с DOM в prev / next / etc.из вашего javascript, поэтому он использует компонент карусели Bootstrap, чтобы сделать все(вы можете оформить это так, как считаете нужным)

Каждая карта установлена ​​в качестве элемента карусели, но в вашем описании вы хотите разбить на страницы 3 за раз.Правильный подход здесь состоит в том, чтобы иметь один элемент карусели для каждых трех карт.См. Пример ниже

Пример одного элемента карусели с несколькими картами:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        <div class="carousel-item active">
            <div class="row">
                <div class="col-md-4">
                    <div class="card">Card 1</div>
                </div>
                <div class="col-md-4">
                    <div class="card">Card 2</div>
                </div>
                <div class="col-md-4">
                    <div class="card">Card 3</div>
                </div>
            </div>
        </div>
        <div class="carousel-item active">
            <div class="row">
                <div class="col-md-4">
                    <div class="card">Card 4</div>
                </div>
                <div class="col-md-4">
                    <div class="card">Card 5</div>
                </div>
                <div class="col-md-4">
                    <div class="card">Card 6</div>
                </div>
            </div>
        </div>
        <div class="carousel-item">
            <div class="row">
                <div class="col-md-4">
                    <div class="card">Card 7</div>
                </div>
            </div>
        </div>
    </div>
</div>
...