@Component({
selector: 'my-slide-img',
templateUrl: 'slide-img.component.html',
styleUrls: ['slide-img.component.css'],
animations: [
trigger('imgMove', [
/** 不显示 */
state('off', style({'display': 'none', 'z-index': '0', 'transform': 'translateX(0)'})),
/** 上一张图片 */
state('prev', style({'z-index': '1',
'transform': 'translateX(-100%)'})),
/** 下一张图片 */
state('next', style({'z-index': '2', 'transform': 'translateX(100%)'})),
/** 当前图片 */
state('on', style({'z-index': '3', 'transform': 'translateX(0)'})),
transition('prev=>on', [
animate('0.3s ease-in')
]),
transition('next=>on', [
animate('0.3s ease-in')
]),
transition('on=>prev', [
animate('0.3s ease-in')
]),
transition('on=>next', [
animate('0.3s ease-in')
])
])
]
})
export class SlideImgComponent { }
@Input() public imgs: SlideImg[];
<my-slide-img [imgs]="imgs"></my-slide-img>
<div class="imgs">
<img src="{{img.Url}}" alt="" class="img"
*ngFor="let img of imgs;let i=index"
(mySwipe)="Change($event)"
[@imgMove]="ImgState(i)">
</div>
<div class="btn" (click)="Prev()">Prev</div>
<div class="btn" (click)="Next()">Next</div>
.imgs{
position: relative;width: 100%;height: 15em;
overflow: hidden;
}
.img{
position: absolute;
width: 100%;
height: 100%;
background: pink;
transition: 0.2s;
}
.btn{
display: inline-block;
padding: 1em 2em;font-size: 1em;border-radius: 0.5em;
border: 1px solid #ddd;cursor: pointer;
margin: 1em;background: #eee;box-shadow: 0.1em 0.1em 0.2em #aaa;
}
.btn:active{
background: #eee;
box-shadow: none;
}
import { Component, OnInit, Input,
animate,
style,
transition,
trigger,
state,
HostListener
} from '@angular/core';
import { SlideImg } from './slide-img.interface';
@Component({
selector: 'my-slide-img',
templateUrl: 'slide-img.component.html',
styleUrls: ['slide-img.component.css'],
animations: [
trigger('imgMove', [
/** 不显示 */
state('off', style({'display': 'none', 'z-index': '0', 'transform': 'translateX(0)'})),
/** 上一张图片 */
state('prev', style({'z-index': '1',
'transform': 'translateX(-100%)'})),
/** 下一张图片 */
state('next', style({'z-index': '2', 'transform': 'translateX(100%)'})),
/** 当前图片 */
state('on', style({'z-index': '3', 'transform': 'translateX(0)'})),
transition('prev=>on', [
animate('0.3s ease-in')
]),
transition('next=>on', [
animate('0.3s ease-in')
]),
transition('on=>prev', [
animate('0.3s ease-in')
]),
transition('on=>next', [
animate('0.3s ease-in')
])
])
]
})
export class SlideImgComponent {
@Input() public imgs: SlideImg[];
public current;
constructor() {
this.current = 0;
}
public ImgState(index) {
if (this.imgs && this.imgs.length) {
if (this.current === 0) {
return index === 0 ? 'on' :
index === 1 ? 'next' :
index === this.imgs.length - 1 ? 'prev' :
'off';
} else if (this.current === this.imgs.length - 1) {
return index === this.imgs.length - 1 ? 'on' :
index === this.imgs.length - 2 ? 'prev' :
index === 0 ? 'next' :
'off';
}
switch (index - this.current) {
case 0:
return 'on';
case 1:
return 'next';
case -1:
return 'prev';
default:
return 'off';
}
} else {
return 'off';
}
}
public Next() {
this.current = (this.current + 1) % this.imgs.length;
}
public Prev() {
this.current = this.current - 1 < 0 ? this.imgs.length - 1 : this.current - 1;
}
public Change(e) {
if (e === 'left') {
this.Next();
} else if (e === 'right') {
this.Prev();
}
}
}
import { Directive, Input, HostListener, Output, EventEmitter } from '@angular/core';
@Directive({ selector: '[mySwipe]' })
export class SwipeDirective {
@Output() public mySwipe = new EventEmitter<string>();
private touchStartX;
private touchStartY;
@HostListener('touchstart', ['$event']) public onTouchStart(e) {
this.touchStartX = e.changedTouches[0].clientX;
this.touchStartY = e.changedTouches[0].clientY;
}
@HostListener('touchend', ['$event']) public onTouchEnd(e) {
let moveX = e.changedTouches[0].clientX - this.touchStartX;
let moveY = e.changedTouches[0].clientY - this.touchStartY;
if (Math.abs(moveY) < Math.abs(moveX)) {
/**
* Y轴移动小于X轴 判定为横向滑动
*/
if (moveX > 50) {
this.mySwipe.emit('right');
} else if (moveX < -50) {
this.mySwipe.emit('left');
}
} else if (Math.abs(moveY) > Math.abs(moveX)) {
/**
* Y轴移动大于X轴 判定为纵向滑动
*/
if (moveY > 50) {
this.mySwipe.emit('down');
} else if (moveY < -50) {
this.mySwipe.emit('up');
}
}
this.touchStartX = this.touchStartY = -1;
}
}
@Output() public mySwipe = new EventEmitter<string>();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有