sách gpt4 ai đã đi

javascript - 如何将 HTML CSS 图像 slider 转换为 Angular 图像 slider ?

In lại 作者:行者123 更新时间:2023-12-04 14:54:19 32 4
mua khóa gpt4 Nike

我正在尝试从头开始以 Angular 实现图像 slider ,并尝试复制 w3school基于图像 slider 。

下面我尝试用 Angular 实现,谁能指导我如何使用 Angular 实现?

在这里你可以找到stackblitzLiên kết

component.html




1 / 3


Caption Text




2 / 3


Caption Two




3 / 3


Caption Three















component.ts

import { Component, OnInit, ViewChild, ElementRef, ViewChildren, QueryList } from '@angular/core';

@Thành phần({
selector: 'app-custom-gallery',
templateUrl: './custom-gallery.component.html',
styleUrls: ['./custom-gallery.component.css']
})
export class CustomGalleryComponent implements OnInit {

slideIndex:number = 1;
constructor (public elementRef: ElementRef) { }

ngOnInit(): void {
this.showSlides(this.slideIndex);
}

plusSlides(n) {
this.showSlides(this.slideIndex += n);
}

currentSlide(n) {
console.log(n)
this.showSlides(this.slideIndex = n);
}

showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {this.slideIndex = 1}
if (n < 1) {this.slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
console.log(typeof slides[i]);
// slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
// slides[this.slideIndex-1].style.display = "block";
dots[this.slideIndex-1].className += " active";
}

}

组件.css

* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
max-width: 1000px;
vị trí: tương đối;
margin: auto;
}

/* Next & previous buttons */
.prev, .next {
cursor: pointer;
vị trí: tuyệt đối;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: đậm;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}

/* Position the "next button" to the right */
.next {
phải: 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(0,0,0,0.8);
}

/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
vị trí: tuyệt đối;
bottom: 8px;
width: 100%;
căn chỉnh văn bản: ở giữa;
}

/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
cỡ chữ: 12px;
padding: 8px 12px;
vị trí: tuyệt đối;
top: 0;
}

/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
hiển thị: khối nội tuyến;
transition: background-color 0.6s ease;
}

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

/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}

@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

@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}
}

1 Câu trả lời

您可以使用以下方法:

创建一个 ImageSlider 组件,它将 images 列表作为输入,并跟踪当前幻灯片

export class ImageSliderComponent {
@Input() images: string[];

slideIndex: number = 0;

changeSlide(n: number) {
this.slideIndex += n;
}
}

在模板中,只显示事件幻灯片(引用hình ảnh nguồn绑定(bind)):



{{ slideIndex + 1}} / {{ images.length }}


[src]="images[slideIndex]"
style="width:100%"
/>
Caption Text






像这样使用你的组件:


堆栈 Blitz :https://stackblitz.com/edit/angular-ivy-x9nk16?file=src%2Fapp%2Fimage-slider.component.ts

Để ý:

  • 避免在 Angular 应用程序中使用 ElementRef,getElementsByClassName 等,这通常表明您没有使用 Angular 约定。

关于javascript - 如何将 HTML CSS 图像 slider 转换为 Angular 图像 slider ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68407015/

32 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com