-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarousel.js
160 lines (127 loc) · 5.52 KB
/
Carousel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { createElement, Text, Wrapper } from './createElement'
import { Timeline, Animation } from './animation'
import { ease } from './cubicBezier'
import css from "./carousel.css"
export class Carousel {
constructor(config) {
this.children = [];
this.attributes = new Map();
this.properties = new Map();
}
setAttribute(name, value) {
this[name] = value;
}
appendChild(child) {
this.children.push(child)
}
render() {
let timeLine = new Timeline;
timeLine.start();
let position = 0;
let nextPicStopHandler = null
let children = this.data.map((url, currentPosition) => {
let lastPosition = (currentPosition - 1 + this.data.length) % this.data.length;
let nextPosition = (currentPosition + 1) % this.data.length;
let offset = 0;
let onStart = () => {
timeLine.pause()
clearTimeout(nextPicStopHandler)
let currentElement = children[currentPosition];
let currentTransformValue = Number(currentElement.style.transform.match(/^translateX\(([\s\S]+)px\)/)[1]);
offset = currentTransformValue + 500 * currentPosition;
}
let onPan = event => {
let lastElement = children[lastPosition];
let currentElement = children[currentPosition];
let nextElement = children[nextPosition];
let dx = event.clientX - event.startX;
let currentTransformValue = -500 * currentPosition + offset + dx;
let lastTransformValue = - 500 - 500 * lastPosition + offset + dx;
let nextTransformValue = 500 - 500 * nextPosition + offset + dx;
lastElement.style.transform = `translateX(${lastTransformValue}px)`;
currentElement.style.transform = `translateX(${currentTransformValue}px)`;
nextElement.style.transform = `translateX(${nextTransformValue}px)`;
}
let onPanend = event => {
let direction = 0;
let dx = event.clientX - event.startX;
if (dx + offset > 250 || dx > 0 && event.isFlick) {
direction = 1;
} else if (dx + offset < -250 || dx < 0 && event.isFlick) {
direction = -1;
}
timeLine.reset();
timeLine.restart();
let lastElement = children[lastPosition];
let currentElement = children[currentPosition];
let nextElement = children[nextPosition];
let lastAnimation = new Animation(
lastElement.style,
"transform",
- 500 - 500 * lastPosition + offset + dx,
- 500 - 500 * lastPosition + direction * 500,
500, 0, ease, v => `translateX(${v}px)`
);
let currentAnimation = new Animation(
currentElement.style,
"transform",
-500 * currentPosition + offset + dx,
- 500 * currentPosition + direction * 500,
500, 0, ease, v => `translateX(${v}px)`
);
let nextAnimation = new Animation(
nextElement.style,
"transform",
500 - 500 * nextPosition + offset + dx,
500 - 500 * nextPosition + direction * 500,
500, 0, ease, v => `translateX(${v}px)`
);
timeLine.add(lastAnimation);
timeLine.add(currentAnimation);
timeLine.add(nextAnimation);
position = (position - direction + this.data.length) % this.data.length;
nextPicStopHandler = setTimeout(nextPic, 3000)
}
let element = <img src={url} onStart={onStart} onPan={onPan} onPanend={onPanend} enableGesture={true} />;
element.style.transform = "translateX(0px)"
element.addEventListener("dragstart", event => event.preventDefault())
return element
})
let nextPic = () => {
let nextPosition = (position + 1) % this.data.length;
// 当前图片位置
let current = children[position];
// 下一张图片位置
let next = children[nextPosition]
/**
* 轮播步骤:
* 1. 轮播时图片时两两一组进行
* 2. 把当前展示的图向左滑动一个位置
* 3. 把当前
*/
let start = -100 * position;
let end = -100 - 100 * position;
let currentAnimation = new Animation(current.style, "transform",
start,
end,
500, 0, ease, v => `translateX(${5 * v}px)`
);
let nextAnimation = new Animation(next.style, "transform",
100 - 100 * nextPosition,
-100 * nextPosition,
500, 0, ease, v => `translateX(${5 * v}px)`
);
timeLine.add(currentAnimation);
timeLine.add(nextAnimation);
position = nextPosition;
nextPicStopHandler = setTimeout(nextPic, 3000)
}
nextPicStopHandler = setTimeout(nextPic, 3000)
return <div class="carousel">
{children}
</div>;
}
mountTo(parent) {
this.render().mountTo(parent)
}
}