根据用户选择拆分图像滑块的框架

时间:2018-11-26 13:14:37

标签: html css angular

我有一个单独的Angular Component,其中有一个图像滑块。单击左侧或右侧按钮,图像将移动并设置动画。 我想拆分图像的框架,并想滑动一个以上的图像(基本上这个数字取决于用户的选择,应该在组件的HTML文件中可编辑),然后单击按钮。单个图像过渡一切正常。我想使图像的数量动态化,而我的HTML也是如此。我不能使用任何第三方组件。

这是我的TS代码:

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

@Component({
  selector: 'app-slider',
  templateUrl: './slider.component.html',
  styleUrls: ['./slider.component.css']
})
export class SliderComponent implements OnInit {
  slideIndex: number = 1;
  slides: any;
  image: any = [
    'assets/img2.jpg',
    'assets/img3.jpg',
    'assets/img4.jpg',
    'assets/img3.jpg',
    'assets/img4.jpg'
  ];
  b: any = [];
  constructor() {}
  ngOnInit() {
    this.slides = document.getElementsByClassName('mySlides');
    this.slides[0].style.display = 'block';
    for (let i = 0; i < this.image.length + 1; i++) {
      this.b.push(i);
    }
  }
  plusSlides(n: any) {
    this.showSlides((this.slideIndex += n));
  }
  currentSlide(n: any) {
    this.showSlides((this.slideIndex = n));
  }
  showSlides(n: any) {
    var i;
    this.slides = document.getElementsByClassName('mySlides');
    var dots = document.getElementsByClassName('dot');
    if (n > this.slides.length) {
      this.slideIndex = 1;
    }
    if (n < 1) {
      this.slideIndex = this.slides.length;
    }
    for (i = 0; i < this.slides.length; i++) {
      this.slides[i].style.display = 'none';
    }
    for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace('active', '');
    }

    this.slides[this.slideIndex - 1].style.display = 'block';
    dots[this.slideIndex - 1].className += ' active';
  }
}

这是我的HTML代码:

 <body>
    <div class="slideshow-container">
    <div class="mySlides">
      <div class="numbertext">1 / {{image.length+1}}</div>
      <img  src="assets/img1.jpg" style="width:50%">
      <div class="text"></div>
    </div>

    <div class="mySlides" *ngFor="let img of image;let x=index">
      <div class="numbertext">{{x+2}} / {{image.length+1}}</div>
      <img [src]="img" style="width:50%">
      <div class="text"></div>
    </div>

      <a class="prev fa fa-arrow-circle-left" style="font-size:24px" (click)="plusSlides(-1)"></a>
      <a class="next fa fa-arrow-circle-right" style="font-size:24px" (click)="plusSlides(1)"></a>
    </div>

    <br>

    <div style="text-align:center">
      <span class="dot" *ngFor="let a of b;let x=index;" (click)="currentSlide(x+1)"></span> 
    </div>
    </body>

这是我的CSS代码:

 * {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}

.mySlides {display: none;}

img {margin-left:180px;
height: 150px;
cursor: pointer;
animation-name: fade;
animation-duration: 1.5s;
}



/* Slideshow container */
.slideshow-container {
  max-width: 700px;
  position: relative;
  margin: auto;
 }

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto; 
  padding: 16px;
  margin-top: -22px;
  font-weight: bold;
  font-size: 18px; 
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(240, 192, 192, 0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #5bcfec;
}

/* Fading animation */
@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}

0 个答案:

没有答案