Skip to content

Commit e9072d9

Browse files
李钿李钿
authored andcommitted
变更图片切换策略
1 parent 0a0d721 commit e9072d9

File tree

3 files changed

+165
-62
lines changed

3 files changed

+165
-62
lines changed

example/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class App extends Component {
2424
{
2525
this.state.imags.map(item => {
2626
console.info("111",item);
27-
return <div className="img">
27+
return <div className="img" key={item}>
2828
<img src={item} alt="" width="100%" height="auto" className=""/>
2929
</div>
3030
})

src/components/ImageContainer.jsx

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
33

4-
const IMG_RESET_SIZE = {
5-
width: 0,
6-
height: 0,
7-
left: 0,
8-
top: 0,
4+
/**
5+
*
6+
* @param {number} value
7+
* @param {number} min
8+
* @param {number} max
9+
*/
10+
11+
function setScope(value, min, max){
12+
if(value < min){ return min}
13+
if(value > max){ return max}
14+
return value;
915
}
1016

1117
/**
@@ -25,19 +31,16 @@ const IMG_RESET_SIZE = {
2531
* 对应关系: newPointLeft = zoom * oldPointLeft
2632
* newPointTop = zoom * oldPointTop
2733
*
28-
* 坐标位置:left = (newPointLeft - oldPointLeft) = (zoom - 1) * oldPointLeft
29-
* top = (newPointTop - oldPointTop) = (zoom - 1) * oldPointTop
34+
* 坐标位置:left = startLeft + (newPointLeft - oldPointLeft) = (zoom - 1) * oldPointLeft
35+
* top = startTop + (newPointTop - oldPointTop) = (zoom - 1) * oldPointTop
3036
*/
3137

32-
function setScope(value, min, max){
33-
if(value < min){ return min}
34-
if(value > max){ return max}
35-
return value;
36-
}
37-
3838
class ImageContainer extends PureComponent {
3939
static propTypes = {
4040
maxZoomNum: PropTypes.number,
41+
handleStart: PropTypes.func,
42+
handleMove: PropTypes.func,
43+
handleEnd: PropTypes.func
4144
}
4245

4346
static defaultProps = {
@@ -66,21 +69,25 @@ class ImageContainer extends PureComponent {
6669

6770
switch (event.touches.length) {
6871
case 1:
69-
// event.preventDefault();
70-
// event.stopPropagation();
72+
event.preventDefault();
7173
let targetEvent = event.touches[0];
7274
this.startX = targetEvent.clientX;
7375
this.startY = targetEvent.clientY;
7476

7577
this.startLeft = this.state.left;
7678
this.startTop = this.state.top;
79+
80+
if(this.state.width === this.originWidth){
81+
this.callHandleStart()
82+
}
7783
break;
7884

7985
case 2: //两个手指
8086
event.preventDefault();
8187
event.stopPropagation();
8288

83-
this.isTwoFinger = true;
89+
//设置手双指模式
90+
this.isTwoFingerMode = true;
8491

8592
//计算两个手指中间点屏幕上的坐标
8693
const middlePointClientLeft = Math.abs(Math.round((event.touches[ 0 ].clientX + event.touches[ 1 ].clientX) / 2));
@@ -95,60 +102,59 @@ class ImageContainer extends PureComponent {
95102
//计算手指中间点在图片上的位置(坐标值)
96103
this.oldPointLeft = middlePointClientLeft - this.startLeft;
97104
this.oldPointTop = middlePointClientTop - this.startTop;
98-
console.info("this.oldPointLeft = %s; this.oldPointTop = %s", this.oldPointLeft, this.oldPointTop);
99105

100106
var dx = event.touches[ 0 ].clientX - event.touches[ 1 ].clientX;
101107
var dy = event.touches[ 0 ].clientY - event.touches[ 1 ].clientY;
102108
this._touchZoomDistanceStart = this._touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
103-
104-
console.info("this._touchZoomDistanceStart = ", this._touchZoomDistanceStart);
105109
break;
106-
107110
default:
108111
break;
109112
}
110113
}
111114

112115
handleTouchMove = (event) =>{
113-
// console.info("handleTouchMove = %s",event.touches[ 0 ].clientX)
114-
115116
switch (event.touches.length) {
116117
case 1:
117118
let targetEvent = event.touches[0],
118119
diffX = targetEvent.clientX - this.startX,
119120
diffY = targetEvent.clientY - this.startY;
120121

121-
122-
let top = (this.props.screenHeight - this.state.height)/2,
123-
left = setScope(this.startLeft + diffX, ( this.originWidth - this.state.width ), 0 );
122+
//图片宽度等于初始宽度,直接调用 handleMove 函数
123+
if(this.state.width === this.originWidth){
124+
if(this.props.handleMove){
125+
this.props.handleMove(diffX);
126+
}
127+
} else{
128+
let top = (this.props.screenHeight - this.state.height)/2,
129+
left = this.startLeft + diffX;
124130

125-
if(this.state.height > this.props.screenHeight){
126-
top = setScope(this.startTop + diffY, ( this.props.screenHeight - this.state.height ), 0 );
127-
}
131+
if(this.state.height > this.props.screenHeight){
132+
top = setScope(this.startTop + diffY, ( this.props.screenHeight - this.state.height ), 0 );
133+
}
134+
135+
if(left < this.originWidth - this.state.width){
136+
this.callHandleStart();
137+
if(this.props.handleMove){
138+
this.props.handleMove(left + this.state.width - this.originWidth);
139+
}
140+
} else if(left > 0){
141+
this.callHandleStart();
142+
if(this.props.handleMove){
143+
this.props.handleMove(left);
144+
}
145+
}
146+
147+
left = setScope(left, this.originWidth - this.state.width, 0);
148+
149+
console.info("this.startX = %s, this.startY = %s, this.startLeft = %s, this.startTop = %s, diffX = %s, diffY = %s", this.startX, this.startY, this.startLeft, this.startTop, diffX, diffY);
150+
this.setState({
151+
left,
152+
top
153+
})
128154

129-
console.info("this.startX = %s, this.startY = %s, this.startLeft = %s, this.startTop = %s, diffX = %s, diffY = %s", this.startX, this.startY, this.startLeft, this.startTop, diffX, diffY);
130-
this.setState({
131-
left,
132-
top
133-
})
134-
if(this.startLeft + diffX < 0 && this.startLeft + diffX > this.originWidth - this.state.width){
135-
event.preventDefault();
136-
event.stopPropagation();
137155
}
138-
// this.setState((prevState, props) => {
139-
// let top = (props.screenHeight - prevState.height)/2,
140-
// left = setScope(this.startLeft + diffX, ( this.originWidth - prevState.width ), 0 );
141-
142-
// if(prevState.height > props.screenHeight){
143-
// top = setScope(this.startTop + diffY, ( props.screenHeight - prevState.height ), 0 );
144-
// }
145-
146-
// console.info("left = %s ; top = %s", left, top);
147-
// return {
148-
// left,
149-
// top
150-
// }
151-
// })
156+
157+
event.preventDefault();
152158
break;
153159

154160
case 2: //两个手指
@@ -185,8 +191,8 @@ class ImageContainer extends PureComponent {
185191

186192
handleTouchEnd = (event) =>{
187193
console.info("handleTouchEnd", event.touches.length);
188-
if(this.isTwoFinger){
189-
this.isTwoFinger = false;
194+
if(this.isTwoFingerMode){//双指操作结束
195+
this.isTwoFingerMode = false;
190196
let left, top, width, height;
191197

192198
width = setScope(this.state.width, this.originWidth, this.props.maxZoomNum * this.originWidth);
@@ -220,6 +226,25 @@ class ImageContainer extends PureComponent {
220226
this.startTop = top;
221227
console.info("this.startX = %s, this.startY = %s, this.startLeft = %s, this.startTop = %s", this.startX, this.startY, this.startLeft, this.startTop);
222228
}
229+
} else{//单指结束(ontouchend)
230+
this.callHandleEnd();
231+
}
232+
event.preventDefault();
233+
}
234+
235+
callHandleStart = () =>{
236+
if(!this.isCalledHandleStart){
237+
this.isCalledHandleStart = true;
238+
if(this.props.handleStart){
239+
this.props.handleStart();
240+
}
241+
}
242+
}
243+
244+
callHandleEnd = () =>{
245+
this.isCalledHandleStart = false;
246+
if(this.props.handleEnd){
247+
this.props.handleEnd();
223248
}
224249
}
225250

src/components/ListContainer.jsx

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,53 @@ class ListContainer extends PureComponent {
9595
}
9696

9797
state = {
98-
width: 0,
98+
left: 0,
99+
isNeedSpring: false
100+
}
101+
102+
componentWillMount() {
103+
const {
104+
screenWidth,
105+
urls,
106+
index,
107+
gap
108+
} = this.props;
109+
110+
this.length = urls.length;
111+
this.perDistance = screenWidth + gap;
112+
this.maxLeft = this.perDistance * (this.length - 1);
113+
114+
this.setState({
115+
left: - this.perDistance * index,
116+
isNeedSpring: false
117+
})
118+
}
119+
120+
handleStart = () =>{
121+
console.info("ListContainer handleStart")
122+
this.startLeft = this.state.left;
123+
this.setState({
124+
isNeedSpring: false
125+
})
126+
}
127+
128+
handleMove = (diffX) =>{
129+
console.info("ListContainer handleStart diffX = %s",diffX);
130+
131+
this.setState({
132+
left: this.startLeft + diffX,
133+
isNeedSpring: false
134+
})
135+
}
136+
137+
handleEnd = () =>{
138+
let left = Math.round(this.state.left / this.perDistance) * this.perDistance
139+
if(left > 0){ left = 0}
140+
if(left < -this.maxLeft){ left = -this.maxLeft}
141+
this.setState({
142+
left,
143+
isNeedSpring: true
144+
})
99145
}
100146

101147
render() {
@@ -107,22 +153,54 @@ class ListContainer extends PureComponent {
107153
gap
108154
} = this.props;
109155

110-
let defaultStyle = {
111-
left: - (screenWidth + gap) * index
112-
}
156+
const {
157+
left,
158+
isNeedSpring
159+
} = this.state
113160

114161
return (
115-
<HandleWrapContainer
162+
<Motion style={{x: isNeedSpring ? spring(left) : left}}>
163+
{
164+
({x}) => {
165+
let defaultStyle = {
166+
WebkitTransform: `translate3d(${x}px, 0, 0)`,
167+
transform: `translate3d(${x}px, 0, 0)`,
168+
}
169+
170+
return (
171+
<div
172+
className="viewer-list-container"
173+
style={defaultStyle}
174+
>
175+
{
176+
urls.map((item,i) => <ImageContainer
177+
key={i}
178+
src={item}
179+
handleStart={this.handleStart}
180+
handleMove={this.handleMove}
181+
handleEnd={this.handleEnd}
182+
left={this.perDistance * i}
183+
screenWidth={screenWidth}
184+
screenHeight={screenHeight}/>)
185+
}
186+
</div>
187+
)
188+
}
189+
}
190+
</Motion>
191+
/*<HandleWrapContainer
116192
style={defaultStyle}
117193
diff={screenWidth + gap}
118194
maxW={(screenWidth + gap)*(urls.length-1)}>
119-
{ urls.map((item,i) => <ImageContainer
195+
{
196+
urls.map((item,i) => <ImageContainer
120197
key={i}
121198
src={item}
122-
left={(screenWidth + gap) * i}
199+
left={this.perDistance * i}
123200
screenWidth={screenWidth}
124-
screenHeight={screenHeight}/>) }
125-
</HandleWrapContainer>
201+
screenHeight={screenHeight}/>)
202+
}
203+
</HandleWrapContainer>*/
126204
);
127205
}
128206
}

0 commit comments

Comments
 (0)